compacto-ui

Search documentation

Jump to a page or a component.

DataTable

Sortable, row-virtualized table — 5,000 rows here, ~20 in the DOM.

peer: @tanstack/react-table, @tanstack/react-virtual

Preview

DELETE/exports/9305001911 ms
DELETE/exports/102011911 ms
DELETE/exports/875001911 ms
PUT/webhooks/7862001910 ms
PUT/webhooks/8634041910 ms
PUT/webhooks/9402001910 ms
POST/tokens/refresh/7192001909 ms
POST/tokens/refresh/7964001909 ms
POST/tokens/refresh/8732001909 ms
GET/invoices/5752041908 ms
GET/invoices/6522001908 ms
GET/invoices/7292041908 ms
DELETE/users/5082011907 ms
DELETE/users/5855001907 ms
PUT/exports/3644041906 ms
PUT/exports/4412001906 ms
PUT/exports/5184041906 ms

Installation

No install — ask an AI agent to copy this component from registry.json, then pnpm add @tanstack/react-table, @tanstack/react-virtual.

import { DataTable, dataTableColumnHelper, DataTableColumn, DataTableFeatures } from "@/components/ui/data-table";

Notes

  • Headless core: @tanstack/react-table v9 produces the header groups and the sorted row model; this component owns markup, --app-* styling, the sticky header, sort affordances and aria-sort.
  • Rows are virtualized with @tanstack/react-virtual over the *sorted* row model, positioned by two spacer <tr>s so the table keeps native column layout. Only rows in view (plus overscan) are in the DOM.
  • Virtualization needs a bounded scroll box — that is why height exists. Override with a className like h-full inside a parent that already constrains height.
  • Build columns with dataTableColumnHelper<Row>() at module scope. v9 types a column def against the registered feature set, so a def from a bare createColumnHelper() will not type-check here.
  • Keep columns and data referentially stable (module constant, state, or a query result). A fresh array per render rebuilds the row model every render.
  • Sorting is internal after defaultSorting; a header click cycles asc → desc → off. enableSorting: false on a column removes its affordance.
  • Derives ${testId}-header-<columnId> per header and ${testId}-row-<rowId> per rendered row from data-testid — only rows in view exist to be found.

Props

The props worth knowing. Everything else passes straight through to the underlying element, including className, which is merged last so it always wins.

PropTypeDefaultDescription
columnsDataTableColumn<Row>[]—From dataTableColumnHelper<Row>().columns([...]). `size` is width in px; `minSize` / `maxSize` bound a drag; `enableResizing: false` removes a handle.
dataRow[]—The rows. Stable reference.
getRowId(row: Row, index: number) => stringrow indexStable id per row — React key, virtual measurement, derived test id.
heightnumber400Scroll box height in px.
rowHeightnumber36Estimated row height in px; rows are measured after mount.
defaultSortingSortingState—Initial sort, e.g. [{ id: "ms", desc: true }].
resizablebooleantrueShow resize handles on every column that allows it.
defaultColumnSizingRecord<string, number>—Initial widths by column id, over each column's size.
onColumnSizingChange(sizing: Record<string, number>) => void—Full widths-by-id map after every drag tick.
onRowClick(row: Row) => void—Makes rows clickable; called with the row's original datum.
emptyLabelstring"No rows"Copy shown in place of rows when data is empty.