'use no memo'; import { Cell, flexRender, Row, Table } from '@tanstack/react-table'; import clsx from 'clsx'; import { CProps } from '../props'; import { SelectRow } from './SelectRow'; import { IConditionalStyle } from '.'; interface TableBodyProps { table: Table; dense?: boolean; noHeader?: boolean; enableRowSelection?: boolean; conditionalRowStyles?: IConditionalStyle[]; lastSelected: string | null; onChangeLastSelected: (newValue: string | null) => void; onRowClicked?: (rowData: TData, event: CProps.EventMouse) => void; onRowDoubleClicked?: (rowData: TData, event: CProps.EventMouse) => void; } export function TableBody({ table, dense, noHeader, enableRowSelection, conditionalRowStyles, lastSelected, onChangeLastSelected, onRowClicked, onRowDoubleClicked }: TableBodyProps) { function handleRowClicked(target: Row, event: CProps.EventMouse) { onRowClicked?.(target.original, event); if (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()); } } } function getRowStyles(row: Row) { return { ...conditionalRowStyles! .filter(item => item.when(row.original)) .reduce((prev, item) => ({ ...prev, ...item.style }), {}) }; } return ( {table.getRowModel().rows.map((row: Row, index) => ( {enableRowSelection ? ( ) : null} {row.getVisibleCells().map((cell: Cell) => ( handleRowClicked(row, event)} onDoubleClick={event => onRowDoubleClicked?.(row.original, event)} > {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} ))} ); }