import { flexRender, getCoreRowModel, useReactTable, type ColumnDef, } from "@tanstack/react-table"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { ScrollArea } from "@/components/ui/scroll-area"; import { useEffect } from "react"; interface VersionTableProps { data: TData[]; columns: ColumnDef[]; isLoading: boolean; onTableInit?: (table: any) => void; onRowClick?: (row: TData) => void; scrollable?: boolean; maxHeight?: string; } export function VersionTable({ data, columns, isLoading, onTableInit, onRowClick, scrollable = false, maxHeight = "calc(100vh - 320px)", }: VersionTableProps) { const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getRowId: (row: any) => row.id?.toString(), enableRowSelection: true, }); useEffect(() => { onTableInit?.(table); }, [table, onTableInit]); const tableContent = ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {isLoading ? ( Đang tải dữ liệu... ) : table.getRowModel().rows.length === 0 ? ( Không có dữ liệu. ) : ( table.getRowModel().rows.map((row) => ( onRowClick?.(row.original)} className={onRowClick ? "cursor-pointer hover:bg-muted/50" : ""} > {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) )}
); if (scrollable) { return (
{tableContent}
); } return
{tableContent}
; }