import { Header, flexRender } from '@tanstack/react-table'; import { filterHOC } from './Filter'; import { TableHeaderCell } from './TableHeaderCell'; interface Props = Record> { headers: Header[]; onSortChange?(colId: string, desc: boolean): void; } export function TableHeaderRow< D extends Record = Record >({ headers, onSortChange }: Props) { return ( {headers.map((header) => { const sortDirection = header.column.getIsSorted(); const { meta: { className, width } = { className: '', width: undefined }, } = header.column.columnDef; return ( { header.column.toggleSorting(desc); if (onSortChange) { onSortChange(header.id, desc); } }} isSorted={!!sortDirection} isSortedDesc={sortDirection ? sortDirection === 'desc' : false} render={() => flexRender(header.column.columnDef.header, header.getContext()) } renderFilter={ header.column.getCanFilter() ? () => flexRender( header.column.columnDef.meta?.filter || filterHOC('Filter'), { column: header.column, } ) : undefined } /> ); })} ); }