import React from 'react'; import { flexRender, type Column, type RowData, type Table as TanStackTable } from '@tanstack/react-table'; import { cn } from '@/lib/cn'; import { isString } from '@/lib/objects'; import { Empty, EmptyDescription } from '@/components/ui/empty'; import { emptyCompactClass } from '@/components/ui/styles'; declare module '@tanstack/react-table' { interface ColumnMeta { cellClassName?: string; headerClassName?: string; /** Hidden columns whose sort state this header reports through `aria-sort`. */ sortColumnIds?: string[]; } } interface Props { table: TanStackTable; /** Replaces the table when there are no rows; a string renders as a compact `Empty` description. */ emptyState: React.ReactNode; className?: string; tableClassName?: string; isFetching?: boolean; } const ariaSort = ( table: TanStackTable, column: Column ): React.AriaAttributes['aria-sort'] => { const ids = column.columnDef.meta?.sortColumnIds; const direction = ids ? (ids.map((id) => table.getColumn(id)?.getIsSorted()).find(Boolean) ?? false) : column.getIsSorted(); if (direction === 'asc') return 'ascending'; if (direction === 'desc') return 'descending'; return undefined; }; export default function DataTable({ table, emptyState, className, tableClassName, isFetching = false, }: Props) { const rows = table.getRowModel().rows; return (
{rows.length === 0 ? (
{isString(emptyState) ? ( {emptyState} ) : ( emptyState )}
) : (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} ))} {rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} ))}
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
)}
); }