Tables
DataTable is the table on the Users, Invoices, Orders and Files pages. It
is built on TanStack Table (MIT) and handles:
- sorting;
- search and filter chips;
- row selection with a bulk-action bar;
- a column picker;
- pagination;
- loading, empty and error states.
1. The smallest table
import { DataTable, type ColumnDef } from "@pulze/blocks"
const columns: ColumnDef<Invoice, unknown>[] = [
{ accessorKey: "number", header: "Invoice" },
{ accessorKey: "total", header: "Amount", cell: ({ getValue }) => formatMoney(getValue() as number) },
]
<DataTable columns={columns} data={invoices} getRowId={(i) => i.id} />
That already sorts (click a header), pages (8 rows by default) and shows an empty state.
2. Columns
Each column is a TanStack ColumnDef:
| Field | Example | Notes |
|---|---|---|
accessorKey |
"name" |
Field to read and sort by |
id |
"name" |
Needed when there's no accessorKey; used by the column picker |
header |
"User" |
Text (also the label in the column picker). "" for a row-actions column: screen readers hear "Actions" |
cell |
({ row }) => <Avatar … /> |
Custom rendering; row.original is the record |
enableSorting |
false |
Turn sorting off for one column |
enableHiding |
false |
Keep it out of the column picker |
size |
120 |
Fixed width in px |
sortUndefined |
"last" |
Where empty values go when sorting |
Status column:
{ accessorKey: "status", header: "Status",
cell: ({ row }) => <StatusPill tone={TONE[row.original.status]}>{row.original.status}</StatusPill> }
3. Toolbar: search, filters, actions
<DataTable
…
search={{ value: q, onChange: setQ, placeholder: "Search name or email" }}
filters={
<FilterChip label="Status" value={statuses} onChange={setStatuses}
options={[{ value: "paid", label: "Paid", count: 41 }, …]} />
}
toolbarActions={<Button variant="outline" size="sm"><Download />Export CSV</Button>}
columnPicker
header={<StatusTabs />} // optional, above the toolbar
/>
search and filters don't filter the data themselves. You filter (or ask
your API) and pass the result as data. That keeps the table the same
whether the data is local or on a server.
4. Selection and bulk actions
<DataTable
…
selectable
bulkActions={(rows, clear) => (
<>
<BulkAction onClick={() => email(rows)}><Mail />Email</BulkAction>
<BulkAction danger onClick={() => { remove(rows); clear(); toast.undo(`${rows.length} deleted`, () => restore(rows)) }}>
<Trash2 />Delete
</BulkAction>
</>
)}
/>
The dark bar slides up when rows are selected. "Select all" selects the current page. The selection clears when the data changes.
5. Client mode vs server mode
Client mode (default): pass all rows. Sorting and paging happen in the browser. Good up to a few thousand rows.
Server mode: pass total, and handle paging and sorting yourself:
const [pagination, setPagination] = useState({ page: 1, pageSize: 20 })
const [sorting, setSorting] = useState<SortingState>([{ id: "createdAt", desc: true }])
const q = useQuery({
queryKey: ["orders", pagination, sorting, search],
queryFn: () => getOrders({
page: pagination.page,
pageSize: pagination.pageSize,
sort: sorting[0] ? { key: sorting[0].id as keyof Order, dir: sorting[0].desc ? "desc" : "asc" } : undefined,
search,
}),
placeholderData: (prev) => prev, // keep rows on screen while the next page loads
})
<DataTable
columns={columns}
data={q.data?.items ?? []}
total={q.data?.total ?? 0}
pagination={pagination} onPaginationChange={setPagination}
sorting={sorting} onSortingChange={setSorting}
loading={q.isPending} error={q.isError} onRetry={q.refetch}
/>
6. Other options
| Prop | Notes |
|---|---|
pageSizes |
Choices in "Rows per page" (default [8, 20, 50]) |
loading / error / onRetry |
Skeleton rows, or an error with "Try again" |
empty |
Your own empty state (defaults to "No results") |
onRowClick |
Makes rows clickable, e.g. open a detail page |
7. CSV export
function toCsv(rows: User[]) {
const head = ["Name", "Email", "Role"]
const body = rows.map((u) =>
[u.name, u.email, u.role].map((v) => `"${String(v).replace(/"/g, '""')}"`).join(",")
)
const blob = new Blob([[head.join(","), ...body].join("\n")], { type: "text/csv" })
const a = Object.assign(document.createElement("a"), {
href: URL.createObjectURL(blob),
download: "users.csv",
})
a.click()
}