Table
Structured data with consistent alignment and optional selection.
| Project | Status |
|---|---|
| Weekend notes | Active |
| A new idea | Draft |
Installation
pnpm dlx shadcn@latest add \
https://react-toolbox.io/r/table.json \
https://react-toolbox.io/r/button.jsonNew here? Set up your project first. Styles, fonts, and required components are included. The command includes the components used by the examples below.
Usage
Put TableHead and TableRow directly inside Table. Selection is off by default. Enable it with selectionMode and give each selectable row a stable string value.
Composition
Compose native sections and custom rows with TableRoot. Row value keeps selection stable through reordering and filtering; header semantics pass through wrappers.
pnpm dlx shadcn@latest add https://react-toolbox.io/r/example-recipes.jsonCustom rows and sections
| Name | Role | |
|---|---|---|
| Ada Lovelace | Member | |
| Grace Hopper | Member | |
| 2 members | ||
Filter, sort, select and paginate
Selection uses stable record IDs across filtering, sorting, and pages. The header selects the current page. Keep remote fetching and larger data engines in your application.
| Owner | |||
|---|---|---|---|
| Coastal survey | Ada | 2,500.00 | |
| Forest canopy | Margaret | 4,200.00 | |
| River habitats | Grace | 1,800.00 |
Show composition code
'use client';
import { useState } from 'react';
import { Button } from '@/components/ui/material/button';
import { DataTableRecipe } from '@/components/ui/examples/recipes';
import {
TableRoot,
TableCaption,
TableHeader,
TableHead,
TableBody,
TableRow,
TableCell,
TableFooter,
} from '@/components/ui/material/table';
const projects = [
{ id: 'coast', name: 'Coastal survey', owner: 'Ada', budget: 2500 },
{ id: 'river', name: 'River habitats', owner: 'Grace', budget: 1800 },
{ id: 'forest', name: 'Forest canopy', owner: 'Margaret', budget: 4200 },
{ id: 'wetland', name: 'Wetland census', owner: 'Katherine', budget: 3200 },
];
function PersonRow({ person }: { person: { id: string; name: string } }) {
return (
<TableRow value={person.id} selectLabel={`Select ${person.name}`}>
<TableCell>{person.name}</TableCell>
<TableCell>Member</TableCell>
</TableRow>
);
}
function NameHeading() {
return <TableCell>Name</TableCell>;
}
export default function TableComposition() {
const [people, setPeople] = useState([
{ id: 'ada', name: 'Ada Lovelace' },
{ id: 'grace', name: 'Grace Hopper' },
]);
const [selected, setSelected] = useState<string[]>(['ada']);
return (
<div className="space-y-8">
<section aria-label="Custom table rows" className="space-y-4">
<h3 className="text-toolbox-subheading">Custom rows and sections</h3>
<Button onClick={() => setPeople([...people].reverse())}>Reverse row order</Button>
<div className="overflow-x-auto">
<TableRoot selectionMode="multiple" selected={selected} onSelectionChange={setSelected}>
<TableCaption>Team members</TableCaption>
<TableHeader>
<TableHead>
<NameHeading />
<TableCell>Role</TableCell>
</TableHead>
</TableHeader>
<TableBody>
{people.map((person) => (
<PersonRow key={person.id} person={person} />
))}
</TableBody>
<TableFooter>
<TableRow>
<TableCell colSpan={3}>{people.length} members</TableCell>
</TableRow>
</TableFooter>
</TableRoot>
</div>
<output className="block text-toolbox-body">Selected: {selected.join(', ') || 'none'}</output>
</section>
<section aria-label="Data table workflow" className="space-y-4">
<h3 className="text-toolbox-subheading">Filter, sort, select and paginate</h3>
<p className="text-toolbox-body/6">
Selection uses stable record IDs across filtering, sorting, and pages. The header selects the current page.
Keep remote fetching and larger data engines in your application.
</p>
<DataTableRecipe projects={projects} pageSize={3} />
</section>
</div>
);
}
State and callbacks
TableandTableRootuse stable row values withselected/onSelectionChangeordefaultSelected. Select-all affects mounted, enabled rows and preserves selections outside the current view.- Use
selectionMode="single"orselectionMode="multiple"to enable selection. Give each selectableTableRowa stable stringvalue. Rows without a value remain display-only. Row and headeronSelectionChangecallbacks can cancel the event before selection changes.
API reference
Key props and defaults. Full types and inline documentation are included with the source in material/table.
Table
| Property | Type | Default |
|---|---|---|
defaultSelected | readonly string[] | — |
onSelectionChange | (selected: string[]) => void | — |
selected | readonly string[] | — |
selectionMode | 'none' | 'single' | 'multiple' | — |
TableRow
| Property | Type | Default |
|---|---|---|
onSelectionChange | (checked: boolean, event: React.MouseEvent<HTMLInputElement>) => voidCancel the event to prevent a row selection change. | — |
selectionDisabled | boolean | false |
value | stringStable identity for selection inside Table or TableRoot; omit for structural rows. | — |
TableHead
| Property | Type | Default |
|---|---|---|
displaySelect | boolean | true |
onSelectionChange | (checked: boolean, event: React.MouseEvent<HTMLInputElement>) => voidCancel the event to prevent a bulk selection change. | — |
TableCell
| Property | Type | Default |
|---|---|---|
numeric | boolean | false |
sorted | 'asc' | 'desc' | — |
tagName | 'td' | 'th' | — |
Material guidelines
- Use tables for comparable values organized into consistent columns. Left-align descriptive text and right-align numeric headers and values.
- Keep row selection distinct from sorting or other actions. On narrow screens, contain horizontal scrolling inside the table instead of widening the page.