# Table

Shows data in rows and columns, so people can scan, compare, sort and act on it.

```tsx
import { DataTable, Table } from '@halo-ds/react';
```

## When to use it
Tables are built from cells. Table header cell names a column and can sort it; Table cell holds one value, like text, a number, a person, a status, a checkbox or actions. Lay cells out in rows, then add Pagination underneath for long data.

## Do
- Right-align numbers and their headers, so digits line up.
- Keep the header row visible when the table scrolls.
- Use Medium when rows have avatars or descriptions.
- Put row actions in a "more" button at the end of the row.

## Don't
- Use a table for a few items or for layout; use a List.
- Centre text in cells; it is hard to scan.
- Wrap long text onto many lines; cut it off and show the rest elsewhere.
- Show selection with colour alone; tick the checkbox too.

## Table or List?
Both show many items. Pick by whether people compare values across columns.
- **Table**: Many items with the same fields, like name, role and status. People sort, compare and select rows. Wide screens, where columns have room.
- **List**: Items with one or two lines, like files or messages. Small screens, where columns would squeeze. On phones, show a table's rows as a List.

## Props
### DataTable props
| Prop | Type | What it does |
| --- | --- | --- |
| `columns` (required) | `DataTableColumn<T>[]` |  |
| `rows` (required) | `T[]` |  |
| `getRowId` (required) | `(row: T) => Key` |  |
| `selectable` | `boolean \| undefined` | Adds a checkbox column. |
| `selected` | `Key[] \| undefined` |  |
| `onSelectedChange` | `((ids: Key[]) => void) \| undefined` |  |
| `pageSize` | `number \| undefined` | Rows per page. Pass 0 to show every row. |
| `empty` | `ReactNode` | Shown when there are no rows, like an EmptyState. |
| `expandable` | `{ render: (row: T) => ReactNode; rowExpandable?: ((row: T) => boolean) \| undefined; } \| undefined` | Rows open to show details underneath. `rowExpandable` turns it off for some rows. |
| `classNames` | `Partial<Record<TablePart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<TablePart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
| `size` | `"sm" \| "md" \| undefined` | Row height: 52 or 40px. Figma: Size. |
| `caption` | `ReactNode` | Visible or screen reader caption. |
| `toolbar` | `ReactNode` | Toolbar above the table, like a TableToolbar. |
| `bordered` | `boolean \| undefined` | Wraps the table in a bordered box with rounded corners. |
| `stickyHeader` | `boolean \| undefined` | Keeps the header row in view while the rows scroll. Pair it with `maxHeight`. |
| `maxHeight` | `string \| number \| undefined` | Height after which the rows scroll, like 400 or "60vh". |
| `stickyFirstColumn` | `boolean \| undefined` | Keeps the first column in view when a wide table scrolls sideways. |
It also takes the usual HTML attributes for its element.

### Table props
| Prop | Type | What it does |
| --- | --- | --- |
| `size` | `"sm" \| "md" \| undefined` | Row height: 52 or 40px. Figma: Size. |
| `toolbar` | `ReactNode` | Toolbar above the table, like a TableToolbar. |
| `footer` | `ReactNode` | Footer below the table, like a TableFooter. |
| `bordered` | `boolean \| undefined` | Wraps the table in a bordered box with rounded corners. |
| `caption` | `ReactNode` | Visible or screen reader caption. |
| `stickyHeader` | `boolean \| undefined` | Keeps the header row in view while the rows scroll. Pair it with `maxHeight`. |
| `maxHeight` | `string \| number \| undefined` | Height after which the rows scroll, like 400 or "60vh". |
| `stickyFirstColumn` | `boolean \| undefined` | Keeps the first column in view when a wide table scrolls sideways. |
| `classNames` | `Partial<Record<TablePart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<TablePart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Badge, DataTable } from '@halo-ds/react';
import type { DataTableColumn } from '@halo-ds/react';

interface Member {
  id: number;
  name: string;
  role: string;
  status: 'Active' | 'Invited';
  projects: number;
}

const members: Member[] = [
  { id: 1, name: 'Ada Obi', role: 'Admin', status: 'Active', projects: 12 },
  { id: 2, name: 'Kofi Mensah', role: 'Editor', status: 'Active', projects: 7 },
  { id: 3, name: 'Lena Park', role: 'Editor', status: 'Invited', projects: 0 },
  { id: 4, name: 'Mateo Silva', role: 'Viewer', status: 'Active', projects: 3 },
];

const columns: DataTableColumn<Member>[] = [
  { key: 'name', header: 'Name', sortable: true },
  { key: 'role', header: 'Role', sortable: true, filters: [{ value: 'Admin', label: 'Admin' }, { value: 'Editor', label: 'Editor' }, { value: 'Viewer', label: 'Viewer' }] },
  { key: 'status', header: 'Status', render: (m) => <Badge size="sm" color={m.status === 'Active' ? 'success' : 'neutral'}>{m.status}</Badge> },
  { key: 'projects', header: 'Projects', sortable: true, numeric: true },
];

export default function Example() {
  return (
    <div style={{ width: '100%' }}>
      <DataTable columns={columns} rows={members} getRowId={(m) => m.id} selectable pageSize={0} />
    </div>
  );
}
```

### Simple table
Built by hand from the table parts, in the small size.
```tsx
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@halo-ds/react';

const rows = [
  ['January', 12840, 312],
  ['February', 14210, 355],
  ['March', 16975, 401],
] as const;

export default function Demo() {
  return (
    <div style={{ width: '100%', maxWidth: 480 }}>
      <Table size="sm" caption="Monthly usage">
        <TableHead>
          <tr>
            <TableHeaderCell>Month</TableHeaderCell>
            <TableHeaderCell align="right">Visits</TableHeaderCell>
            <TableHeaderCell align="right">Sign ups</TableHeaderCell>
          </tr>
        </TableHead>
        <TableBody>
          {rows.map(([month, visits, signUps]) => (
            <TableRow key={month}>
              <TableCell>{month}</TableCell>
              <TableCell numeric>{visits.toLocaleString()}</TableCell>
              <TableCell numeric>{signUps}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </div>
  );
}
```

### Filters and expandable rows
Filters in the column headers, and rows that open to show more details.
```tsx
import { DataTable } from '@halo-ds/react';
import type { DataTableColumn } from '@halo-ds/react';

const members = [
  { id: 1, name: 'Ada Obi', email: 'ada@example.com', role: 'Admin', status: 'Active' },
  { id: 2, name: 'Kofi Mensah', email: 'kofi@example.com', role: 'Editor', status: 'Active' },
  { id: 3, name: 'Lena Park', email: 'lena@example.com', role: 'Editor', status: 'Invited' },
  { id: 4, name: 'Mateo Silva', email: 'mateo@example.com', role: 'Viewer', status: 'Active' },
  { id: 5, name: 'Sade Bello', email: 'sade@example.com', role: 'Viewer', status: 'Active' },
  { id: 6, name: 'Yuki Tanaka', email: 'yuki@example.com', role: 'Editor', status: 'Invited' },
];

type Member = (typeof members)[number];

const columns: DataTableColumn<Member>[] = [
  { key: 'name', header: 'Name' },
  { key: 'role', header: 'Role', filters: [{ value: 'Admin', label: 'Admin' }, { value: 'Editor', label: 'Editor' }, { value: 'Viewer', label: 'Viewer' }] },
  { key: 'status', header: 'Status', filters: [{ value: 'Active', label: 'Active' }, { value: 'Invited', label: 'Invited' }] },
];

export default function Demo() {
  return (
    <div style={{ width: '100%' }}>
      <DataTable
        columns={columns}
        rows={members}
        getRowId={(m) => m.id}
        pageSize={0}
        expandable={{ render: (m) => <span>{m.email}</span> }}
      />
    </div>
  );
}
```

### Sticky header and first column
The header stays while rows scroll, and the names stay while the table scrolls sideways.
```tsx
import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from '@halo-ds/react';

const members = [
  { id: 1, name: 'Ada Obi', email: 'ada@example.com', role: 'Admin', status: 'Active' },
  { id: 2, name: 'Kofi Mensah', email: 'kofi@example.com', role: 'Editor', status: 'Active' },
  { id: 3, name: 'Lena Park', email: 'lena@example.com', role: 'Editor', status: 'Invited' },
  { id: 4, name: 'Mateo Silva', email: 'mateo@example.com', role: 'Viewer', status: 'Active' },
  { id: 5, name: 'Sade Bello', email: 'sade@example.com', role: 'Viewer', status: 'Active' },
  { id: 6, name: 'Yuki Tanaka', email: 'yuki@example.com', role: 'Editor', status: 'Invited' },
];

export default function Demo() {
  return (
    <div style={{ width: '100%', maxWidth: 520 }}>
      <Table stickyHeader stickyFirstColumn maxHeight={220} style={{ minWidth: 720 }}>
        <TableHead>
          <tr>
            <TableHeaderCell>Name</TableHeaderCell>
            <TableHeaderCell>Email</TableHeaderCell>
            <TableHeaderCell>Role</TableHeaderCell>
            <TableHeaderCell>Status</TableHeaderCell>
          </tr>
        </TableHead>
        <TableBody>
          {members.map((m) => (
            <TableRow key={m.id}>
              <TableCell style={{ whiteSpace: 'nowrap' }}>{m.name}</TableCell>
              <TableCell>{m.email}</TableCell>
              <TableCell>{m.role}</TableCell>
              <TableCell>{m.status}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/table
