import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons' import { chakra, Table, TableContainer, Tbody, Td, Th, Thead, Tr } from '@chakra-ui/react' import { flexRender, getCoreRowModel, getSortedRowModel, SortingState, useReactTable } from '@tanstack/react-table' import { AnimatePresence, motion } from 'framer-motion' import React, { useEffect } from 'react' import { CustomColumnDef } from './types' // import { useSticky } from 'react-table-sticky' export type DataTableProps = { data: Data[] columns: CustomColumnDef[] } export function DataTable({ data, columns }: DataTableProps) { const [sorting, setSorting] = React.useState([]) const [columnVisibility, setColumnVisibility] = React.useState({}) useEffect(() => { const visibleObject = {} for (let col of columns) { visibleObject[(col as any)?.accessorKey] = col?.isHidden ? false : true } setColumnVisibility(visibleObject) }, [columns]) const table = useReactTable({ columns, data, getCoreRowModel: getCoreRowModel(), onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), state: { sorting, columnVisibility, }, onColumnVisibilityChange: setColumnVisibility, }) return ( {table.getHeaderGroups().map((headerGroup) => { return ( {headerGroup.headers.map((header) => { const meta: any = header.column.columnDef.meta return ( ) })} ) })} {table.getRowModel().rows.map((row) => { return ( {row.getVisibleCells().map((cell) => { 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())}
) }