/
MRT_SelectCheckbox.tsx
132 lines (122 loc) · 3.49 KB
/
MRT_SelectCheckbox.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { type MouseEvent } from 'react';
import Checkbox, { type CheckboxProps } from '@mui/material/Checkbox';
import Radio, { type RadioProps } from '@mui/material/Radio';
import Tooltip from '@mui/material/Tooltip';
import { type Theme } from '@mui/material/styles';
import {
type MRT_Row,
type MRT_RowData,
type MRT_TableInstance,
} from '../../types';
import {
getIsRowSelected,
getMRT_RowSelectionHandler,
getMRT_SelectAllHandler,
} from '../../utils/row.utils';
import { getCommonTooltipProps } from '../../utils/style.utils';
import { parseFromValuesOrFunc } from '../../utils/utils';
export interface MRT_SelectCheckboxProps<TData extends MRT_RowData>
extends CheckboxProps {
row?: MRT_Row<TData>;
staticRowIndex?: number;
table: MRT_TableInstance<TData>;
}
export const MRT_SelectCheckbox = <TData extends MRT_RowData>({
row,
staticRowIndex,
table,
...rest
}: MRT_SelectCheckboxProps<TData>) => {
const {
getState,
options: {
enableMultiRowSelection,
localization,
muiSelectAllCheckboxProps,
muiSelectCheckboxProps,
selectAllMode,
},
} = 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(muiSelectAllCheckboxProps, { table })
: parseFromValuesOrFunc(muiSelectCheckboxProps, {
row,
table,
})),
...rest,
};
const onSelectionChange = row
? getMRT_RowSelectionHandler({
row,
staticRowIndex,
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',
inputProps: {
'aria-label': selectAll
? localization.toggleSelectAll
: localization.toggleSelectRow,
},
onChange: (event) => {
event.stopPropagation();
row ? onSelectionChange!(event) : onSelectAllChange(event);
},
size: (density === 'compact' ? 'small' : 'medium') as 'medium' | 'small',
...checkboxProps,
onClick: (e: MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
checkboxProps?.onClick?.(e);
},
sx: (theme: Theme) => ({
height: density === 'compact' ? '1.75rem' : '2.5rem',
m: density !== 'compact' ? '-0.4rem' : undefined,
width: density === 'compact' ? '1.75rem' : '2.5rem',
zIndex: 0,
...parseFromValuesOrFunc(checkboxProps?.sx, theme),
}),
title: undefined,
} as CheckboxProps | RadioProps;
return (
<Tooltip
{...getCommonTooltipProps()}
title={
checkboxProps?.title ??
(selectAll
? localization.toggleSelectAll
: localization.toggleSelectRow)
}
>
{enableMultiRowSelection === false ? (
<Radio {...commonProps} />
) : (
<Checkbox
indeterminate={
!isChecked && selectAll
? table.getIsSomeRowsSelected()
: row?.getIsSomeSelected() && row.getCanSelectSubRows()
}
{...commonProps}
/>
)}
</Tooltip>
);
};