/
MRT_TableHeadCellFilterLabel.tsx
180 lines (170 loc) · 5.52 KB
/
MRT_TableHeadCellFilterLabel.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { type MouseEvent, useState } from 'react';
import Box from '@mui/material/Box';
import Grow from '@mui/material/Grow';
import IconButton, { type IconButtonProps } from '@mui/material/IconButton';
import Popover from '@mui/material/Popover';
import Tooltip from '@mui/material/Tooltip';
import { MRT_TableHeadCellFilterContainer } from './MRT_TableHeadCellFilterContainer';
import {
type MRT_Header,
type MRT_RowData,
type MRT_TableInstance,
} from '../../types';
import {
getColumnFilterInfo,
useDropdownOptions,
} from '../../utils/column.utils';
import { getValueAndLabel, parseFromValuesOrFunc } from '../../utils/utils';
export interface MRT_TableHeadCellFilterLabelProps<TData extends MRT_RowData>
extends IconButtonProps {
header: MRT_Header<TData>;
table: MRT_TableInstance<TData>;
}
export const MRT_TableHeadCellFilterLabel = <TData extends MRT_RowData = {}>({
header,
table,
...rest
}: MRT_TableHeadCellFilterLabelProps<TData>) => {
const {
options: {
columnFilterDisplayMode,
icons: { FilterAltIcon },
localization,
},
refs: { filterInputRefs },
setShowColumnFilters,
} = table;
const { column } = header;
const { columnDef } = column;
const filterValue = column.getFilterValue() as [string, string] | string;
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
const {
currentFilterOption,
isMultiSelectFilter,
isRangeFilter,
isSelectFilter,
} = getColumnFilterInfo({ header, table });
const dropdownOptions = useDropdownOptions({ header, table });
const getSelectLabel = (index?: number) =>
getValueAndLabel(
dropdownOptions?.find(
(option) =>
getValueAndLabel(option).value ===
(index !== undefined ? filterValue[index] : filterValue),
),
).label;
const isFilterActive =
(Array.isArray(filterValue) && filterValue.some(Boolean)) ||
(!!filterValue && !Array.isArray(filterValue));
const filterTooltip =
columnFilterDisplayMode === 'popover' && !isFilterActive
? localization.filterByColumn?.replace(
'{column}',
String(columnDef.header),
)
: localization.filteringByColumn
.replace('{column}', String(columnDef.header))
.replace(
'{filterType}',
currentFilterOption
? // @ts-ignore
localization[
`filter${
currentFilterOption?.charAt(0)?.toUpperCase() +
currentFilterOption?.slice(1)
}`
]
: '',
)
.replace(
'{filterValue}',
`"${
Array.isArray(filterValue)
? (filterValue as [string, string])
.map((value, index) =>
isMultiSelectFilter ? getSelectLabel(index) : value,
)
.join(
`" ${isRangeFilter ? localization.and : localization.or} "`,
)
: isSelectFilter
? getSelectLabel()
: (filterValue as string)
}"`,
)
.replace('" "', '');
return (
<>
<Grow
in={
columnFilterDisplayMode === 'popover' ||
(!!filterValue && !isRangeFilter) ||
(isRangeFilter && // @ts-ignore
(!!filterValue?.[0] || !!filterValue?.[1]))
}
unmountOnExit
>
<Box component="span" sx={{ flex: '0 0' }}>
<Tooltip placement="top" title={filterTooltip}>
<IconButton
disableRipple
onClick={(event: MouseEvent<HTMLButtonElement>) => {
if (columnFilterDisplayMode === 'popover') {
setAnchorEl(event.currentTarget);
} else {
setShowColumnFilters(true);
}
queueMicrotask(() => {
filterInputRefs.current[`${column.id}-0`]?.focus?.();
filterInputRefs.current[`${column.id}-0`]?.select?.();
});
event.stopPropagation();
}}
size="small"
{...rest}
sx={(theme) => ({
height: '16px',
ml: '4px',
opacity: isFilterActive ? 1 : 0.3,
p: '8px',
transform: 'scale(0.75)',
transition: 'all 150ms ease-in-out',
width: '16px',
...(parseFromValuesOrFunc(rest?.sx, theme) as any),
})}
>
<FilterAltIcon />
</IconButton>
</Tooltip>
</Box>
</Grow>
{columnFilterDisplayMode === 'popover' && (
<Popover
anchorEl={anchorEl}
anchorOrigin={{
horizontal: 'center',
vertical: 'top',
}}
disableScrollLock
onClick={(event) => event.stopPropagation()}
onClose={(event) => {
//@ts-ignore
event.stopPropagation();
setAnchorEl(null);
}}
onKeyDown={(event) => event.key === 'Enter' && setAnchorEl(null)}
open={!!anchorEl}
slotProps={{ paper: { sx: { overflow: 'visible' } } }}
transformOrigin={{
horizontal: 'center',
vertical: 'bottom',
}}
>
<Box sx={{ p: '1rem' }}>
<MRT_TableHeadCellFilterContainer header={header} table={table} />
</Box>
</Popover>
)}
</>
);
};