import { flexRender } from "@tanstack/react-table"; import clsx from "clsx"; import { PropsWithChildren } from "react"; import type { JSONRecord } from "../../interfaces/JSONRecord.js"; import { getComponent } from "../../registries/components"; import type { Pagination as DefaultPagination } from "../pagination/Pagination"; import type { DefaultCellFooter } from "./components/DefaultCellFooter"; import type { DefaultCellHeader } from "./components/DefaultCellHeader"; import { useTable, UseTableProps } from "./hooks/useTable"; export interface TableProps extends UseTableProps { className?: string; enableFooter?: boolean; enablePagination?: boolean; } export function Table({ className, enableFooter, enablePagination = true, children, ...props }: PropsWithChildren>) { const { tableInstance, i18n } = useTable(props); const CellHeader = getComponent("CellHeader"); const CellFooter = getComponent("CellFooter"); const Pagination = getComponent("Pagination"); const { pagination } = tableInstance.getState(); return (
{tableInstance.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { const sort = header.column.getIsSorted(); return ( ); })} ))} {tableInstance.getRowModel().rows.map((row) => { return ( {row .getVisibleCells() .filter((cell) => !cell.column.columnDef.meta?.hidden) .map((cell) => { return ( ); })} ); })} {enableFooter && ( {tableInstance.getFooterGroups().map((footerGroup) => ( {footerGroup.headers.map((header) => ( ))} ))} )}
{header.isPlaceholder ? null : }
props.onClick?.(cell.row.original, { action: "row" })} {...cell.column.columnDef?.meta?.cellProps} key={cell.id} data-testid={`body-cell-${cell.id}`} > {flexRender(cell.column.columnDef.cell, cell.getContext())}
{header.isPlaceholder ? null : }
{props.data.length && pagination && enablePagination ? ( tableInstance.setPageIndex(page)} onClickPreviousPage={() => tableInstance.previousPage()} onClickNextPage={() => tableInstance.nextPage()} onPageSizeChange={(pageSize) => tableInstance.setPageSize(pageSize)} /> ) : null}
{children}
); }