// SPDX-License-Identifier: Apache-2.0 /** * M3 DataTable: TanStack Table v9 (sorting, global filter, pagination) rendered as a semantic `` * styled with the `--md-sys-*` tokens. With `virtual`, rows are virtualized by TanStack Virtual instead of paged. */ import { useVirtualizer } from "@tanstack/react-virtual"; import { type ColumnDef, columnFilteringFeature, createColumnHelper, createFilteredRowModel, createPaginatedRowModel, createSortedRowModel, filterFn_includesString, globalFilteringFeature, rowPaginationFeature, rowSortingFeature, sortFn_alphanumeric, sortFn_basic, sortFn_text, tableFeatures, useTable, type RowData, } from "@tanstack/react-table"; import { type CSSProperties, type ReactNode, useId, useRef } from "react"; /** The features of every DataTable: sorting, column and global filtering, pagination. */ export const dataTableFeatures = tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric, text: sortFn_text, basic: sortFn_basic, }, columnFilteringFeature, globalFilteringFeature, filteredRowModel: createFilteredRowModel(), filterFns: { includesString: filterFn_includesString }, rowPaginationFeature, paginatedRowModel: createPaginatedRowModel(), }); export type DataTableFeatures = typeof dataTableFeatures; export type DataColumn = ColumnDef; /** Column helper typed for DataTable: `const col = dataColumns(); col.accessor("name", { header: "Name" })`. */ export const dataColumns = () => createColumnHelper(); export interface DataTableProps { data: TData[]; columns: DataColumn[]; /** Accessible caption (also the table label). */ caption?: ReactNode; /** Show a search field filtering every column. */ searchable?: boolean; searchLabel?: string; /** Rows per page (default 25). Ignored with `virtual`. */ pageSize?: number; /** Virtualize rows in a scroll container of this height (px) instead of paging. */ virtual?: { height: number; rowHeight?: number; overscan?: number }; /** Initial sort, e.g. `[{ id: "name", desc: false }]`. */ initialSorting?: { id: string; desc: boolean }[]; getRowId?: (row: TData, index: number) => string; onRowClick?: (row: TData) => void; emptyLabel?: ReactNode; className?: string; } const EMPTY: never[] = []; const s = { root: { display: "flex", flexDirection: "column", gap: 8, color: "var(--md-sys-color-on-surface)", font: "var(--md-sys-typescale-body-medium-weight, 400) var(--md-sys-typescale-body-medium-size, 14px) / var(--md-sys-typescale-body-medium-line-height, 20px) var(--md-sys-typescale-body-medium-font, inherit)", }, container: { overflow: "auto", borderRadius: "var(--md-sys-shape-corner-medium, 12px)", border: "1px solid var(--md-sys-color-outline-variant)", background: "var(--md-sys-color-surface)", }, table: { width: "100%", borderCollapse: "collapse" }, caption: { textAlign: "start", padding: "12px 16px", font: "var(--md-sys-typescale-title-medium-weight, 500) var(--md-sys-typescale-title-medium-size, 16px) / var(--md-sys-typescale-title-medium-line-height, 24px) var(--md-sys-typescale-title-medium-font, inherit)", }, th: { position: "sticky", top: 0, textAlign: "start", padding: "0 16px", height: 56, background: "var(--md-sys-color-surface-container)", color: "var(--md-sys-color-on-surface-variant)", borderBottom: "1px solid var(--md-sys-color-outline-variant)", font: "var(--md-sys-typescale-title-small-weight, 500) var(--md-sys-typescale-title-small-size, 14px) / var(--md-sys-typescale-title-small-line-height, 20px) var(--md-sys-typescale-title-small-font, inherit)", }, sort: { all: "unset", cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 4, }, td: { padding: "0 16px", height: 52, borderBottom: "1px solid var(--md-sys-color-outline-variant)", }, search: { alignSelf: "flex-start", minWidth: 240, height: 40, padding: "0 16px", borderRadius: "var(--md-sys-shape-corner-extra-large, 28px)", border: "1px solid var(--md-sys-color-outline)", background: "var(--md-sys-color-surface-container-high)", color: "inherit", font: "inherit", }, pager: { display: "flex", alignItems: "center", justifyContent: "flex-end", gap: 8, }, button: { height: 40, padding: "0 16px", borderRadius: "var(--md-sys-shape-corner-full, 20px)", border: "1px solid var(--md-sys-color-outline)", background: "transparent", color: "var(--md-sys-color-primary)", font: "inherit", cursor: "pointer", }, } satisfies Record; const SORT_MARK = { asc: " ▲", desc: " ▼" } as const; export function DataTable(props: DataTableProps) { const { data, columns, caption, searchable = false, searchLabel = "Search", pageSize = 25, virtual, initialSorting = EMPTY, getRowId, onRowClick, emptyLabel = "No data", className, } = props; const searchId = useId(); const table = useTable({ features: dataTableFeatures, data, columns, getRowId, globalFilterFn: "includesString", initialState: { sorting: initialSorting, pagination: { pageIndex: 0, pageSize: virtual ? Number.MAX_SAFE_INTEGER : pageSize, }, }, }); const rows = table.getRowModel().rows; const columnCount = table.getAllLeafColumns().length; const scrollRef = useRef(null); const rowHeight = virtual?.rowHeight ?? 52; // oxlint-disable-next-line react/incompatible-library -- TanStack Virtual renvoie des fonctions non mémoïsables (getVirtualItems, measureElement) : le compilateur React doit l'ignorer const virtualizer = useVirtualizer({ count: virtual ? rows.length : 0, getScrollElement: () => scrollRef.current, estimateSize: () => rowHeight, getItemKey: index => rows[index]?.id ?? index, overscan: virtual?.overscan ?? 8, initialRect: virtual ? { width: 0, height: virtual.height } : undefined, }); const items = virtual ? virtualizer.getVirtualItems() : undefined; const visible = items ? items.map(item => rows[item.index]!) : rows; const padTop = items?.[0]?.start ?? 0; const padBottom = items?.length ? virtualizer.getTotalSize() - items[items.length - 1]!.end : 0; return (
{searchable && ( table.setGlobalFilter(event.currentTarget.value)} /> )}
{caption && } {table.getHeaderGroups().map(group => ( {group.headers.map(header => { const sorted = header.column.getIsSorted(); const label = header.isPlaceholder ? null : ; return ( ); })} ))} {padTop > 0 && ( {/* oxlint-disable-next-line jsx-a11y/control-has-associated-label -- cellule de remplissage de virtualisation (ligne aria-hidden, sans contenu ni contrôle) */} )} {visible.map(row => ( onRowClick(row.original as TData) : undefined} style={onRowClick ? { cursor: "pointer" } : undefined} > {row.getAllCells().map(cell => ( // oxlint-disable-next-line jsx-a11y/control-has-associated-label -- cellule de données (td), pas un contrôle : son contenu est rendu par FlexRender ))} ))} {padBottom > 0 && ( {/* oxlint-disable-next-line jsx-a11y/control-has-associated-label -- cellule de remplissage de virtualisation (ligne aria-hidden, sans contenu ni contrôle) */} )} {rows.length === 0 && ( )}
{caption}
{header.column.getCanSort() ? ( ) : ( label )}
{emptyLabel}
{!virtual && table.getPageCount() > 1 && ( )} ); }