import { ColumnDef, flexRender, getCoreRowModel, useReactTable, createColumnHelper, getExpandedRowModel, } from '@tanstack/react-table'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Empty } from 'antd'; import React from 'react'; export type { ColumnDef }; export { createColumnHelper }; interface DataTableProps { columns: ColumnDef[]; data: TData[]; ExpandComponent?: React.ComponentType<{ row: TData }>; } export function DataTable({ columns, data, ExpandComponent, }: DataTableProps) { const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), }); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ); })} ))} {table.getRowModel().rows?.length > 0 ? ( table.getRowModel().rows.map((row) => { const renderedRow = ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ))} ); if (row.getIsExpanded() && ExpandComponent) { return ( <> {renderedRow} ); } else { return renderedRow; } }) ) : ( )}
); }