Components

Data Grid

Pronew

A sortable, searchable, selectable and paginated table on TanStack Table, with the toolbar and footer already built.

Author
fix: focus ring in dark modemain
Ready0m 38sOct 12, 18:00DK
feat: add RTL support to sidebarfeat/rtl
Ready1m 15sOct 12, 17:13MS
docs: theming guidedocs/theming
Canceled1m 52sOct 12, 16:26AK
chore: bump radix-uimain
Building2m 29sOct 12, 15:39JL
feat: data grid density togglefeat/density
Ready0m 46sOct 12, 14:52DK
fix: combobox keeps selection on blurfix/combobox
Ready1m 23sOct 12, 14:05MS

36 rows

Page 1 of 6

Installation

npx shadcn@latest add @blank-pro/data-grid

Needs a Blank Pro licence key and the @blank-pro registry. Set up Pro access

Usage

import {
  createDataGridColumnHelper,
  DataGrid,
  DataGridColumnHeader,
  DataGridPagination,
  DataGridSearch,
  DataGridSelectAll,
  DataGridSelectRow,
  DataGridTable,
  DataGridToolbar,
  DataGridViewOptions,
  useDataGrid,
} from "@/components/ui/data-grid"
const column = createDataGridColumnHelper<Deployment>()

const columns = column.columns([
  column.display({
    id: "select",
    header: ({ table }) => <DataGridSelectAll table={table} />,
    cell: ({ row }) => <DataGridSelectRow row={row} />,
    enableSorting: false,
    enableHiding: false,
  }),
  column.accessor("commit", {
    header: ({ column }) => <DataGridColumnHeader column={column} title="Commit" />,
  }),
  column.accessor("duration", {
    header: ({ column }) => <DataGridColumnHeader column={column} title="Duration" />,
    meta: { align: "end" },
  }),
])

export function Deployments({ data }: { data: Deployment[] }) {
  const table = useDataGrid({ data, columns, getRowId: (row) => row.id })

  return (
    <DataGrid table={table}>
      <DataGridToolbar>
        <DataGridSearch />
        <DataGridViewOptions className="ml-auto" />
      </DataGridToolbar>
      <DataGridTable />
      <DataGridPagination />
    </DataGrid>
  )
}

useDataGrid is TanStack Table v9's useTable with the features a grid needs already registered: sorting, a global filter, column visibility, row selection and pagination. Everything else — initialState, controlled state with its on…Change callback, manualPagination for server data — is passed straight through. Define columns at module scope or memoise them, along with data; new arrays every render re-run every row model.

DataGrid puts the table in context, so each part is a single element and you arrange them as you like. The markup is Blank's Table.

Column headers are menus: sort ascending or descending, clear the sort, or hide the column, with aria-sort on the header cell. The search waits 150ms after typing before filtering, and Escape clears it; a search with no matches says so and offers to clear it. Shift-click on a row checkbox selects the range from the last row you clicked.

Column meta takes align ("start", "center" or "end"), a label for the column menu when the header is not a string, and headerClassName and cellClassName.

Examples

Loading and empty

loading swaps the rows for skeletons and disables paging. empty on DataGridTable replaces the message shown when there are no rows at all.

3 rows

Page 1 of 1

API Reference

useDataGrid

Takes TanStack useTable options except features; returns the table. globalFilterFn defaults to "includesString" and the page size to 10.

createDataGridColumnHelper

createDataGridColumnHelper<TData>() returns a column helper typed for the grid's features, including its column meta.

DataGrid

div props, plus:

PropTypeDefault
tablethe result of useDataGrid—
loadingbooleanfalse
density"compact" | "default" | "comfortable"—
defaultDensity"compact" | "default" | "comfortable""default"
onDensityChange(density) => void—

DataGridToolbar

div props, with role="toolbar".

DataGridSearch

InputGroupInput props, plus:

PropTypeDefault
debouncenumber (ms)150

DataGridViewOptions

Button props. Lists every column that can be hidden.

DataGridDensityToggle

ToggleGroup props, without its value props — density lives on DataGrid.

DataGridColumnHeader

PropTypeDefault
columnColumn—
titlestring—

DataGridSelectAll, DataGridSelectRow

Checkboxes for a select column. DataGridSelectAll takes the header's table; DataGridSelectRow takes the cell's row.

DataGridTable

Table props, plus:

PropTypeDefault
emptyReactNode"No rows yet."
skeletonRowsnumberthe page size, up to 10

DataGridPagination

div props, plus:

PropTypeDefault
pageSizesnumber[][10, 20, 50]