Base · Data display
Data table
A table with sortable columns and an optional filter, driven by column definitions.
| PDC-4821 | Rotterdam | In transit | 18 |
| PDC-4822 | Hamburg | Delivered | 6 |
| PDC-4823 | Antwerp | Delayed | 24 |
| PDC-4824 | Gdansk | In transit | 11 |
Props
import { DataTable, type DataTableColumn } from "@/components/ui/data-table"
type Shipment = { id: string; destination: string; status: string; pallets: number }
const shipments: Shipment[] = [
{ id: "PDC-4821", destination: "Rotterdam", status: "In transit", pallets: 18 },
{ id: "PDC-4822", destination: "Hamburg", status: "Delivered", pallets: 6 },
{ id: "PDC-4823", destination: "Antwerp", status: "Delayed", pallets: 24 },
{ id: "PDC-4824", destination: "Gdansk", status: "In transit", pallets: 11 },
]
const columns: DataTableColumn<Shipment>[] = [
{ accessorKey: "id", header: "Shipment" },
{ accessorKey: "destination", header: "Destination" },
{ accessorKey: "status", header: "Status" },
{ accessorKey: "pallets", header: "Pallets" },
]
<DataTable
columns={columns}
data={shipments}
filterPlaceholder="Filter shipments"
emptyMessage="No shipments match this filter."
/>Overview
Data table takes an array of records and a list of column definitions and renders them with the Table parts. It adds click-to-sort headers, a text filter across all columns and an empty state. Row logic comes from TanStack Table.
When to use
- For lists of records that people need to sort or search, such as shipments, users or invoices.
- When the same table layout is fed from data rather than written out row by row.
- When cells need custom rendering per column, such as a status color or a formatted number.
When not to use
- For a few static rows with no sorting or filtering: use Table.
- For thousands of rows loaded at once: sort and filter on the server and add Pagination, or virtualize the rows.
- For spreadsheet-style cell editing: this component is read-only.
Anatomy
Toolbar (optional)
A filter input on the left and a slot for actions on the right. Shown when filterable or toolbar is set.
Header
Column titles on a muted background. Sortable columns render the title as a button with a direction icon.
Rows
One TableRow per record, rendered through each column’s cell function.
Empty row
A single centered cell with emptyMessage when there is no data or nothing matches.
Props
The contract every language implements. Names follow each language’s conventions (camelCase in React, snake_case in Rust).
| Prop | Type | Default | Description |
|---|---|---|---|
| sortable | boolean | true | Makes column headers clickable to sort. Opt a single column out with enableSorting: false in its definition. |
| filterable | boolean | false | Shows a text input that filters rows by matching any column. |
| filterPlaceholder | string | Filter shipments | Placeholder and accessible name of the filter input. |
| emptyMessage | string | No shipments match this filter. | Shown in place of rows when there is nothing to display. |
| columns | node | – | Array of TanStack Table column definitions (DataTableColumn<T>): accessorKey, header and an optional cell renderer. |
| data | node | – | Array of records. Keep the reference stable between renders. |
| defaultSorting | node | – | Initial sort, such as [{ id: "pallets", desc: true }]. |
| toolbar | node | – | Content shown to the right of the filter, such as an Export button. |
States
- Unsorted
- Sortable headers show a muted up-down icon.
- Sorted ascending / descending
- The active header shows an up or down arrow and sets aria-sort.
- Filtered
- Only rows containing the typed text remain.
- Empty
- The body shows emptyMessage in the muted color.
Accessibility
Role: table
| Keys | Action |
|---|---|
| Tab | Moves through the filter input, then each sortable column header, then any controls inside cells. |
| Enter/Space | On a column header, cycles the sort: ascending, descending, then unsorted. Text columns start ascending, number columns descending. |
| Shift+Enter | On a column header, adds that column as a secondary sort. |
- Sorted headers set aria-sort to ascending or descending so the order is announced, and the sort control is a real button inside the th.
- The filter input uses filterPlaceholder as its aria-label; keep it descriptive, such as “Filter shipments”.
- Sort direction is shown with an icon, not color.
- Rows are not focusable. Put buttons or links in cells for row actions.
Design tokens
Change any of these in Foundations and this component follows, in every language.
Platform notes
- HTML / JS
- Planned
- Not built in HTML / JS yet.
- React
- Stable
- Matches the contract with no platform differences.
- React Native
- Not available
- Wide sortable grids do not fit phone screens. Use a FlatList of rows or cards with a search field and a sort menu instead.
- Swift (SwiftUI)
- Planned
- Not built in Swift yet.
- Godot
- Planned
- Not built in Godot yet.
- Vue
- Draft
- Usage snippet only; same props and behavior as the React component.
- Svelte
- Draft
- Usage snippet only; same props and behavior as the React component.
- Solid
- Draft
- Usage snippet only; same props and behavior as the React component.