Synertech Web/ui v0.57.5 Site Storybook

Documentation / Données

Table

Compatible serveur@synertech/uiVoir dans Storybook

Un tableau HTML stylé, à composer à la main : Table, TableHeader, TableHeaderCell, TableBody, TableRow et TableCell rendent respectivement <table>, <thead> (qui pose lui-même la ligne <tr>), <th scope="col">, <tbody>, <tr> et <td>. Chacun accepte les attributs natifs de son élément.

C’est la brique de présentation : le tri, la pagination ou la sélection restent à la charge de l’application. Dès que ces besoins apparaissent sur des données, préférez DataGrid. Aucun hook : le tableau se rend côté serveur.

Attention : TableRow a clickable à true par défaut (curseur et survol) ; passez clickable={false} pour une ligne purement informative.

Exemples

Utilisation de base

Les en-têtes se posent directement dans TableHeader, sans <tr> intermédiaire.

Ouvrir dans un onglet
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>
  );
}

Lignes cliquables

onClick sur TableRow ouvre le détail ; doublez-le d’un lien ou d’un bouton dans la ligne pour les utilisateurs au clavier.

Ouvrir dans un onglet
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>
  );
}

Tri par colonne

sortable ne fait que l’apparence de l’en-tête ; le tri se calcule dans l’application, un bouton le rend accessible au clavier et aria-sort l’annonce.

Ouvrir dans un onglet
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>
  );
}

Cellules riches

Une cellule accepte n’importe quel contenu : ici un Chip de statut et une légende de tableau pour les technologies d’assistance.

Ouvrir dans un onglet
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>
  );
}

Référence d’API

Composants

Table

ComposantCompatible serveur
import { Table } from "@synertech/ui";

Props — TableProps

PropTypeDéfautDescription
children• ReactNode —

+ 286 attributs natifs hérités de TableHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, gestionnaires d’événements…).

TableBody

ComposantCompatible serveur
import { TableBody } from "@synertech/ui";

Props — TableBodyProps

PropTypeDéfautDescription
children• ReactNode —

+ 277 attributs natifs hérités de HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, gestionnaires d’événements…).

TableCell

ComposantCompatible serveur
import { TableCell } from "@synertech/ui";

Props — TableCellProps

PropTypeDéfautDescription
children ReactNode —

+ 286 attributs natifs hérités de TdHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, gestionnaires d’événements…).

TableHeader

ComposantCompatible serveur
import { TableHeader } from "@synertech/ui";

Props — TableHeaderProps

PropTypeDéfautDescription
children• ReactNode —

Les cellules d’en-tête, directement : TableHeader dessine la ligne qui les contient. Un TableRow ici serait un <tr> dans un <tr>.

+ 277 attributs natifs hérités de HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, gestionnaires d’événements…).

TableHeaderCell

ComposantCompatible serveur
import { TableHeaderCell } from "@synertech/ui";

Props — TableHeaderCellProps

PropTypeDéfautDescription
children ReactNode —
sortable boolean false

+ 283 attributs natifs hérités de ThHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, gestionnaires d’événements…).

TableRow

ComposantCompatible serveur
import { TableRow } from "@synertech/ui";

Props — TableRowProps

PropTypeDéfautDescription
children• ReactNode —
clickable boolean onClick !== undefined

Dessine la ligne comme une ligne qui répond au clic : teinte au survol et pointeur. Par défaut, selon qu’elle a un onClick ; chaque ligne d’un simple tableau portait une main.

+ 277 attributs natifs hérités de HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, gestionnaires d’événements…).