import { flexRender, getCoreRowModel, getSortedRowModel, TableOptions, useReactTable, } from '@tanstack/react-table'; import { ComponentType, ReactNode } from 'react'; import { AutomationTestingProps } from '@/types'; import { Icon } from '@@/Icon'; import { defaultGetRowId } from './defaultGetRowId'; import { DefaultType } from './types'; interface Props extends AutomationTestingProps { dataset: D[]; columns: TableOptions['columns']; getRowId?(row: D): string; emptyContentLabel?: string; title?: ReactNode; titleIcon?: ComponentType<{ className?: string; size?: number | string }>; initialSortBy?: { id: string; desc: boolean }; 'aria-label'?: string; } export function BasicTable({ dataset, columns, getRowId = defaultGetRowId, emptyContentLabel = 'No items.', title, titleIcon, initialSortBy, 'data-cy': dataCy, 'aria-label': ariaLabel, }: Props) { const tableInstance = useReactTable({ columns, data: dataset, getRowId, initialState: { sorting: initialSortBy ? [initialSortBy] : [], }, defaultColumn: { enableColumnFilter: false, enableHiding: false, }, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }); const headerGroups = tableInstance.getHeaderGroups(); const { rows } = tableInstance.getRowModel(); return (
{title && (
{titleIcon && } {title}
)} {headerGroups.map((hg) => ( {hg.headers.map((header) => ( ))} ))} {rows.length === 0 ? ( ) : ( rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} )) )}
{header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )}
{emptyContentLabel}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
); }