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>[];
|
|
|
|
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,
|
2023-12-28 14:04:44 +03:00
|
|
|
onRowClicked,
|
|
|
|
onRowDoubleClicked
|
2023-12-17 20:19:28 +03:00
|
|
|
}: TableBodyProps<TData>) {
|
|
|
|
function handleRowClicked(row: Row<TData>, event: React.MouseEvent<Element, MouseEvent>) {
|
|
|
|
if (onRowClicked) {
|
|
|
|
onRowClicked(row.original, event);
|
|
|
|
}
|
|
|
|
if (enableRowSelection && row.getCanSelect()) {
|
|
|
|
row.getToggleSelectedHandler()(!row.getIsSelected());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 border-y'>
|
|
|
|
<SelectRow row={row} />
|
|
|
|
</td>
|
|
|
|
) : null}
|
|
|
|
{row.getVisibleCells().map((cell: Cell<TData, unknown>) => (
|
|
|
|
<td
|
|
|
|
key={cell.id}
|
|
|
|
className='px-2 border-y'
|
|
|
|
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;
|