-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateRangeFilter.tsx
74 lines (62 loc) · 1.83 KB
/
DateRangeFilter.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
/* eslint-disable @typescript-eslint/no-unused-vars */
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { IFilterParams } from 'ag-grid-community';
import { IFilterReactComp } from 'ag-grid-react';
import { makeStyles } from '@material-ui/core';
import { DateRangePicker, Range } from 'react-date-range';
// import { DateRangePicker } from 'materialui-daterange-picker';
const useStyles = makeStyles({ wrapper: {} });
export const DateRangeFilter = forwardRef((props: IFilterParams, ref: any) => {
const [range, setRange] = useState<Range>({ key: 'selection' });
const classes = useStyles();
// expose AG Grid Filter Lifecycle callbacks
useImperativeHandle<any, IFilterReactComp>(ref, () => ({
doesFilterPass(params) {
const { api, colDef, column, columnApi, context } = props;
const { node } = params;
const value = props.valueGetter({
api,
colDef,
column,
columnApi,
context,
data: node.data,
getValue: (field) => node.data[field],
node,
});
if (range === undefined || !range.startDate) return false;
return !!(range.startDate && value > range.startDate && range.endDate && range.endDate > value);
},
isFilterActive() {
return !!range && !!range.startDate;
},
getModel() {
if (!this.isFilterActive()) {
return null;
}
return {
startDate: range?.startDate,
endDate: range?.endDate,
filterType: 'date',
};
},
setModel(model: Range) {
setRange(model);
},
}));
useEffect(() => {
props.filterChangedCallback();
}, [props, range]);
return (
<div>
<DateRangePicker
editableDateInputs={true}
onChange={(item) => setRange(item.selection)}
moveRangeOnFirstSelection={false}
showPreview
ranges={[range]}
/>
<button onClick={() => setRange({ key: 'selection' })}>Reset</button>
</div>
);
});