# DataGrid

A table with sorting, paging, row selection and column toggles.

- **Use it for**: Large tables with controlled sorting, paging, selection and column visibility.
- **Not for**: A few read-only rows. Try [Table](https://unseenui.com/components/table/).

## Installation

```bash
npm install @stackunseen/ui
```

## Usage

```tsx
import { DataGrid } from '@stackunseen/ui/application';
import '@stackunseen/ui/styles.css';
import '@stackunseen/ui/application.css';

<DataGrid caption="Customers" columns={columns} rows={pageRows} getRowId={row => row.id}
  sort={sort} onSortChange={setSort} pagination={pagination} onPageChange={setPage}
  selectedRowIds={selectedIds} onSelectionChange={setSelectedIds} />
```

## Props

- `caption` (`string`): The native table caption and scroll-region name.
- `captionVisibility` (`'visible' | 'hidden' (optional)`): Hide the caption on screen when a surrounding card already shows the same heading; screen readers still announce it.
- `columns` (`readonly DataGridColumn<Row>[]`): Stable column IDs, headers and trusted React cell renderers. Use rowHeader for the record name.
- `rows` (`readonly Row[]`): The current page of rows, already filtered, sorted and paged by your app. The grid never fetches or changes them.
- `getRowId` (`(row: Row) => string`): A unique, nonblank ID used for selection and focus recovery.
- `sort` (`DataGridSort | null (optional)`): Current column and direction. onSortChange cycles ascending, descending and unsorted.
- `pagination` (`DataGridPagination (optional)`): Zero-based pageIndex, pageSize and totalRows. For an unknown total, supply hasNextPage.
- `selectedRowIds` (`readonly string[] (optional)`): Selected IDs across pages. The header checkbox changes only selectable rows on the current page.
- `onSelectionChange` (`(ids: readonly string[]) => void (optional)`): Requests the full updated selection, preserving IDs outside this page.
- `columnVisibility` (`Readonly<Record<string, boolean>> (optional)`): Visible columns by ID. onColumnVisibilityChange requests the updated map.
- `filter` (`DataGridFilter (optional)`): Controlled value, accessible label and onValueChange callback for search.

## All props

| Prop | Type | Description |
| --- | --- | --- |
| `caption` (required) | `string` |  |
| `captionVisibility` | `"hidden" \| "visible"` | `hidden` keeps the caption for assistive technology but not on screen. |
| `columns` (required) | `readonly DataGridColumn<Row>[]` |  |
| `columnVisibility` | `Readonly<Record<string, boolean>>` | Missing keys are visible. |
| `dir` | `"ltr" \| "rtl"` |  |
| `disabled` | `boolean` |  |
| `emptyMessage` | `string` |  |
| `error` | `string` |  |
| `filter` | `DataGridFilter` |  |
| `getRowId` (required) | `(row: Row) => string` |  |
| `getRowLabel` | `((row: Row) => string)` | Human-readable name for each row selection checkbox; defaults to its stable ID. |
| `isRowSelectable` | `((row: Row) => boolean)` |  |
| `loading` | `boolean` |  |
| `onColumnVisibilityChange` | `((visibility: Readonly<Record<string, boolean>>) => void)` |  |
| `onPageChange` | `((pageIndex: number) => void)` |  |
| `onSelectionChange` | `((selectedRowIds: readonly string[]) => void)` |  |
| `onSortChange` | `((sort: DataGridSort \| null) => void)` | Cycles ascending, descending, then null. |
| `pagination` | `DataGridPagination` |  |
| `rows` (required) | `readonly Row[]` | An application-owned page/window. |
| `selectedRowIds` | `readonly string[]` | When present, shows controlled selection; IDs outside this page are preserved. |
| `sort` | `DataGridSort \| null` |  |
| `toolbar` | `ReactNode` | Trusted application controls, such as actions on selected rows or retry. |

## Keyboard

Tab reaches sort buttons, selection, column controls and paging. Wide tables scroll within their labeled region.

## Works with

[GeneratedUI](https://unseenui.com/components/generated-ui/), [AIForm](https://unseenui.com/components/ai-form/), [Chart](https://unseenui.com/components/chart/), [Table](https://unseenui.com/components/table/), [Pagination](https://unseenui.com/components/pagination/), [Matrix](https://unseenui.com/components/matrix/), [StatusBadge](https://unseenui.com/components/status-badge/), [Card](https://unseenui.com/components/card/), [Tag](https://unseenui.com/components/tag/), [DiffStat](https://unseenui.com/components/diff-stat/), [ElapsedTime](https://unseenui.com/components/elapsed-time/), [Numeric](https://unseenui.com/components/numeric/)

Page: https://unseenui.com/components/data-grid/
