This page as markdown: /data-table.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt
DataTable
Status: stable
A data-driven table over the Table primitives — a columns def + data array with built-in sorting (click a sortable header to cycle asc → desc → none; locale-aware string order; controlled with sort + onSortChange), optional row click / selection, rows grouped under collapsible header rows (groupBy), and an empty state. For server data or paging, control the sort, set manualSorting, and pair with Pagination.
Keywords: data table, table, sortable, columns, rows, empty state, pagination, list, grouped, group by, sections, issue list, issue tracker, status groups, collapsible groups
Usage
Use DataTable when a data array and column definition should render a sortable, semantic table with optional row actions and an empty state. Use the lower-level Table parts for hand-authored markup or highly custom row layouts.
Best practices
- Do: Put a table that fills a card in
. The table runs edge to edge and its first and last columns inset to the card padding on their own; do not hand-set pl-6 / pr-6 on columns. - Do: Provide stable rowKey values for dynamic data so selection and sorting do not jump.
- Do: Use sortable only on columns where ordering is obvious and useful.
- Do: For server-side sorting and paging, control the sort (sort + onSortChange), set manualSorting, and pass the page the server returned with Pagination beside it. Without manualSorting a sortable header reorders only the rows on the current page.
- Do: Group rows by status for an issue tracker (or by owner, by day) with groupBy + groups on ONE DataTable: every group shares the header and the column widths, each header row carries the group's count and an add action, and sorting applies within each group. Do not stack one DataTable per group inside Collapsibles; the header repeats and the columns stop lining up.
- Do: Move to TanStack Table, rendered with Sable's Table parts, once the table needs server-side sorting or filtering, column visibility or resizing, nested or aggregated grouping, or virtualised rows. DataTable stays dependency-free and does not grow into a data grid;
sable docs tables-with-tanstackmaps one onto the other. - Don't: Make every row clickable and also fill it with competing buttons. Choose a clear row interaction model.
- Don't: Use DataTable for heterogeneous cards or timeline content. A list or card layout will scan better.
Anatomy
| Part | Required | Description |
|---|---|---|
| Columns | yes | Definitions for headers, cell renderers, sorting, alignment, and classes. |
| Rows | yes | The data array rendered through the column definitions. |
| Empty state | no | Content spanning all columns when the row array is empty. |
| Group header row | no | With groupBy: one row per group heading its own tbody (a th with scope=rowgroup across every column) holding the disclosure toggle, the group's icon, label and count, and an optional trailing action. |
| Caption | no | Accessible table caption rendered by the underlying Table. |
Props
| Name | Type | Default | Description | |||
|---|---|---|---|---|---|---|
| columns | DataTableColumn |
— | Column defs: { key, header, cell?, sortValue?, sortable?, align?, className? }. cell defaults to row[key]; sortValue defaults to row[key]. When cell renders a formatted string (e.g. a date), pass sortValue returning the underlying value (e.g. date.getTime()) so the column sorts by the real value, not the rendered text. | |||
| data | Row[] | — | The rows to render. | |||
| rowKey | (row, index) => string | number | index in data | Stable key per row, used for the React key and for selection. index is always the row's place in data, never its sorted or grouped position. | |||
| onRowClick | (row) => void | — | Row click handler — makes rows pointer-interactive. | |||
| isRowSelected | (row) => boolean | — | Mark a row visually selected (data-selected), e.g. the one open in a detail pane. Derive it from a stable id (row.id === selectedId), not the row index, so selection survives re-sorts. For checking several rows, use selectedKeys. | |||
| selectedKeys | ReadonlySet<string | number> | — | The checked rows, by rowKey, for bulk actions. With onSelectedKeysChange it adds a checkbox column (each box named by rowSelectionLabel) and a select-all box in the header that acts on the rows shown and goes mixed when some are checked. Checked rows get the selected fill. Controlled: when the rows shown change (a filter, another view), set the keys to keep, usually none. Needs rowKey. | |||
| onSelectedKeysChange | (keys: Set<string | number>) => void | — | Called with the new set of checked keys. Its presence turns selection on. A click on a box never fires onRowClick. | |||
| rowSelectionLabel | (row) => string | () => "Select row" | A row checkbox's accessible name. Name the row, e.g. (t) => Select ${t.subject}, so a screen reader can tell the boxes apart. |
|||
| selectAllLabel | (count: number) => string | (count) => Select all ${count} shown |
The select-all checkbox's accessible name; say what it acts on ("Select all 7 tickets shown"). | |||
| defaultSort | DataTableSort ({ key: string; direction: "asc" | "desc" }) | — | Initial sort column + direction, uncontrolled. | |||
| sort | DataTableSort | null | — | The sort, controlled; null for unsorted. A header click calls onSortChange with the next step of asc → desc → none and changes nothing by itself. | |||
| onSortChange | (sort: DataTableSort | null) => void | — | Called with the next sort when a sortable header is clicked, controlled or not. Fetch the next page from here for server-sorted data. | |||
| manualSorting | boolean | false | Skip the local sort: data already arrives in sort order (a server sorts and pages). Headers still show aria-sort and report clicks. | |||
| locale | string | the reader's locale | BCP 47 locale strings are compared in. Comparison is case-insensitive, ignores accents, and orders digits by value ("file2" before "file10"); dates and numbers compare by value; ties keep the order of data. | |||
| empty | ReactNode | "No results." | Rendered spanning all columns when data is empty. Pass an EmptyState for the full treatment (icon, title, next step). | |||
| caption | ReactNode | — | Bottom table caption. | |||
| groupBy | (row) => string | — | Group the rows under header rows by the key this returns (a status, an owner, a day). One header serves every group, so the columns line up across them; sorting applies within each group. | |||
| groups | { key: string; label?: ReactNode; icon?: ReactNode; action?: ReactNode; defaultExpanded?: boolean }[] | — | Order and presentation of the groups, by key: the header row's label (defaults to the key), a leading icon such as a status mark, a trailing action such as an icon Button that adds a row to the group, and whether it starts open (default true). Groups groupBy returns that are not listed follow, in the order their first row appears in data. A listed group with no rows is not shown. | |||
| collapsible | boolean | true | Whether each group's header row is a toggle that opens and closes the group. A closed group keeps its header row and count; its rows are hidden and select-all skips them. | |||
| expandedGroups | ReadonlySet |
— | The open groups, by key (controlled). Without it each group starts at its defaultExpanded and the table holds the state. | |||
| onExpandedGroupsChange | (keys: Set |
— | Called with the new set of open group keys when a group header is toggled. | |||
| ref / ...props | ComponentPropsWithRef<"table"> | — | Remaining DOM props (aria-label, id, data-*) and the ref land on the Tokens used
Motion
Accessibility
|