Synertech Web/ui v0.57.5 Site Storybook

Documentation / Data

Table

Server-compatible@synertech/uiOpen in Storybook

A styled HTML table, composed by hand: Table, TableHeader, TableHeaderCell, TableBody, TableRow and TableCell render <table>, <thead> (which sets its own <tr> row), <th scope="col">, <tbody>, <tr> and <td> respectively. Each accepts its element’s native attributes.

It is the presentation building block: sorting, pagination and selection are left to the application. As soon as those needs arise on data, prefer DataGrid. No hooks: the table renders on the server.

Beware: TableRow has clickable set to true by default (cursor and hover); pass clickable={false} for a purely informative row.

Examples

Basic usage

Headers go straight into TableHeader, with no intermediate <tr>.

Open in a tab
import { Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow } from "@synertech/ui";

const invoices = [
  { id: "F-2024-031", client: "Atelier Dupont", amount: 1250, status: "Payée" },
  { id: "F-2024-032", client: "Boulangerie Petit", amount: 380, status: "En attente" },
  { id: "F-2024-033", client: "Garage Moreau", amount: 2140, status: "En retard" },
];

const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });

export function Basic() {
  return (
    <Table>
      <TableHeader>
        <TableHeaderCell>Facture</TableHeaderCell>
        <TableHeaderCell>Client</TableHeaderCell>
        <TableHeaderCell className="text-right">Montant</TableHeaderCell>
      </TableHeader>
      <TableBody>
        {invoices.map((invoice) => (
          <TableRow key={invoice.id} clickable={false}>
            <TableCell className="font-medium text-gray-900 dark:text-white">
              {invoice.id}
            </TableCell>
            <TableCell>{invoice.client}</TableCell>
            <TableCell className="text-right">{euro.format(invoice.amount)}</TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}

Clickable rows

onClick on TableRow opens the detail; back it with a link or a button in the row for keyboard users.

Open in a tab
import { useState } from "react";
import { Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow } from "@synertech/ui";

const invoices = [
  { id: "F-2024-031", client: "Atelier Dupont", amount: 1250, status: "Payée" },
  { id: "F-2024-032", client: "Boulangerie Petit", amount: 380, status: "En attente" },
  { id: "F-2024-033", client: "Garage Moreau", amount: 2140, status: "En retard" },
];

export function ClickableRows() {
  const [opened, setOpened] = useState<string | null>(null);

  return (
    <div className="space-y-3">
      <Table>
        <TableHeader>
          <TableHeaderCell>Facture</TableHeaderCell>
          <TableHeaderCell>Client</TableHeaderCell>
        </TableHeader>
        <TableBody>
          {invoices.map((invoice) => (
            <TableRow key={invoice.id} onClick={() => setOpened(invoice.id)}>
              <TableCell>
                <button type="button" className="text-blue-600 hover:underline">
                  {invoice.id}
                </button>
              </TableCell>
              <TableCell>{invoice.client}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
      <p className="text-sm">Facture ouverte : {opened ?? "aucune"}</p>
    </div>
  );
}

Sorting by column

sortable only styles the header; sorting is computed in the application, a button makes it keyboard accessible and aria-sort announces it.

Open in a tab
import { useMemo, useState } from "react";
import { Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow } from "@synertech/ui";

type SortKey = "client" | "amount";

const invoices = [
  { id: "F-2024-031", client: "Atelier Dupont", amount: 1250, status: "Payée" },
  { id: "F-2024-032", client: "Boulangerie Petit", amount: 380, status: "En attente" },
  { id: "F-2024-033", client: "Garage Moreau", amount: 2140, status: "En retard" },
];

const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });

export function Sortable() {
  const [sortKey, setSortKey] = useState<SortKey>("client");
  const [ascending, setAscending] = useState(true);

  const rows = useMemo(() => {
    const sorted = [...invoices].sort((a, b) =>
      sortKey === "amount" ? a.amount - b.amount : a.client.localeCompare(b.client, "fr")
    );
    return ascending ? sorted : sorted.reverse();
  }, [sortKey, ascending]);

  const sortBy = (key: SortKey) => {
    setAscending(key === sortKey ? !ascending : true);
    setSortKey(key);
  };
  const ariaSort = (key: SortKey) =>
    key === sortKey ? (ascending ? "ascending" : "descending") : "none";

  return (
    <Table>
      <TableHeader>
        {(["client", "amount"] as const).map((key) => (
          <TableHeaderCell key={key} sortable aria-sort={ariaSort(key)}>
            <button type="button" className="uppercase" onClick={() => sortBy(key)}>
              {key === "client" ? "Client" : "Montant"}
            </button>
          </TableHeaderCell>
        ))}
      </TableHeader>
      <TableBody>
        {rows.map((invoice) => (
          <TableRow key={invoice.id} clickable={false}>
            <TableCell>{invoice.client}</TableCell>
            <TableCell>{euro.format(invoice.amount)}</TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}

Rich cells

A cell accepts any content: here a status Chip and a table caption for assistive technologies.

Open in a tab
import { Chip, Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow } from "@synertech/ui";

const invoices = [
  { id: "F-2024-031", client: "Atelier Dupont", amount: 1250, status: "Payée" },
  { id: "F-2024-032", client: "Boulangerie Petit", amount: 380, status: "En attente" },
  { id: "F-2024-033", client: "Garage Moreau", amount: 2140, status: "En retard" },
];

const statusColor = { Payée: "success", "En attente": "warning", "En retard": "error" } as const;

export function RichCells() {
  return (
    <Table>
      <caption className="sr-only">Factures du mois</caption>
      <TableHeader>
        <TableHeaderCell>Client</TableHeaderCell>
        <TableHeaderCell>Statut</TableHeaderCell>
      </TableHeader>
      <TableBody>
        {invoices.map((invoice) => (
          <TableRow key={invoice.id} clickable={false}>
            <TableCell>{invoice.client}</TableCell>
            <TableCell>
              <Chip
                variant="dot"
                size="sm"
                color={statusColor[invoice.status as keyof typeof statusColor]}
                label={invoice.status}
              />
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}

API reference

Components

Table

ComponentServer-compatible
import { Table } from "@synertech/ui";

Props — TableProps

PropTypeDefaultDescription
children• ReactNode —

+ 286 inherited native attributes from TableHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).

TableBody

ComponentServer-compatible
import { TableBody } from "@synertech/ui";

Props — TableBodyProps

PropTypeDefaultDescription
children• ReactNode —

+ 277 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).

TableCell

ComponentServer-compatible
import { TableCell } from "@synertech/ui";

Props — TableCellProps

PropTypeDefaultDescription
children ReactNode —

+ 286 inherited native attributes from TdHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).

TableHeader

ComponentServer-compatible
import { TableHeader } from "@synertech/ui";

Props — TableHeaderProps

PropTypeDefaultDescription
children• ReactNode —

The header cells, straight in: TableHeader draws the row they sit in. A TableRow here would be a <tr> in a <tr>.

+ 277 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).

TableHeaderCell

ComponentServer-compatible
import { TableHeaderCell } from "@synertech/ui";

Props — TableHeaderCellProps

PropTypeDefaultDescription
children ReactNode —
sortable boolean false

+ 283 inherited native attributes from ThHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).

TableRow

ComponentServer-compatible
import { TableRow } from "@synertech/ui";

Props — TableRowProps

PropTypeDefaultDescription
children• ReactNode —
clickable boolean onClick !== undefined

Draws the row as one that responds to a click: a hover tint and a pointer. Defaults to whether it has an onClick; every row of a plain table wore a hand cursor.

+ 277 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).