"use client"; import { flexRender, getCoreRowModel, useReactTable, type ColumnDef, type RowSelectionState, } from "@tanstack/react-table"; import { useState } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; import { useI18n } from "@/i18n/i18n-context"; interface DataTableProps { columns: ColumnDef[]; data: TData[]; onRowClick?: (row: TData) => void; rowSelection?: RowSelectionState; onRowSelectionChange?: ( updater: RowSelectionState | ((prev: RowSelectionState) => RowSelectionState), ) => void; getRowId?: (row: TData) => string; } export function DataTable({ columns, data, onRowClick, rowSelection: externalRowSelection, onRowSelectionChange: externalOnRowSelectionChange, getRowId, }: DataTableProps) { const { t } = useI18n(); const [internalRowSelection, setInternalRowSelection] = useState({}); const rowSelection = externalRowSelection ?? internalRowSelection; const setRowSelection = externalOnRowSelectionChange ?? setInternalRowSelection; const table = useReactTable({ data, columns, state: { rowSelection }, onRowSelectionChange: setRowSelection, getCoreRowModel: getCoreRowModel(), enableRowSelection: true, ...(getRowId ? { getRowId } : {}), }); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( onRowClick?.(row.original)} className={cn( "h-12 transition-colors hover:bg-accent data-[state=selected]:bg-primary/5", onRowClick && "cursor-pointer", )} > {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( {t("common.noData")} )}
); }