Table
A sortable data table with custom cell rendering.
Demo
Usage
import { Table, Badge, Text, type Column, type Sort } from '@tuttn/slant'
const columns: Column<Row>[] = [
{ key: 'component', label: 'Component' },
{ key: 'state', label: 'State', render: row => <Badge>{row.state}</Badge>, compare: (a, b) => order[a.state] - order[b.state] },
{ key: 'adds', label: 'Adds', numeric: true },
]
// Uncontrolled: the table keeps its own sort, starting from defaultSort.
<Table columns={columns} rows={rows} rowKey={row => row.component}
caption="Most used components" defaultSort={{ key: 'adds', direction: 'descending' }} />
// Controlled: you hold the sort, say to keep it in the URL.
const [sort, setSort] = useState<Sort>({ key: 'adds', direction: 'descending' })
<Table columns={columns} rows={rows} rowKey={row => row.component} sort={sort} onSortChange={setSort} />
// Selectable rows, uncontrolled: a leading checkbox column, and a plain form
// submits the checked rows' keys under "components".
<form>
<Table columns={columns} rows={rows} rowKey={row => row.component} selectable name="components" />
</form>
// Controlled: you hold the keys, say for a bar of bulk actions above the table.
const [selection, setSelection] = useState<string[]>([])
<Table columns={columns} rows={rows} rowKey={row => row.component}
selectable selection={selection} onSelectionChange={setSelection}
rowLabel={row => row.component} isRowSelectable={row => row.state !== 'Alpha'} />
// Expandable rows, uncontrolled: a toggle on each row opens a detail row under it.
<Table columns={columns} rows={rows} rowKey={row => row.component}
renderExpanded={row => <Text>{row.summary}</Text>} defaultExpanded={['Button']} />
// Controlled, and only rows with a summary can open.
const [open, setOpen] = useState<string[]>([])
<Table columns={columns} rows={rows} rowKey={row => row.component}
renderExpanded={row => <Summary of={row} />} expanded={open} onExpandedChange={setOpen}
isRowExpandable={row => Boolean(row.summary)} />
// Group bands: one band per state, Stable first; each band sorts by itself.
<Table columns={columns} rows={rows} rowKey={row => row.component}
groupBy={row => row.state} groups={['Stable', 'Beta']}
renderGroup={(state, rows) => <>{state} · {rows.length}</>} />
// Records on narrow screens: below 36rem of the table's own width, each row
// stacks its cells as label and value pairs.
<Table columns={columns} rows={rows} rowKey={row => row.component} layout="stack" selectable />Props
| Prop | Type | Default | Description |
|---|---|---|---|
columns | Column<Row>[] | — | The columns, in order: `{ key, label, numeric?, sortable?, render?, compare? }`. Numbers sort numerically and anything else as text by locale, unless `compare` is given. |
rows | Row[] | — | The data, one row each. Sorting copies it and never mutates it. |
rowKey | (row: Row) => string | — | A stable, unique key for each row. |
caption | string | — | A caption read to screen readers only. |
sort | Sort | — | The sort, controlled. Omit it and the table sorts itself, starting from `defaultSort`. |
defaultSort | Sort | — | The starting sort when uncontrolled; unsorted when omitted. |
onSortChange | (sort: Sort) => void | — | Called with the sort a header click asks for: a new column, or the same one flipped. |
selectable | boolean | false | Adds a leading column of checkboxes, with a "select all" checkbox in its header. |
selection | string[] | — | The selected rows' keys (`rowKey` values), controlled. They come in the rows' given order, whatever the sort; keys of rows not in `rows` (another page, say) are kept, first. |
defaultSelection | string[] | [] | The starting selection when uncontrolled. |
onSelectionChange | (keys: string[]) => void | — | Called with every selection a checkbox asks for, in the same order as `selection`. |
rowLabel | (row: Row) => string | the first column's value, as text | Names a row for its checkbox, read as `messages.selectRow(name)`, and for its details toggle, read as `messages.rowDetails(name)`. |
name | string | — | Gives every row checkbox this `name` and its row's key as `value`, so a plain form submits the selection. |
isRowSelectable | (row: Row) => boolean | every row can | Whether a row can be selected; one that can't gets a disabled checkbox. |
layout | 'table' | 'stack' | 'table' | `stack` turns each row into a labelled record once the wrapper is narrower than 36rem: the cells stack as label and value pairs, the checkbox and the details toggle sit at the record's top, and the header becomes a strip of sort buttons. Wider, it is the same table. Either way the table keeps its roles. |
renderExpanded | (row: Row) => ReactNode | — | Makes rows expandable: a leading toggle opens a detail row under the row, one cell across every column, holding what this returns for it. |
expanded | string[] | — | The open rows' keys, controlled. They come in the rows' given order, whatever the sort; keys of rows not in `rows` are kept, first. |
defaultExpanded | string[] | [] | The rows open at first when uncontrolled. |
onExpandedChange | (keys: string[]) => void | — | Called with every set of open rows a toggle asks for, in the same order as `expanded`. |
isRowExpandable | (row: Row) => boolean | every row can, with `renderExpanded` | Whether a row can open; one that can't gets no toggle, and its key in `expanded` shows nothing. |
groupBy | (row: Row) => string | — | Bands the rows: each row goes in the band this names, its own row group under a heading row. Sorting sorts within each band, and the bands keep their order. |
groups | readonly string[] | — | The bands' order. Bands it doesn't list follow, in the order their first row appears in `rows`. A band with no rows doesn't show. |
renderGroup | (group: string, rows: Row[]) => ReactNode | the band's name and its row count, `messages.groupRows(count)` | The band heading's content. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the scrolling wrapper, which is also the stacked layout's container. |
Keyboard
| Tab / Shift+Tab | moves through the sort buttons, then each row's checkbox and details toggle, and anything focusable in an open detail row, in reading order |
| Enter / Space | on a sort button, sorts by that column, or flips its direction; on a details toggle, opens or closes the row's details |
| Space | on a checkbox, selects or clears its row; on the header's, every selectable row |
| Shift+click | on a checkbox, selects or clears every row from the last one clicked, in the order the table shows, across bands |
Accessibility
- A real
<table>with a caption read to screen readers only. Sortable headers hold a button, and the header cell carriesaria-sort; each change of sort is announced politely. - Each row's checkbox is named
messages.selectRow(name)and its details togglemessages.rowDetails(name), both fromrowLabelor the first column's value. A selected row hasaria-selected="true". - The details toggle is a button with
aria-expanded. While the row is open it points at the detail row witharia-controls; the detail row is only rendered while open, so a closed toggle has noaria-controls. The header cell over the toggles is a plaintd, since an emptythwould be a header that names nothing. - Each band is its own row group (
tbody) whose first row is ath scope="rowgroup"across every column, so the band's name and count head the rows under it. - With
layout="stack", every element carries its table role explicitly (table,rowgroup,row,columnheader,rowheader,cell), because a CSS display change drops a table's semantics in some browsers, and the caption names the table througharia-labelledby. The labels inside each record arearia-hidden: screen readers already hear the column header for each cell. The headers that don't sort are visually hidden in the strip, not removed.
Sorting
Sortable headers are buttons with aria-sort on the cell. The first click sorts numeric columns descending and text columns ascending; clicking again flips the direction. Each change of sort is announced through the root's live region, for example “Adds, sorted descending”; the words come from the sortAscending and sortDescending messages.
Selection
With selectable, each row starts with a native checkbox, so Tab reaches it and Space toggles it. It is named “Select …” (messages.selectRow) after rowLabel, or the first column's value. The header's checkbox (messages.selectAllRows) is checked when every selectable row is, mixed when some are, and selects or clears them all. Shift-click selects or clears every row from the last one clicked, in the order the table shows. Rows that isRowSelectable turns down get a disabled checkbox.
The selection is a list of row keys, in the rows' given order whatever the sort, so it survives sorting; keys of rows the table isn't showing are kept. A selected row has data-selected and aria-selected="true", and an accent-soft background, with its text at --sl-text. Text in a tone colour (a success or warning Badge) loses contrast on that background in Paper, so put a status's colour in a Pixel, as the demo does. A count or a bar of bulk actions is yours to draw, from onSelectionChange.
Expandable rows
renderExpanded adds a leading column of toggles, after the checkboxes. A toggle opens a detail row right under its row, one cell across every column, holding what renderExpanded returns; it renders only while open, so a closed row costs nothing. The open rows are keys, like the selection: expanded, defaultExpanded and onExpandedChange, in the rows' given order, and they survive sorting. Rows that isRowExpandable turns down get no toggle. An open row has data-expanded, and its chevron turns to point down.
Group bands
groupBy puts each row in a band, its own tbody under a heading row with the band's name and messages.groupRows(count), or what renderGroup draws. The bands come in groups order, then any others in the order their first row appears in rows; a band with no rows doesn't show. Sorting sorts inside each band and leaves the bands in place. Select all and shift-click ranges run across bands, in the order the rows show. Bands don't collapse; a band you want to hide is a filter of your rows.
Narrow screens
By default the table scrolls sideways inside its wrapper on narrow screens. With layout="stack", the wrapper is a size container, and once it is narrower than 36rem each row becomes a record: the checkbox and the details toggle on its top line, then each cell as its column's label beside its value. The header row becomes a wrapping strip of the sort buttons, with the select-all checkbox, so the records still sort. Wider than that, nothing changes. The query is on the table's own width, not the screen's, so a table in a narrow pane stacks too. As a size container the wrapper takes its width from its parent, as a block does: inside a shrink-to-fit parent (an inline-flex row, say) give it a width.