2025-09-10 09:59:17 +07:00
|
|
|
import {
|
|
|
|
|
flexRender,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
useReactTable,
|
|
|
|
|
type ColumnDef,
|
|
|
|
|
} from "@tanstack/react-table";
|
|
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
} from "@/components/ui/table";
|
2026-01-18 22:52:19 +07:00
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
2025-09-10 09:59:17 +07:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
|
|
interface VersionTableProps<TData> {
|
|
|
|
|
data: TData[];
|
|
|
|
|
columns: ColumnDef<TData, any>[];
|
|
|
|
|
isLoading: boolean;
|
2025-12-02 11:03:15 +07:00
|
|
|
onTableInit?: (table: any) => void;
|
2026-01-18 22:52:19 +07:00
|
|
|
onRowClick?: (row: TData) => void;
|
|
|
|
|
scrollable?: boolean;
|
|
|
|
|
maxHeight?: string;
|
2025-09-10 09:59:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function VersionTable<TData>({
|
|
|
|
|
data,
|
|
|
|
|
columns,
|
|
|
|
|
isLoading,
|
|
|
|
|
onTableInit,
|
2026-01-18 22:52:19 +07:00
|
|
|
onRowClick,
|
|
|
|
|
scrollable = false,
|
|
|
|
|
maxHeight = "calc(100vh - 320px)",
|
2025-09-10 09:59:17 +07:00
|
|
|
}: VersionTableProps<TData>) {
|
|
|
|
|
const table = useReactTable({
|
|
|
|
|
data,
|
|
|
|
|
columns,
|
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
getRowId: (row: any) => row.id?.toString(),
|
|
|
|
|
enableRowSelection: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onTableInit?.(table);
|
|
|
|
|
}, [table, onTableInit]);
|
|
|
|
|
|
2026-01-18 22:52:19 +07:00
|
|
|
const tableContent = (
|
2025-09-10 09:59:17 +07:00
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
|
<TableRow key={headerGroup.id}>
|
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
|
<TableHead key={header.id}>
|
|
|
|
|
{header.isPlaceholder
|
|
|
|
|
? null
|
|
|
|
|
: flexRender(
|
|
|
|
|
header.column.columnDef.header,
|
|
|
|
|
header.getContext()
|
|
|
|
|
)}
|
|
|
|
|
</TableHead>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell colSpan={columns.length}>Đang tải dữ liệu...</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
) : table.getRowModel().rows.length === 0 ? (
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell colSpan={columns.length}>Không có dữ liệu.</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
) : (
|
|
|
|
|
table.getRowModel().rows.map((row) => (
|
|
|
|
|
<TableRow
|
|
|
|
|
key={row.id}
|
|
|
|
|
data-state={row.getIsSelected() && "selected"}
|
2026-01-18 22:52:19 +07:00
|
|
|
onClick={() => onRowClick?.(row.original)}
|
|
|
|
|
className={onRowClick ? "cursor-pointer hover:bg-muted/50" : ""}
|
2025-09-10 09:59:17 +07:00
|
|
|
>
|
|
|
|
|
{row.getVisibleCells().map((cell) => (
|
|
|
|
|
<TableCell key={cell.id}>
|
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
|
</TableCell>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
);
|
2026-01-18 22:52:19 +07:00
|
|
|
|
|
|
|
|
if (scrollable) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="rounded-md border">
|
|
|
|
|
<ScrollArea className="w-full" style={{ height: maxHeight }}>
|
|
|
|
|
{tableContent}
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <div className="rounded-md border">{tableContent}</div>;
|
2025-09-10 09:59:17 +07:00
|
|
|
}
|