Files

105 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import React from 'react';
import { flexRender, type Column, type RowData, type Table as TanStackTable } from '@tanstack/react-table';
import { isString } from '@/lib/objects';
import { Empty, EmptyDescription } from '@/components/ui/empty';
2026-10-12 09:04:03 -04:00
import { useSkin } from '@/themes/skins';
2026-10-05 18:10:18 -04:00
declare module '@tanstack/react-table' {
interface ColumnMeta<TData extends RowData, TValue> {
cellClassName?: string;
headerClassName?: string;
/** Hidden columns whose sort state this header reports through `aria-sort`. */
sortColumnIds?: string[];
}
}
2026-10-12 09:04:03 -04:00
export interface DataTableProps<TData extends RowData> {
2026-10-05 18:10:18 -04:00
table: TanStackTable<TData>;
/** 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 = <TData extends RowData>(
table: TanStackTable<TData>,
column: Column<TData, unknown>
): 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();
2026-10-08 16:53:20 -04:00
if (direction === 'asc') {
return 'ascending';
}
if (direction === 'desc') {
return 'descending';
}
2026-10-05 18:10:18 -04:00
return undefined;
};
export default function DataTable<TData extends RowData>({
table,
emptyState,
className,
tableClassName,
isFetching = false,
2026-10-12 09:04:03 -04:00
}: DataTableProps<TData>) {
2026-10-05 18:10:18 -04:00
const rows = table.getRowModel().rows;
2026-10-12 09:04:03 -04:00
const ui = useSkin('Table', { fetching: isFetching });
2026-10-05 18:10:18 -04:00
return (
2026-10-12 09:04:03 -04:00
<div aria-busy={isFetching} {...ui('root', className)}>
2026-10-05 18:10:18 -04:00
{rows.length === 0 ? (
2026-10-12 09:04:03 -04:00
<div {...ui('empty')}>
2026-10-05 18:10:18 -04:00
{isString(emptyState) ? (
2026-10-12 09:04:03 -04:00
<Empty size='compact'>
2026-10-05 18:10:18 -04:00
<EmptyDescription>{emptyState}</EmptyDescription>
</Empty>
) : (
emptyState
)}
</div>
) : (
2026-10-12 09:04:03 -04:00
<div {...ui('scroller')}>
<table {...ui('table', tableClassName)}>
<thead {...ui('head')}>
2026-10-05 18:10:18 -04:00
{table.getHeaderGroups().map((headerGroup) => (
2026-10-12 09:04:03 -04:00
<tr key={headerGroup.id} {...ui('headerRow')}>
2026-10-05 18:10:18 -04:00
{headerGroup.headers.map((header) => (
<th
key={header.id}
2026-10-08 16:53:20 -04:00
scope='col'
2026-10-05 18:10:18 -04:00
aria-sort={ariaSort(table, header.column)}
2026-10-12 09:04:03 -04:00
{...ui('headerCell', header.column.columnDef.meta?.headerClassName)}
2026-10-05 18:10:18 -04:00
>
{header.isPlaceholder
? null
: flexRender(header.column.columnDef.header, header.getContext())}
</th>
))}
</tr>
))}
</thead>
2026-10-12 09:04:03 -04:00
<tbody {...ui('body')}>
2026-10-05 18:10:18 -04:00
{rows.map((row) => (
2026-10-12 09:04:03 -04:00
<tr key={row.id} {...ui('row')}>
2026-10-05 18:10:18 -04:00
{row.getVisibleCells().map((cell) => (
2026-10-12 09:04:03 -04:00
<td key={cell.id} {...ui('cell', cell.column.columnDef.meta?.cellClassName)}>
2026-10-05 18:10:18 -04:00
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
}