import { type MouseEvent } from 'react'; import { Checkbox, type CheckboxProps, Radio, type RadioProps, Switch, type SwitchProps, Tooltip, } from '@mantine/core'; import { type MRT_Row, type MRT_RowData, type MRT_TableInstance, } from '../../types'; import { getIsRowSelected, getMRT_RowSelectionHandler, getMRT_SelectAllHandler, } from '../../utils/row.utils'; import { parseFromValuesOrFunc } from '../../utils/utils'; interface Props extends CheckboxProps { renderedRowIndex?: number; row?: MRT_Row; table: MRT_TableInstance; } export const MRT_SelectCheckbox = ({ renderedRowIndex = 0, row, table, ...rest }: Props) => { 'use no memo'; const { getState, options: { enableMultiRowSelection, localization, mantineSelectAllCheckboxProps, mantineSelectCheckboxProps, selectAllMode, selectDisplayMode, }, } = table; const { density, isLoading } = getState(); const selectAll = !row; const allRowsSelected = selectAll ? selectAllMode === 'page' ? table.getIsAllPageRowsSelected() : table.getIsAllRowsSelected() : undefined; const isChecked = selectAll ? allRowsSelected : getIsRowSelected({ row, table }); const checkboxProps = { ...(selectAll ? parseFromValuesOrFunc(mantineSelectAllCheckboxProps, { table }) : parseFromValuesOrFunc(mantineSelectCheckboxProps, { row, table, })), ...rest, }; const onSelectionChange = row ? getMRT_RowSelectionHandler({ renderedRowIndex, row, table, }) : undefined; const onSelectAllChange = getMRT_SelectAllHandler({ table }); const commonProps = { 'aria-label': selectAll ? localization.toggleSelectAll : localization.toggleSelectRow, checked: isChecked, disabled: isLoading || (row && !row.getCanSelect()) || row?.id === 'mrt-row-create', onChange: (event) => { event.stopPropagation(); if (selectAll) { onSelectAllChange(event); } else { onSelectionChange!(event); } }, size: density === 'xs' ? 'sm' : 'md', ...checkboxProps, onClick: (e: MouseEvent) => { e.stopPropagation(); checkboxProps?.onClick?.(e); }, title: undefined, } as CheckboxProps & RadioProps & SwitchProps; return ( {selectDisplayMode === 'switch' ? ( ) : selectDisplayMode === 'radio' || enableMultiRowSelection === false ? ( ) : ( )} ); };