Guides
A customizable React data grid
For teams replacing a spreadsheet-like screen: when a data grid is the right tool, how to wire ReactLoom's DataGrid to your data, edit cells safely, group with totals, export, and style it without forking.
Grid or table?
A grid (role="grid") turns the whole data set into one tab stop that you explore with arrow keys, like a spreadsheet. That suits data people work in: triaging orders, editing prices, reconciling invoices. For data people only read, a Table is better: screen readers browse it with their normal table commands and there is nothing new to learn. DataGrid is in its own package, @reactloom/data-grid, because it brings TanStack Table and TanStack Virtual (about 32 kB brotli).
Your data, your state
Columns are plain objects: a key, a header, and optional cell (render), value (for sorting, search and export), sortable, width and align. Every piece of state — sort, page, search, selection, column widths and visibility, grouping — can be controlled, so you can keep it in the URL or a store and restore it.
For large data sets, let the server sort and page, and tell the grid how many rows exist:
Server-side sorting and pagination
import { DataGrid, type DataGridColumn, type SortDescriptor } from '@reactloom/data-grid';
import { useDataGridDataSource, type DataGridServerRequest } from '@reactloom/data-grid/server';
import { Button, SearchInput, Stack } from '@reactloom/ui';
import { useState } from 'react';
import { makeOrders, money, type Order } from './data';
const columns: Array<DataGridColumn<Order>> = [
{ key: 'id', header: 'Order', sortable: true, width: 120 },
{ key: 'customer', header: 'Customer', sortable: true, width: 190 },
{ key: 'total', header: 'Total', sortable: true, align: 'end', width: 130, cell: (o) => money.format(o.total) },
];
const database = makeOrders(240);
const pageSize = 10;
// This demo uses in-memory rows. In an app, forward the query and signal to fetch().
async function fetchOrders({ offset, limit, sorts, search, signal }: DataGridServerRequest) {
await new Promise<void>((resolve, reject) => {
const abort = () => { clearTimeout(timer); reject(signal.reason); };
const timer = setTimeout(() => {
signal.removeEventListener('abort', abort);
resolve();
}, 350);
signal.addEventListener('abort', abort, { once: true });
});
const rows = database.filter((order) => order.customer.toLowerCase().includes(search.trim().toLowerCase()));
rows.sort((a, b) => {
for (const { column, direction } of sorts) {
const comparison = column === 'total' ? a.total - b.total
: column === 'customer' ? a.customer.localeCompare(b.customer) : a.id.localeCompare(b.id);
if (comparison) return direction === 'descending' ? -comparison : comparison;
}
return 0;
});
return { rows: rows.slice(offset, offset + limit), rowCount: rows.length };
}
export default function Example() {
const [page, setPage] = useState(1);
const [sorts, setSorts] = useState<SortDescriptor[]>([]);
const [search, setSearch] = useState('');
const { rows, rowCount, loading, error, refresh } = useDataGridDataSource(fetchOrders, { page, pageSize, sorts, search });
return (
<Stack gap="3" style={{ width: '100%' }}>
<SearchInput label="Search server orders" value={search} onValueChange={(value) => { setSearch(value); setPage(1); }} />
{error && <div role="alert">Could not load orders. <Button size="sm" onClick={refresh}>Retry</Button></div>}
<DataGrid
aria-label="Orders (server)"
columns={columns}
rows={rows}
getRowId={(order) => order.id}
manualSorting
manualFiltering
manualPagination
rowCount={rowCount}
pageSize={pageSize}
page={page}
onPageChange={setPage}
sorts={sorts}
onSortsChange={(next) => { setSorts(next); setPage(1); }}
search={search}
loading={loading}
emptyState={error ? 'Orders unavailable' : 'No orders found'}
/>
</Stack>
);
}Editing cells
Mark a column editable (or pass a function to decide per row) and choose an editor. The grid follows the APG grid pattern: Enter, F2 or typing starts editing, Enter saves, Escape cancels, and focus returns to the cell. You keep the data: onCellEdit tells you what changed and you update your rows.
Validated edits
import { DataGrid, type DataGridColumn } from '@reactloom/data-grid';
import { Badge } from '@reactloom/ui';
import { useState } from 'react';
import { makeOrders, money, type Order } from './data';
const tone = { Paid: 'success', Pending: 'warning', Refunded: 'neutral' } as const;
const columns: Array<DataGridColumn<Order>> = [
{ key: 'id', header: 'Order', width: 120 },
{
key: 'customer',
header: 'Customer',
width: 200,
editable: true,
validate: (value) => (String(value).trim() ? undefined : 'Enter a customer name'),
},
{
key: 'status',
header: 'Status',
width: 140,
// Refunded orders are final.
editable: (order) => order.status !== 'Refunded',
editor: 'select',
options: [
{ value: 'Paid', label: 'Paid' },
{ value: 'Pending', label: 'Pending' },
{ value: 'Refunded', label: 'Refunded' },
],
cell: (o) => <Badge tone={tone[o.status]}>{o.status}</Badge>,
},
{
key: 'items',
header: 'Items',
align: 'end',
width: 100,
editable: true,
editor: 'number',
validate: (value) => (typeof value === 'number' && Number.isInteger(value) && value > 0 ? undefined : 'Enter a whole number above 0'),
},
{ key: 'total', header: 'Total', align: 'end', width: 130, cell: (o) => money.format(o.total) },
];
export default function Example() {
const [orders, setOrders] = useState(() => makeOrders(6));
return (
<DataGrid
aria-label="Editable orders"
columns={columns}
rows={orders}
getRowId={(o) => o.id}
onCellEdit={({ rowId, column, value }) =>
// Save to your API here; return the promise to show "Saving…" and surface errors.
setOrders((rows) => rows.map((o) => (o.id === rowId ? { ...o, [column]: value } : o)))
}
/>
);
}- Validate twice.
validategives instant feedback in the cell; your API must still check, because a client can send anything. - Return the save promise. The editor stays open with “Saving…” and, if the request fails, shows the error message in the cell instead of silently reverting.
- Numbers follow the user’s locale. A German user can type
1.234,5and you receive1234.5.
Grouping and totals
groupBy groups rows by a column’s value under rows that open and close; aggregate on a column adds a sum, average, minimum, maximum or count to each group. Grouping runs in the browser on the rows you pass; for millions of rows, group on the server and pass the results.
Orders grouped by region
import { DataGrid, type DataGridColumn } from '@reactloom/data-grid';
import { Select, Stack } from '@reactloom/ui';
import { useState } from 'react';
import { makeOrders, money, type Order } from './data';
const columns: Array<DataGridColumn<Order>> = [
{ key: 'id', header: 'Order', sortable: true, width: 120 },
{ key: 'customer', header: 'Customer', sortable: true, width: 190 },
{ key: 'status', header: 'Status', sortable: true, width: 120 },
{ key: 'region', header: 'Region', sortable: true, width: 110 },
{ key: 'items', header: 'Items', align: 'end', width: 90, aggregate: 'sum' },
{ key: 'total', header: 'Total', sortable: true, align: 'end', width: 130, cell: (o) => money.format(o.total), aggregate: 'sum', formatAggregate: (n) => money.format(n) },
];
const rows = makeOrders(24);
export default function Example() {
const [groupBy, setGroupBy] = useState<string | null>('region');
return (
<Stack gap="3" style={{ width: '100%' }}>
<div style={{ maxWidth: 220 }}>
<Select
label="Group by"
value={groupBy ?? 'none'}
onValueChange={(v) => setGroupBy(v === 'none' ? null : v)}
options={[
{ value: 'none', label: 'No grouping' },
{ value: 'region', label: 'Region' },
{ value: 'status', label: 'Status' },
{ value: 'customer', label: 'Customer' },
]}
/>
</div>
<DataGrid aria-label="Orders by group" columns={columns} rows={rows} getRowId={(o) => o.id} groupBy={groupBy} pageSize={12} density="compact" />
</Stack>
);
}Export
The export section covers CSV: the grid exports what the user is looking at — search, sort and visible columns, every page — as values rather than formatted text, and neutralises spreadsheet formulas in text cells.
Styling without forking
The grid uses theme tokens for colour, spacing and type, so a brand theme restyles it with the rest of the app. Its styles are in the pl.components cascade layer, so your own CSS wins without !important; density and striped cover the common variations. The customization reference lists the class names, state attributes and tokens it uses.