2024-12-13 13:55:11 +03:00
|
|
|
'use no memo';
|
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type Cell, flexRender, type Row, type Table } from '@tanstack/react-table';
|
2024-06-07 20:17:03 +03:00
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-02-19 23:29:45 +03:00
|
|
|
import { SelectRow } from './SelectRow';
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type IConditionalStyle } from '.';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
interface TableBodyProps<TData> {
|
|
|
|
table: Table<TData>;
|
|
|
|
dense?: boolean;
|
|
|
|
noHeader?: boolean;
|
|
|
|
enableRowSelection?: boolean;
|
|
|
|
conditionalRowStyles?: IConditionalStyle<TData>[];
|
|
|
|
|
2025-02-19 22:32:50 +03:00
|
|
|
lastSelected: string | null;
|
|
|
|
onChangeLastSelected: (newValue: string | null) => void;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-22 14:03:13 +03:00
|
|
|
onRowClicked?: (rowData: TData, event: React.MouseEvent<Element>) => void;
|
|
|
|
onRowDoubleClicked?: (rowData: TData, event: React.MouseEvent<Element>) => void;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
2025-02-19 23:29:45 +03:00
|
|
|
export function TableBody<TData>({
|
2024-06-07 20:17:03 +03:00
|
|
|
table,
|
|
|
|
dense,
|
|
|
|
noHeader,
|
|
|
|
enableRowSelection,
|
|
|
|
conditionalRowStyles,
|
|
|
|
lastSelected,
|
2024-11-21 00:26:04 +03:00
|
|
|
onChangeLastSelected,
|
2024-06-07 20:17:03 +03:00
|
|
|
onRowClicked,
|
|
|
|
onRowDoubleClicked
|
|
|
|
}: TableBodyProps<TData>) {
|
2025-02-22 14:03:13 +03:00
|
|
|
function handleRowClicked(target: Row<TData>, event: React.MouseEvent<Element>) {
|
2024-12-04 22:52:45 +03:00
|
|
|
onRowClicked?.(target.original, event);
|
2024-06-07 20:17:03 +03:00
|
|
|
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
|
|
|
|
);
|
2024-08-06 14:38:10 +03:00
|
|
|
const newSelection: Record<string, boolean> = {};
|
2024-06-07 20:17:03 +03:00
|
|
|
toggleRows.forEach(row => {
|
|
|
|
newSelection[row.id] = !target.getIsSelected();
|
|
|
|
});
|
|
|
|
table.setRowSelection(prev => ({ ...prev, ...newSelection }));
|
2025-02-19 22:32:50 +03:00
|
|
|
onChangeLastSelected(null);
|
2024-06-07 20:17:03 +03:00
|
|
|
} else {
|
2024-11-21 00:26:04 +03:00
|
|
|
onChangeLastSelected(target.id);
|
2024-06-07 20:17:03 +03:00
|
|
|
target.toggleSelected(!target.getIsSelected());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRowStyles(row: Row<TData>) {
|
|
|
|
return {
|
|
|
|
...conditionalRowStyles!
|
|
|
|
.filter(item => item.when(row.original))
|
|
|
|
.reduce((prev, item) => ({ ...prev, ...item.style }), {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tbody>
|
|
|
|
{table.getRowModel().rows.map((row: Row<TData>, index) => (
|
|
|
|
<tr
|
|
|
|
key={row.id}
|
|
|
|
className={clsx(
|
2024-12-17 11:37:42 +03:00
|
|
|
'cc-scroll-row',
|
|
|
|
'clr-hover cc-animate-color',
|
2024-06-07 20:17:03 +03:00
|
|
|
!noHeader && 'scroll-mt-[calc(2px+2rem)]',
|
2025-03-09 21:57:21 +03:00
|
|
|
row.getIsSelected() ? 'clr-selected' : 'odd:bg-prim-200 even:bg-prim-100'
|
2024-06-07 20:17:03 +03:00
|
|
|
)}
|
|
|
|
style={{ ...(conditionalRowStyles ? getRowStyles(row) : []) }}
|
|
|
|
>
|
|
|
|
{enableRowSelection ? (
|
2025-03-09 21:57:21 +03:00
|
|
|
<td key={`select-${row.id}`} className='pl-3 pr-1 border-y'>
|
2024-11-21 00:26:04 +03:00
|
|
|
<SelectRow row={row} onChangeLastSelected={onChangeLastSelected} />
|
2024-06-07 20:17:03 +03:00
|
|
|
</td>
|
|
|
|
) : null}
|
|
|
|
{row.getVisibleCells().map((cell: Cell<TData, unknown>) => (
|
|
|
|
<td
|
|
|
|
key={cell.id}
|
|
|
|
className='px-2 align-middle border-y'
|
|
|
|
style={{
|
|
|
|
cursor: onRowClicked || onRowDoubleClicked ? 'pointer' : 'auto',
|
|
|
|
paddingBottom: dense ? '0.25rem' : '0.5rem',
|
2024-09-27 15:28:52 +03:00
|
|
|
paddingTop: dense ? '0.25rem' : '0.5rem',
|
|
|
|
width: noHeader && index === 0 ? `calc(var(--col-${cell.column.id}-size) * 1px)` : 'auto'
|
2024-06-07 20:17:03 +03:00
|
|
|
}}
|
|
|
|
onClick={event => handleRowClicked(row, event)}
|
2025-02-19 22:32:50 +03:00
|
|
|
onDoubleClick={event => onRowDoubleClicked?.(row.original, event)}
|
2024-06-07 20:17:03 +03:00
|
|
|
>
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
</td>
|
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
);
|
|
|
|
}
|