# Table

Data tables in two styles: Workspace inside the opportunity and contact workspaces (36 px header, 40 px rows, 13 px, in a bordered shell), and Main for the contacts and opportunities lists (32 px uppercase header, 45 px rows, 14 px). Group headers band the rows under them.

- Beta: ready to use; its API can still change in a minor release.
- Category: Layout & surfaces

```tsx
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell, TableGroupHeader, TableGroupRows } from "@leap-crm/design-system";
```

## Best practices

- Do: Use `variant="workspace"` for a table inside the opportunity or contact workspace, and `main` for the full contacts and opportunities lists.
- Do: Group rows with `TableGroupHeader` and `TableGroupRows`, like documents by folder. Use `dotClassName` for a role colour, so the colour stays a small dot beside the name and count.
- Do: Wrap a long value, like a matter name or an email, in `Truncate` so the cell keeps its width and the full value shows on hover.
- Do: Show an empty cell with `EmptyValue`, never a blank or "N/A".
- Don't: Set `animated` on a long table that re-renders often. Every animated row is measured on each render.
- Don't: Tint a row's background in its status colour. Put the status in a `Badge` in its own cell, so colour stays an accent and the row stays readable.

## Keyboard

| Keys | What they do |
| --- | --- |
| Enter, Space | Open or close the focused group header. |

## Related

- [Workspace Table](./workspace-table.md)
- [List](./list.md)
- [Pagination](./pagination.md)
- [Scroll Area](./scroll-area.md)

## Examples

What the examples import:

```tsx
import { Button, Table, TableBody, TableCell, TableGroupHeader, TableGroupRows, TableHead, TableHeader, TableRow } from "@leap-crm/design-system";
import { Fragment, useState } from "react";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const contacts = [
  ["Jane Citizen", "Purchaser", "jane@citizen.com.au"],
  ["John Citizen", "Purchaser", "john@citizen.com.au"],
  ["Paddington Realty", "Agent", "sales@paddington.com.au"],
] as const;

const folders = [
  { name: "Pleadings", files: ["Statement of claim.pdf", "Defence.pdf"] },
  { name: "Correspondence", files: ["Letter to other side.docx"] },
] as const;

const parties = [
  ["Jane Citizen", "Purchaser"],
  ["John Citizen", "Purchaser"],
  ["Paddington Realty", "Agent"],
  ["Harbour Bank", "Lender"],
  ["Lee & Co Lawyers", "Other side"],
] as const;

function LiveTable() {
  const [rows, setRows] = useState(parties.slice(0, 3));
  const next = parties.find(party => !rows.includes(party));
  return (
    <div className="flex flex-col gap-3">
      <div className="flex gap-2">
        <Button
          intent="outline"
          size="xs"
          disabled={next === undefined}
          onClick={() => next && setRows([...rows, next])}
        >
          Add
        </Button>
        <Button
          intent="outline"
          size="xs"
          disabled={rows.length === 0}
          onClick={() => setRows(rows.slice(1))}
        >
          Remove first
        </Button>
        <Button
          intent="outline"
          size="xs"
          onClick={() =>
            setRows(rows.toSorted((a, b) => a[0].localeCompare(b[0]) * -1))
          }
        >
          Sort Z–A
        </Button>
      </div>
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>Name</TableHead>
            <TableHead>Role</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody animated>
          {rows.map(([name, role]) => (
            <TableRow key={name}>
              <TableCell>{name}</TableCell>
              <TableCell>{role}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </div>
  );
}

function Grouped() {
  const [open, setOpen] = useState<Record<string, boolean>>({
    Pleadings: true,
  });
  return (
    <Table className="w-130">
      <TableHeader>
        <TableRow>
          <TableHead>Name</TableHead>
          <TableHead>Added</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {folders.map(folder => (
          <Fragment key={folder.name}>
            <TableGroupHeader
              title={folder.name}
              count={folder.files.length}
              open={open[folder.name] ?? false}
              onOpenChange={next =>
                setOpen(current => ({ ...current, [folder.name]: next }))
              }
              colSpan={2}
            />
            <TableGroupRows open={open[folder.name] ?? false}>
              {folder.files.map(file => (
                <TableRow key={file}>
                  <TableCell>{file}</TableCell>
                  <TableCell>2 Nov</TableCell>
                </TableRow>
              ))}
            </TableGroupRows>
          </Fragment>
        ))}
      </TableBody>
    </Table>
  );
}
```

### Rows arriving and leaving

TableBody animated: a new row slides in with a brief tint, a removed one fades and the rows below glide up, and re-sorted rows glide to their places. Rows there on first render stay still. Key each row by its record. Every animated row is measured when the body re-renders, so leave it off for long tables that re-render often.

```tsx
<LiveTable />
```

### Workspace

The last row drops its rule so the shell's edge isn't doubled.

```tsx
<Table className="w-130">
  <TableHeader>
    <TableRow>
      <TableHead>Name</TableHead>
      <TableHead>Role</TableHead>
      <TableHead>Email</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    {contacts.map(([name, role, email]) => (
      <TableRow key={name}>
        <TableCell>{name}</TableCell>
        <TableCell>{role}</TableCell>
        <TableCell>{email}</TableCell>
      </TableRow>
    ))}
  </TableBody>
</Table>
```

### Main

```tsx
<Table variant="main" className="w-130">
  <TableHeader>
    <TableRow>
      <TableHead>Name</TableHead>
      <TableHead>Role</TableHead>
      <TableHead>Email</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    {contacts.map(([name, role, email]) => (
      <TableRow key={name}>
        <TableCell>{name}</TableCell>
        <TableCell>{role}</TableCell>
        <TableCell>{email}</TableCell>
      </TableRow>
    ))}
  </TableBody>
</Table>
```

### Group headers

A 28 px band with the group's title and count; the chevron points down while open and right while closed. Press it to fold the group: its rows, wrapped in TableGroupRows, ease shut on the resize curve and stay in the table, so the columns keep their widths and no heading moves. Render the rows whether open or not.

```tsx
<Grouped />
```

## Props

Each part takes every prop of its HTML element: table, thead, tbody, tr, th and td.

### Table

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"workspace" \| "main"` | `"workspace"` | Workspace for tables inside the opportunity and contact workspaces: 36 px header, 40 px rows, 13 px text, in a bordered shell. Main for the contacts and opportunities lists: 32 px uppercase header, 45 px rows, 14 px. |
| `tableClassName` | `string` |  | Classes for the <table> itself; className sizes and places the whole table, shell included. |

### TableGroupHeader

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `ReactNode` |  | The group's name, like a stage or a date. |
| `count` | `number` |  | How many rows the group holds, in a small count after the title. |
| `dotClassName` | `string` |  | An 8 px dot before the title, coloured by a background class, like a relationship side's colour. |
| `open` (required) | `boolean` |  | Whether the group's rows show. |
| `onOpenChange` (required) | `(open: boolean) => void` |  | Runs when it opens or closes, with whether it's now open. |
| `colSpan` (required) | `number` |  | How many columns the band spans. |

### TableGroupRows

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (required) | `boolean` |  | Whether the rows show: pass the same value as the group's header. |
| `children` (required) | `ReactNode` |  | The group's TableRows. Render them whether open or not. |
