import React, { useMemo } from "react"; import { ArrowUpIcon, ArrowDownIcon } from "@heroicons/react/24/solid"; import { useReactTable, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, ColumnDef, flexRender, getSortedRowModel, Table as ReactTable, CellContext, HeaderContext, } from "@tanstack/react-table"; import Checkbox from "../checkbox/checkbox"; export interface TableProps { data: TData[]; columns: ColumnDef[]; enableRowSelection?: boolean; enableSorting?: boolean; cellProps?: ( context: CellContext ) => React.HTMLProps; headerCellProps?: ( context: HeaderContext ) => React.HTMLProps; header?: (table: ReactTable) => React.ReactNode; footer?: (table: ReactTable) => React.ReactNode; } export function Table({ data, columns, enableRowSelection = false, enableSorting = false, cellProps = () => ({}), headerCellProps = () => ({}), header = () => null, footer = () => null, }: TableProps) { const table = useReactTable({ data, columns: columns, getCoreRowModel: getCoreRowModel(), getFilteredRowModel: getFilteredRowModel(), getSortedRowModel: enableSorting ? getSortedRowModel() : undefined, getPaginationRowModel: getPaginationRowModel(), debugTable: true, }); return (
{header(table)}
{table.getHeaderGroups().map((headerGroup, headerGroupIndex) => ( {headerGroup.headers.map((header, headerIndex) => { return ( ); })} ))} {table.getRowModel().rows.map((row) => { return ( {row.getVisibleCells().map((cell, cellIndex) => { return ( ); })} ); })}
{header.isPlaceholder ? null : (
{enableRowSelection && headerGroupIndex === table.getHeaderGroups().length - 1 && headerIndex === 0 && ( )} {flexRender( header.column.columnDef.header, header.getContext() )} {{ asc: ( ), desc: ( ), }[header.column.getIsSorted() as string] ?? null}
)}
{enableRowSelection && cellIndex === 0 && ( )} {flexRender( cell.column.columnDef.cell, cell.getContext() )}
{footer(table)}
); } export default Table;