# components/Table (zidane-ui)

Import: `import { … } from 'zidane-ui'`. Source: [ui/src/components/Table.tsx](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx).

## Components

### `Table`

Component.

Renders rows under a header, sortable by the columns that allow it. With
`search`, filterable columns, `pageSize` or `detail` it also finds, narrows,
pages and opens rows.

```ts
function Table<Row>(props: TableProps<Row>): Element
```

**Type parameters**

- `Row`

**Parameters**

- `props` (`TableProps<Row>`): [`TableProps`](https://zidane.si/docs/reference/ui-components-table.md#TableProps).

**Returns** `Element`

**Props**

- `caption?` (`ReactNode`): A visible caption above the table: the part's title.
- `className?` (`string`): Extra classes on the root element.
- `columns` (`readonly TableColumn<Row>[]`): The columns, in order.
- `defaultSort?` (`TableSort | null`): The first order. Default: the rows as given.
- `density?` (`'default' | 'compact'`, default `'default'`): `compact` sets cells in the metadata size with no row lines, for a dialog's facts.
- `detail?` (`(row: Row) => ReactNode`): A row's detail. A row with one gets a caret that opens it under the row; `null` for none.
- `filterBar?` (`TableFilterBar`): A row of buttons above the table, "All (36)", "EU (9)", that keeps the rows of one value of a column.
- `framed?` (`boolean`, default `true`): Set in a tray. Off inside a surface that is already a tray.
- `highlight?` (`(row: Row) => boolean`): Rows to emphasize: an accent wash behind them, wherever the sort puts them.
- `label?` (`string`): Accessible name of the table (and of its scroll region).
- `maxHeight?` (`string | number`): Rows scroll past this height (a CSS length or pixels) under a header that stays put.
- `note?` (`ReactNode`): A key line under the caption, in the caption size's smaller muted text: what a color in the cells means.
- `onSortChange?` (`(sort: TableSort) => void`): Called with the next order when a sortable header is clicked.
- `pageSize?` (`number`): Rows on one page. More rows (after search and filters) get Previous and Next under the table,
  and the `totals` move to a line above the table.
- `rowEnd?` (`(row: Row) => ReactNode`): Controls at the end of each row (a row's actions), in a last column of `rowEndWidth` px that keeps its width
  whatever it holds, so nothing moves when they change. Give controls that only show on hover or focus the
  `zg-reveal` class: the row is their group.
- `rowEndWidth?` (`number`): The width of the `rowEnd` column, in px. Default: 144.
- `rowKey?` (`(row: Row, index: number) => string | number`): A row's stable key. Default: its index.
- `rowMenu?` (`(row: Row) => readonly RowActionItem[]`): The rows of a row's right-click menu. Absent or empty: the browser's own menu.
- `rows` (`readonly Row[]`): The rows, in their first order.
- `search?` (`boolean`): A search field above the table keeps the rows whose raw cell text holds the query (any case).
- `sort?` (`TableSort | null`): The order, controlled. Use it with `onSortChange`.
- `style?` (`CSSProperties`): Inline styles on the root element.
- `toolbarLead?` (`ReactNode`): Controls at the start of the toolbar line, before the filter bar, the search and the filters:
  the line wraps only when it has no room.
- `totals?` (`(rows: readonly Row[]) => Partial<Record<string, ReactNode>> | null`): A last row, by column id, from the rows after search and filters (every page). The first
  column shows the "Total" label when it has no total of its own. With more than one page the
  totals show as a line above the table instead. `null`: no row.
- `totalsAbove?` (`boolean`): Show the `totals` as figures above the table (the rows shown, then each total) even on one
  page: for a table the reader filters, so the figures follow the filter at a glance.

Source: [ui/src/components/Table.tsx:215](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L215)

## Functions

### `compareCells`

Function.

Compares two cell values for sorting: numbers as numbers, text in natural order, empty cells last.

```ts
function compareCells(a: TableCellValue, b: TableCellValue): number
```

**Parameters**

- `a` (`TableCellValue`): The first value.
- `b` (`TableCellValue`): The second value.

**Returns** `number`

Source: [ui/src/components/Table.tsx:164](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L164)

## Interfaces

### `TableColumn`

Interface.

One column of a [`Table`](https://zidane.si/docs/reference/ui-components-table.md#Table).

```ts
interface TableColumn<Row>
```

**Type parameters**

- `Row`

**Properties**

- `filterable?` (`boolean`): A select above the table keeps the rows whose raw value is the one picked.
- `id` (`string`): Stable id. With no `value`, also the key (or index, for an array row) the cell reads.
- `label` (`string`): The header text.
- `mono?` (`boolean`): Ids, paths and codes: mono at the label size. Default: a text column whose every value
  looks like `ORD-1040` or starts with `/`.
- `numeric?` (`boolean`): Right-aligned, tabular figures, sorted as numbers.
- `render?` (`(row: Row) => ReactNode`): Draws the cell. Default: the raw value as text.
- `sortable?` (`boolean`): The header is a button that sorts by this column.
- `value?` (`(row: Row) => TableCellValue`): The cell's raw value. Default: `row[id]`.

Source: [ui/src/components/Table.tsx:27](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L27)

### `TableFilterBar`

Interface.

A [`Table`](https://zidane.si/docs/reference/ui-components-table.md#Table)'s filter bar: one button per value of a column, with its count.

```ts
interface TableFilterBar
```

**Properties**

- `column` (`string`): The column id. Its select filter is not offered.
- `onChange?` (`(value: string | null) => void`): Called with the value picked, `null` for every row.
- `value?` (`string | null`): The value shown, controlled: `null` (or a value not in the column) shows every row.
  Leave it out to let the table keep its own.

Source: [ui/src/components/Table.tsx:58](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L58)

### `TableProps`

Interface.

Props for [`Table`](https://zidane.si/docs/reference/ui-components-table.md#Table).

```ts
interface TableProps<Row>
```

**Type parameters**

- `Row`

**Properties**

- `caption?` (`ReactNode`): A visible caption above the table: the part's title.
- `className?` (`string`): Extra classes on the root element.
- `columns` (`readonly TableColumn<Row>[]`): The columns, in order.
- `defaultSort?` (`TableSort | null`): The first order. Default: the rows as given.
- `density?` (`'default' | 'compact'`, default `'default'`): `compact` sets cells in the metadata size with no row lines, for a dialog's facts.
- `detail?` (`(row: Row) => ReactNode`): A row's detail. A row with one gets a caret that opens it under the row; `null` for none.
- `filterBar?` (`TableFilterBar`): A row of buttons above the table, "All (36)", "EU (9)", that keeps the rows of one value of a column.
- `framed?` (`boolean`, default `true`): Set in a tray. Off inside a surface that is already a tray.
- `highlight?` (`(row: Row) => boolean`): Rows to emphasize: an accent wash behind them, wherever the sort puts them.
- `label?` (`string`): Accessible name of the table (and of its scroll region).
- `maxHeight?` (`string | number`): Rows scroll past this height (a CSS length or pixels) under a header that stays put.
- `note?` (`ReactNode`): A key line under the caption, in the caption size's smaller muted text: what a color in the cells means.
- `onSortChange?` (`(sort: TableSort) => void`): Called with the next order when a sortable header is clicked.
- `pageSize?` (`number`): Rows on one page. More rows (after search and filters) get Previous and Next under the table,
  and the `totals` move to a line above the table.
- `rowEnd?` (`(row: Row) => ReactNode`): Controls at the end of each row (a row's actions), in a last column of `rowEndWidth` px that keeps its width
  whatever it holds, so nothing moves when they change. Give controls that only show on hover or focus the
  `zg-reveal` class: the row is their group.
- `rowEndWidth?` (`number`): The width of the `rowEnd` column, in px. Default: 144.
- `rowKey?` (`(row: Row, index: number) => string | number`): A row's stable key. Default: its index.
- `rowMenu?` (`(row: Row) => readonly RowActionItem[]`): The rows of a row's right-click menu. Absent or empty: the browser's own menu.
- `rows` (`readonly Row[]`): The rows, in their first order.
- `search?` (`boolean`): A search field above the table keeps the rows whose raw cell text holds the query (any case).
- `sort?` (`TableSort | null`): The order, controlled. Use it with `onSortChange`.
- `style?` (`CSSProperties`): Inline styles on the root element.
- `toolbarLead?` (`ReactNode`): Controls at the start of the toolbar line, before the filter bar, the search and the filters:
  the line wraps only when it has no room.
- `totals?` (`(rows: readonly Row[]) => Partial<Record<string, ReactNode>> | null`): A last row, by column id, from the rows after search and filters (every page). The first
  column shows the "Total" label when it has no total of its own. With more than one page the
  totals show as a line above the table instead. `null`: no row.
- `totalsAbove?` (`boolean`): Show the `totals` as figures above the table (the rows shown, then each total) even on one
  page: for a table the reader filters, so the figures follow the filter at a glance.

Source: [ui/src/components/Table.tsx:71](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L71)

### `TableSort`

Interface.

The order of a [`Table`](https://zidane.si/docs/reference/ui-components-table.md#Table)'s rows.

```ts
interface TableSort
```

**Properties**

- `column` (`string`): The column id.
- `direction` (`'asc' | 'desc'`): `asc` or `desc`.

Source: [ui/src/components/Table.tsx:50](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L50)

## Types

### `TableCellValue`

Type.

A cell's raw value: what a column sorts by and shows when it has no `render`.

```ts
type TableCellValue = string | number | null | undefined
```

Source: [ui/src/components/Table.tsx:24](https://github.com/Tahul/zidane/blob/main/ui/src/components/Table.tsx#L24)
