-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
gridSingleSelectOperators.ts
95 lines (86 loc) · 3.05 KB
/
gridSingleSelectOperators.ts
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
import { GridFilterInputSingleSelect } from '../components/panel/filterPanel/GridFilterInputSingleSelect';
import { GridFilterOperator } from '../models/gridFilterOperator';
import { GridFilterItem } from '../models/gridFilterItem';
import { GridFilterInputMultipleSingleSelect } from '../components/panel/filterPanel/GridFilterInputMultipleSingleSelect';
import { GridCellParams, GridColDef } from '../models';
import { GridApiCommunity } from '../models/api/gridApiCommunity';
const parseObjectValue = (value: GridFilterItem) => {
if (value == null || typeof value !== 'object') {
return value;
}
return value.value;
};
export const getGridSingleSelectQuickFilterFn = (
value: any,
column: GridColDef,
apiRef: React.MutableRefObject<GridApiCommunity>,
) => {
if (!value) {
return null;
}
const { valueOptions, valueFormatter, field } = column;
const potentialValues = [parseObjectValue(value).toString()];
const iterableColumnValues =
typeof valueOptions === 'function' ? valueOptions({ field }) : valueOptions || [];
if (iterableColumnValues) {
iterableColumnValues.forEach((option) => {
// for each valueOption, check if the formatted value
let optionValue;
let optionLabel;
if (typeof option === 'object') {
optionValue = option.value;
optionLabel = option.label;
} else {
optionValue = option;
if (valueFormatter) {
optionLabel = valueFormatter({ value: option, field, api: apiRef.current });
} else {
optionLabel = option;
}
}
if (optionLabel.slice(0, value.length).toLowerCase() === value.toLowerCase()) {
if (!potentialValues.includes(optionValue)) {
potentialValues.push(optionValue.toString());
}
}
});
}
return ({ value: columnValue }: GridCellParams): boolean => {
return columnValue != null
? potentialValues.includes(parseObjectValue(columnValue).toString())
: false;
};
};
export const getGridSingleSelectOperators = (): GridFilterOperator[] => [
{
value: 'is',
getApplyFilterFn: (filterItem) => {
if (filterItem.value == null || filterItem.value === '') {
return null;
}
return ({ value }): boolean => parseObjectValue(value) === parseObjectValue(filterItem.value);
},
InputComponent: GridFilterInputSingleSelect,
},
{
value: 'not',
getApplyFilterFn: (filterItem) => {
if (filterItem.value == null || filterItem.value === '') {
return null;
}
return ({ value }): boolean => parseObjectValue(value) !== parseObjectValue(filterItem.value);
},
InputComponent: GridFilterInputSingleSelect,
},
{
value: 'isAnyOf',
getApplyFilterFn: (filterItem) => {
if (!Array.isArray(filterItem.value) || filterItem.value.length === 0) {
return null;
}
const filterItemValues = filterItem.value.map(parseObjectValue);
return ({ value }): boolean => filterItemValues.includes(parseObjectValue(value));
},
InputComponent: GridFilterInputMultipleSingleSelect,
},
];