# Table

Records people scan, compare, sort and act on: sortable columns, row selection by checkbox, a row that opens its record.

- Family: collection
- Import: `import { Table } from "@senvenli/react/collection";`
- Page: https://design.senvenli.com/docs/components/table
- JSON: https://design.senvenli.com/docs/components/table.json

## Example: Orders

Columns as data: money aligned to the end, a status as a lozenge, two lines in one cell.

```tsx
import { Table } from "@senvenli/react/collection";
import { Badge, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";

const ORDERS = [
  { id: "SO-1042", customer: "Ada Lovelace", email: "ada@example.com", date: "Mar 18, 2026", total: 248.5, isPaid: true },
  { id: "SO-1043", customer: "Grace Hopper", email: "grace@example.com", date: "Mar 18, 2026", total: 96, isPaid: false },
  { id: "SO-1044", customer: "Alan Turing", email: "alan@example.com", date: "Mar 17, 2026", total: 1290, isPaid: true },
  { id: "SO-1045", customer: "Katherine Johnson", email: "katherine@example.com", date: "Mar 16, 2026", total: 54.9, isPaid: true },
];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD" });

export default function Example() {
  return (
    <Table
      aria-label="Orders"
      rows={ORDERS}
      columns={[
        { key: "id", header: "Order", isRowHeader: true },
        { key: "customer", header: "Customer", render: (order) => <Stack space="space-0"><Text truncate>{order.customer}</Text><Text size="body-small" tone="subtle" truncate><bdi>{order.email}</bdi></Text></Stack> },
        { key: "date", header: "Date", hideBelow: "sm" },
        { key: "isPaid", header: "Payment", render: (order) => (order.isPaid ? <Badge tone="success">Paid</Badge> : <Badge tone="warning">Pending</Badge>) },
        { key: "total", header: "Total", align: "end", render: (order) => usd.format(order.total) },
      ]}
    />
  );
}
```

## Examples

### Row selection

A checkbox per row and one in the header for the whole table.

```tsx
import { useState } from "react";
import { Table, type TableProps } from "@senvenli/react/collection";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";

type Product = { id: string; name: string; stock: number; price: number };
const PRODUCTS: Product[] = [
  { id: "chair", name: "Ergonomic chair", stock: 42, price: 349 },
  { id: "desk", name: "Standing desk, oak", stock: 17, price: 589 },
  { id: "arm", name: "Monitor arm, dual", stock: 63, price: 129 },
  { id: "lamp", name: "Desk lamp", stock: 0, price: 79 },
  { id: "tray", name: "Cable tray", stock: 118, price: 38 },
];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", maximumFractionDigits: 0 });

// "all" once the header checkbox is pressed, a set of row ids otherwise.
type Keys = NonNullable<TableProps<Product>["selectedKeys"]>;

export default function Example() {
  const [selected, setSelected] = useState<Keys>(new Set(["desk", "arm"]));
  const count = selected === "all" ? PRODUCTS.length : [...selected].length;
  return (
    <Stack space="space-150">
      <Table
        aria-label="Products"
        rows={PRODUCTS}
        selectionMode="multiple"
        selectedKeys={selected}
        onSelectionChange={setSelected}
        columns={[
          { key: "name", header: "Product" },
          { key: "stock", header: "In stock", align: "end" },
          { key: "price", header: "Price", align: "end", render: (product) => usd.format(product.price) },
        ]}
      />
      <Text size="body-small" tone="subtle">{count} of {PRODUCTS.length} products selected</Text>
    </Stack>
  );
}
```

### Sortable columns

The table reports the sort; you sort the rows, here or on the server.

```tsx
import { useState } from "react";
import { Table, type TableProps } from "@senvenli/react/collection";

type Customer = { id: string; name: string; orders: number; spent: number };
const CUSTOMERS: Customer[] = [
  { id: "1", name: "Ada Lovelace", orders: 42, spent: 13020 },
  { id: "2", name: "Grace Hopper", orders: 18, spent: 11520 },
  { id: "3", name: "Alan Turing", orders: 64, spent: 6080 },
  { id: "4", name: "Katherine Johnson", orders: 27, spent: 4860 },
  { id: "5", name: "Margaret Hamilton", orders: 9, spent: 3780 },
];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", maximumFractionDigits: 0 });

type Sort = NonNullable<TableProps<Customer>["sortDescriptor"]>;

export default function Example() {
  const [sort, setSort] = useState<Sort>({ column: "spent", direction: "descending" });
  const key = sort.column as keyof Customer;
  const sign = sort.direction === "descending" ? -1 : 1;
  const rows = [...CUSTOMERS].sort((a, b) => (a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : 0) * sign);
  return (
    <Table
      aria-label="Customers"
      density="compact"
      rows={rows}
      sortDescriptor={sort}
      onSortChange={setSort}
      columns={[
        { key: "name", header: "Customer", isSortable: true },
        { key: "orders", header: "Orders", align: "end", isSortable: true },
        { key: "spent", header: "Total spent", align: "end", isSortable: true, render: (customer) => usd.format(customer.spent) },
      ]}
    />
  );
}
```

## Guidelines

### When to use it

- Records with several comparable fields on screens 640px and wider.

### Use something else when

- [data-table](https://design.senvenli.com/docs/components/data-table.md): The table also needs search, filters, paging or a column picker.
- [grid-list](https://design.senvenli.com/docs/components/grid-list.md): The screen is narrow or each record is one line of text.

## Accessibility

Keyboard:

- Arrow keys: Move between cells and rows.
- Space: Selects the focused row.
- Enter: Opens the focused row's record.

Built in:

- The row header column names each row for screen readers.
- Sort state is announced on the column header.

You provide:

- An aria-label for the table.

## API reference

### Table

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| aria-label | `string` |  | yes | Names the table ("Orders"). |
| columns | `readonly TableColumn<T>[]` |  | yes |  |
| rows | `readonly T[]` |  | yes |  |
| rowKey | `(row: T) => Key` |  | no | The row's stable id; `row.id` by default. Ids must be unique within the table. When the data has no single unique field, build one from the fields that are (`${row.date}:${row.channel}`), or add the row's index as a last resort. A repeated id still renders every row: the first keeps its id and the later ones are keyed `a#2`, `a#3`, … in row order, and selection and `onRowAction` receive those suffixed keys; a development warning names the repeated ids. A suffixed key follows the row's position, not the row: after a sort or filter, a selection made through it can move to another copy of the id. |
| selectionMode | `"none" \| "single" \| "multiple"` |  | no |  |
| selectedKeys | `"all" \| Iterable<Key>` |  | no |  |
| defaultSelectedKeys | `"all" \| Iterable<Key>` |  | no |  |
| onSelectionChange | `(keys: Selection) => void` |  | no |  |
| sortDescriptor | `SortDescriptor` |  | no |  |
| onSortChange | `(descriptor: SortDescriptor) => void` |  | no |  |
| density | `Density` |  | no | `compact` (36px rows) for dense working screens; `default` (48px) for public and content pages. |
| emptyState | `React.ReactNode` |  | no | Shown when there are no rows: an `EmptyState`, or one sentence on why it is empty and the action that fills it. |
| onRowAction | `(key: Key) => void` |  | no | Opens the record: the whole row is the target. |
| maxHeight | `string` |  | no | A height for the scroll area: the header stays put and gets a shadow once rows scroll under it. |

### Tokens it reads

`--ds-background-selected`, `--ds-border`, `--ds-border-focused`, `--ds-border-selected`, `--ds-radius-large`, `--ds-radius-medium`, `--ds-shadow-overflow`, `--ds-shadow-overlay`, `--ds-shadow-raised`, `--ds-size-row`, `--ds-size-row-compact`, `--ds-space-050`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-space-500`, `--ds-surface-hovered`, `--ds-surface-overlay`, `--ds-surface-raised`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-subtle`, `--ds-text-subtlest`

## Related

- [data-table](https://design.senvenli.com/docs/components/data-table.md)
- [grid-list](https://design.senvenli.com/docs/components/grid-list.md)
