ConceptPortal-public/rsconcept/frontend/src/components/ui/DataTable/TableBody.tsx

104 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-12-17 20:19:28 +03:00
import { Cell, flexRender, Row, Table } from '@tanstack/react-table';
import { IConditionalStyle } from '.';
import SelectRow from './SelectRow';
interface TableBodyProps<TData> {
2023-12-28 14:04:44 +03:00
table: Table<TData>;
dense?: boolean;
enableRowSelection?: boolean;
conditionalRowStyles?: IConditionalStyle<TData>[];
2024-03-25 23:10:29 +03:00
lastSelected: string | undefined;
setLastSelected: React.Dispatch<React.SetStateAction<string | undefined>>;
2023-12-28 14:04:44 +03:00
onRowClicked?: (rowData: TData, event: React.MouseEvent<Element, MouseEvent>) => void;
onRowDoubleClicked?: (rowData: TData, event: React.MouseEvent<Element, MouseEvent>) => void;
2023-12-17 20:19:28 +03:00
}
function TableBody<TData>({
2023-12-28 14:04:44 +03:00
table,
dense,
2023-12-17 20:19:28 +03:00
enableRowSelection,
conditionalRowStyles,
2024-03-25 23:10:29 +03:00
lastSelected,
setLastSelected,
2023-12-28 14:04:44 +03:00
onRowClicked,
onRowDoubleClicked
2023-12-17 20:19:28 +03:00
}: TableBodyProps<TData>) {
2024-03-25 23:10:29 +03:00
function handleRowClicked(target: Row<TData>, event: React.MouseEvent<Element, MouseEvent>) {
2023-12-17 20:19:28 +03:00
if (onRowClicked) {
2024-03-25 23:10:29 +03:00
onRowClicked(target.original, event);
2023-12-17 20:19:28 +03:00
}
2024-03-25 23:10:29 +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
);
const newSelection: { [key: string]: boolean } = {};
toggleRows.forEach(row => {
newSelection[row.id] = !target.getIsSelected();
});
table.setRowSelection(prev => ({ ...prev, ...newSelection }));
setLastSelected(undefined);
} else {
setLastSelected(target.id);
target.toggleSelected(!target.getIsSelected());
}
2023-12-17 20:19:28 +03:00
}
}
function getRowStyles(row: Row<TData>) {
2023-12-28 14:04:44 +03:00
return {
...conditionalRowStyles!
.filter(item => item.when(row.original))
.reduce((prev, item) => ({ ...prev, ...item.style }), {})
};
2023-12-17 20:19:28 +03:00
}
return (
<tbody>
2023-12-28 14:04:44 +03:00
{table.getRowModel().rows.map((row: Row<TData>, index) => (
<tr
key={row.id}
className={
row.getIsSelected()
? 'clr-selected clr-hover'
: index % 2 === 0
? 'clr-controls clr-hover'
: 'clr-app clr-hover'
}
style={conditionalRowStyles && getRowStyles(row)}
>
{enableRowSelection ? (
<td key={`select-${row.id}`} className='pl-3 pr-1 align-middle border-y'>
2024-03-25 23:10:29 +03:00
<SelectRow row={row} setLastSelected={setLastSelected} />
2023-12-28 14:04:44 +03:00
</td>
) : null}
{row.getVisibleCells().map((cell: Cell<TData, unknown>) => (
<td
key={cell.id}
className='px-2 align-middle border-y'
2023-12-28 14:04:44 +03:00
style={{
cursor: onRowClicked || onRowDoubleClicked ? 'pointer' : 'auto',
paddingBottom: dense ? '0.25rem' : '0.5rem',
paddingTop: dense ? '0.25rem' : '0.5rem'
}}
onClick={event => handleRowClicked(row, event)}
onDoubleClick={event => (onRowDoubleClicked ? onRowDoubleClicked(row.original, event) : undefined)}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
);
2023-12-17 20:19:28 +03:00
}
2023-12-28 14:04:44 +03:00
export default TableBody;