# DataTable

> A Table with clickable rows and an empty state.

- Category: [Data display](https://tarve.dev/docs.md#data)
- Import: `import { DataTable } from "@tarve/core";`
- Props type: `DataTableProps<T>` (generic)
- Source: [packages/core/src/extra-controls.ts:692](https://github.com/omarcosr/tarve/blob/main/packages/core/src/extra-controls.ts#L692)

## Usage

```tsx title="DataTable.tsx"
import { Badge, DataTable } from "@tarve/core";

const users = [
  { id: 1, name: "Ada", role: "Owner" },
  { id: 2, name: "Grace", role: "Member" },
];

<DataTable
  style={{ width: 400 }}
  data={users}
  rowKey={(row) => row.id}
  empty="No users."
  columns={[
    { key: "name", header: "Name", accessor: "name" },
    { key: "role", header: "Role", render: (row) => <Badge variant="secondary">{row.role}</Badge> },
  ]}
  onRowClick={(row) => console.log(row.name)}
/>
```

## Props

DataTable has no props of its own beyond the inherited and common props below.

### Inherited from `TableProps`

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` (required) | `TableColumn<T>[]` | — |  |
| `data` (required) | `T[]` | — |  |
| `empty` | `Child` | — |  |
| `onRowClick` | `(row: T, index: number) => void` | — |  |
| `rowKey` | `(row: T, index: number) => string \| number` | — |  |

## Common props

Shared by every component (descriptions: https://tarve.dev/docs.md#common-props): `children`, `class`, `className`, `highlight`, `id`, `key`, `motionFrom`, `onHighlight`, `onPaste`, `onPointerLeave`, `onPointerMove`, `onSize`, `onTransitionEnd`, `rovingGroup`, `style`.

## Types

```ts title="TableColumn"
interface TableColumn<T> {
  key: string;
  header: Child;
  accessor?: keyof T | ((row: T, index: number) => Child);
  render?: (row: T, index: number) => Child;
  width?: Style["width"];
  align?: Style["align"];
}
```

Previous: [Table](https://tarve.dev/docs/components/Table.md) · Next: [DataGrid](https://tarve.dev/docs/components/DataGrid.md)

---

Tarve v0.13.8 · HTML version: https://tarve.dev/docs/components/DataTable · Index for agents: https://tarve.dev/llms.txt
