-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckBoxFilter.tsx
73 lines (66 loc) · 1.69 KB
/
CheckBoxFilter.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
import * as React from 'react';
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { ICellRendererParams, IFilterParams } from 'ag-grid-community';
interface IProps extends IFilterParams {
keyValuePairs: { key: string; value: string }[];
}
export const CheckBoxFilter = forwardRef((props: IProps, ref: any) => {
const [filterState, setFilterState] = useState<string>(props.keyValuePairs[0].value);
// expose AG Grid Filter Lifecycle callbacks
useImperativeHandle(ref, () => ({
doesFilterPass(params: ICellRendererParams) {
const { field } = props.colDef;
// @ts-ignore
return params.data[field] === filterState;
},
isFilterActive() {
return true;
},
getModel() {
if (filterState === 'off') {
return undefined;
}
return {
filterType: 'checkBoxFilter',
values: [filterState],
};
},
setModel(model: {
filterType: string;
keyValuePairs: {
key: string;
value: string;
};
}) {
if (model === null) {
setFilterState(props.keyValuePairs[0].value);
} else {
setFilterState(model.keyValuePairs.value);
}
},
}));
const onYearChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setFilterState(event.target.value);
};
useEffect(() => {
props.filterChangedCallback();
}, [filterState]);
return (
<>
<div style={{ display: 'inline-block' }} onChange={onYearChange}>
{props.keyValuePairs.map((pair, index) => (
<label
key={pair.key}
style={{
margin: '10px',
display: 'inline-block',
}}
>
<input type="radio" name="status" value={pair.value} defaultChecked={index === 0} />
{pair.key}
</label>
))}
</div>
</>
);
});