'use client'; 'use no memo'; import { useCallback } from 'react'; import { type Cell, flexRender, type Row, type Table } from '@tanstack/react-table'; import clsx from 'clsx'; import { cn } from '../utils'; import { SelectRow } from './select-row'; interface TableRowProps { table: Table; row: Row; className?: string; style?: React.CSSProperties; noHeader?: boolean; dense?: boolean; lastSelected: string | null; onChangeLastSelected: (newValue: string | null) => void; onRowClicked?: (rowData: TData, event: React.MouseEvent) => void; onRowDoubleClicked?: (rowData: TData, event: React.MouseEvent) => void; } export function TableRow({ table, row, className, style, noHeader, dense, lastSelected, onChangeLastSelected, onRowClicked, onRowDoubleClicked }: TableRowProps) { const hasBG = className?.includes('bg-') ?? false; const handleRowClicked = useCallback( (target: Row, event: React.MouseEvent) => { onRowClicked?.(target.original, event); if (table.options.enableRowSelection && target.getCanSelect()) { if (event.shiftKey && !!lastSelected && lastSelected !== target.id) { const { rows, rowsById } = table.getRowModel(); const lastIndex = rowsById[lastSelected].index; const currentIndex = target.index; const toggleRows = rows.slice( lastIndex > currentIndex ? currentIndex : lastIndex + 1, lastIndex > currentIndex ? lastIndex : currentIndex + 1 ); const newSelection: Record = {}; toggleRows.forEach(row => { newSelection[row.id] = !target.getIsSelected(); }); table.setRowSelection(prev => ({ ...prev, ...newSelection })); onChangeLastSelected(null); } else { onChangeLastSelected(target.id); target.toggleSelected(!target.getIsSelected()); } } }, [table, lastSelected, onChangeLastSelected, onRowClicked] ); return ( handleRowClicked(row, event)} onDoubleClick={event => onRowDoubleClicked?.(row.original, event)} > {table.options.enableRowSelection ? ( ) : null} {row.getVisibleCells().map((cell: Cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} ); }