import * as React from 'react'; import { Table, Tbody, Td, Th, Thead, Tr, chakra } from '@chakra-ui/react'; import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons'; import { ColumnDef, SortingState, flexRender, getCoreRowModel, getSortedRowModel, useReactTable } from '@tanstack/react-table'; export type DataTableProps = { data: Data[]; columns: ColumnDef[]; }; export function DataTable({ data, columns }: DataTableProps) { const [sorting, setSorting] = React.useState([]); const table = useReactTable({ columns, data, getCoreRowModel: getCoreRowModel(), onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), state: { sorting } }); return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { // see https://tanstack.com/table/v8/docs/api/core/column-def#meta to type this correctly const meta: any = header.column.columnDef.meta; return ( ); })} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => { // see https://tanstack.com/table/v8/docs/api/core/column-def#meta to type this correctly const meta: any = cell.column.columnDef.meta; return ( ); })} ))}
{flexRender(header.column.columnDef.header, header.getContext())} {header.column.getIsSorted() ? ( header.column.getIsSorted() === 'desc' ? ( ) : ( ) ) : null}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
); }