import { type MouseEvent } from 'react'; import { Checkbox, Radio, Switch, Tooltip, type CheckboxProps, type RadioProps, type SwitchProps, } from '@mantine/core'; import { type MRT_Row, type MRT_TableInstance } from '../types'; interface Props = {}> { row?: MRT_Row; selectAll?: boolean; table: MRT_TableInstance; } export const MRT_SelectCheckbox = = {}>({ row, selectAll, table, }: Props) => { const { getState, options: { enableMultiRowSelection, localization, mantineSelectAllCheckboxProps, mantineSelectCheckboxProps, selectAllMode, selectDisplayMode, }, } = table; const { density, isLoading } = getState(); const checkboxProps = !row ? mantineSelectAllCheckboxProps instanceof Function ? mantineSelectAllCheckboxProps({ table }) : mantineSelectAllCheckboxProps : mantineSelectCheckboxProps instanceof Function ? mantineSelectCheckboxProps({ row, table }) : mantineSelectCheckboxProps; const allRowsSelected = selectAll ? selectAllMode === 'page' ? table.getIsAllPageRowsSelected() : table.getIsAllRowsSelected() : undefined; const commonProps = { 'aria-label': selectAll ? localization.toggleSelectAll : localization.toggleSelectRow, checked: selectAll ? allRowsSelected : row?.getIsSelected(), disabled: isLoading || (row && !row.getCanSelect()), onChange: row ? row.getToggleSelectedHandler() : selectAllMode === 'all' ? table.getToggleAllRowsSelectedHandler() : table.getToggleAllPageRowsSelectedHandler(), 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 ? ( ) : ( )} ); };