Skip to content

Table

Structured data with consistent alignment and optional selection.

Project Status
Weekend notes Active
A new idea Draft

Installation

Terminal
pnpm dlx shadcn@latest add \
  https://react-toolbox.io/r/table.json \
  https://react-toolbox.io/r/button.json

New 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.

Terminal
pnpm dlx shadcn@latest add https://react-toolbox.io/r/example-recipes.json

Custom rows and sections

Team members
Name Role
Ada Lovelace Member
Grace Hopper Member
2 members
Selected: ada

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.

Project budgets
Owner
Coastal survey Ada 2,500.00
Forest canopy Margaret 4,200.00
River habitats Grace 1,800.00
0 selected · 4 projects · Page 1 of 2
Show composition code
tsx
'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

  • Table and TableRoot use stable row values with selected/onSelectionChange or defaultSelected. Select-all affects mounted, enabled rows and preserves selections outside the current view.
  • Use selectionMode="single" or selectionMode="multiple" to enable selection. Give each selectable TableRow a stable string value. Rows without a value remain display-only. Row and header onSelectionChange callbacks 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>) => void

Cancel the event to prevent a row selection change.

—
selectionDisabled boolean false
value string

Stable 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>) => void

Cancel 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.

Material Design 1: Data tables