UIComponentsReact
A table of rows that sorts, filters, pages and selects, on TanStack Table.
Built on the headless Data Table.See its API in Docs
Rendered by React 19.3.1
Installation
npx shadcn@4.23.0 add https://hamur.dev/r/react/data-table.jsonIt also adds utils, button, checkbox, dropdown-menu, input, select, table. The app needs its setup first: Installation.
Usage
Define the columns with createDataTableColumnHelper<Row>() and create the table with useDataTable({ columns, data }); both are TanStack Table's createTableHook bound to the item's features (sorting, column filters, pagination, row selection and column visibility), so the table's state lives in TanStack's store. Pass the table to DataTable for the rows, rendered by the headless Data Table in table mode, DataTablePagination for the selected count, the page size and the page buttons, and DataTableViewOptions for a menu that shows or hides columns. A column's header can be a DataTableColumnHeader with its column and title: a menu that sorts it ascending or descending and hides it. A filter is an Input whose value is the column's filter value (column.setFilterValue), and a selection column renders DataTableSelectAllCheckbox in its header and DataTableRowCheckbox in its cell: they read and change TanStack's row selection through the table, select every row of the page or clear them, and select a range with Shift. Name the table with aria-label (or aria-labelledby) on DataTable, and pass rowHeader, the id of the column whose cell names each row (the first data column by default): that cell is a row header, and each row's checkbox is read as "Select" and that name. A table without selection passes enableRowSelection: false to useDataTable. initialState, or state with its on…Change, set the starting or controlled sorting, filters, page and selection; every other TanStack option (getRowId, enableRowSelection, …) goes to useDataTable as it is. To use another TanStack feature, add it to dataTableFeatures in the copied file.
import {
DataTable,
DataTableColumnHeader,
DataTablePagination,
DataTableRowCheckbox,
DataTableSelectAllCheckbox,
DataTableViewOptions,
} from '@/components/ui/data-table'Examples
Default
shadcn's payments: a selection column, an email filter, sortable columns, a row menu, the columns menu, pages.
Differences from shadcn's: shadcn's data table is a guide and an example, not a registry item; this item ships the guide's reusable parts (DataTable, DataTableColumnHeader, DataTablePagination, DataTableViewOptions) and installs @tanstack/react-table 9.3.1; the page's example imports TanStack only through the item. shadcn's guide is TanStack Table v8 (useReactTable with getCoreRowModel(), getSortedRowModel() and the rest, and React.useState for each state slice); v9 registers features and row models once, so the item exports useDataTable and createDataTableColumnHelper from TanStack's createTableHook and keeps the state in TanStack's store instead of the app's useState (pass state and on…Change to control a slice). DataTable takes the table rather than columns and data, so the toolbar and the pagination read the same table. A column defined with createDataTableColumnHelper types its header and cell with the item's features, so pass DataTableColumnHeader a column of that table. The menus are the Dropdown Menu item over Base UI's Menu: the header trigger is highlighted while its menu is open through data-popup-open (shadcn's data-[state=open]), and the "Toggle columns" label sits in a menu group. The page-size select is the Select item over Base UI's Select, with number values. The page number and the page buttons are Base UI Next's Pagination in button mode (Value, First, Previous, Next, Last rendering the Button item, inside a nav landmark), so a button at its bound is aria-disabled rather than disabled and keeps focus where shadcn's loses it on the last page. The table is a native table with its roles written out, the sorted column's header carries aria-sort whichever control sorted it, and a sort change is announced in a polite status region after the table. The checkbox column is the item's own DataTableRowCheckbox and DataTableSelectAllCheckbox where shadcn renders its Checkbox with row.toggleSelected and table.toggleAllPageRowsSelected; the select-all box is mixed, with a dash, while some but not all of the page's rows are selected, and selecting the page keeps the rows selected on other pages. Rows keep shadcn's data-state="selected" beside Base UI's data-selected. Icons are lucide's, inline.
API reference
The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Data Table's API reference.