-
Notifications
You must be signed in to change notification settings - Fork 764
Expand file tree
/
Copy pathDate.js
More file actions
155 lines (141 loc) · 4.16 KB
/
Date.js
File metadata and controls
155 lines (141 loc) · 4.16 KB
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
import React, { Component } from "react";
import PropTypes from "prop-types";
import Const from "../Const";
const legalComparators = ["=", ">", ">=", "<", "<=", "!="];
function dateParser(d) {
return `${d.getFullYear()}-${("0" + (d.getMonth() + 1)).slice(-2)}-${(
"0" + d.getDate()
).slice(-2)}`;
}
class DateFilter extends Component {
constructor(props) {
super(props);
this.dateComparators = this.props.dateComparators || legalComparators;
}
setDefaultDate() {
let defaultDate = "";
const { defaultValue } = this.props;
if (defaultValue && defaultValue.date) {
defaultDate = dateParser(new Date(defaultValue.date));
}
return defaultDate;
}
onChangeComparator(event) {
let date = this.inputDate.value;
const comparator = event.target.value;
if (date === "") {
return;
}
date = new Date(date);
this.props.filterHandler({ date, comparator }, Const.FILTER_TYPE.DATE);
}
getComparatorOptions() {
return [
<option key="-1"></option>,
...this.dateComparators.map((comparator, index) => (
<option key={index} value={comparator}>
{comparator}
</option>
)),
];
}
filter = (event) => {
const comparator = this.dateFilterComparator.value;
const dateValue = event.target.value;
if (dateValue) {
this.props.filterHandler(
{ date: new Date(dateValue), comparator },
Const.FILTER_TYPE.DATE
);
} else {
this.props.filterHandler(null, Const.FILTER_TYPE.DATE);
}
};
cleanFiltered = () => {
const value = this.setDefaultDate();
const comparator = this.props.defaultValue
? this.props.defaultValue.comparator
: "";
this.setState(() => ({ isPlaceholderSelected: value === "" }));
this.dateFilterComparator.value = comparator;
this.inputDate.value = value;
this.props.filterHandler(
{ date: new Date(value), comparator },
Const.FILTER_TYPE.DATE
);
};
applyFilter(filterDateObj) {
const { date, comparator } = filterDateObj;
this.setState(() => ({ isPlaceholderSelected: date === "" }));
this.dateFilterComparator.value = comparator;
this.inputDate.value = dateParser(date);
this.props.filterHandler({ date, comparator }, Const.FILTER_TYPE.DATE);
}
componentDidMount() {
const { dateFilterComparator, inputDate, props } = this;
const comparator = dateFilterComparator.value;
const dateValue = inputDate.value;
if (comparator && dateValue) {
props.filterHandler(
{ date: new Date(dateValue), comparator },
Const.FILTER_TYPE.DATE
);
}
}
render() {
const { defaultValue, style } = this.props;
const { date, comparator } = style;
return (
<div className="filter date-filter">
<select
ref={(n) => (this.dateFilterComparator = n)}
style={comparator}
className="date-filter-comparator form-control"
onChange={this.onChangeComparator}
defaultValue={defaultValue ? defaultValue.comparator : ""}
>
{this.getComparatorOptions()}
</select>
<input
ref={(n) => (this.inputDate = n)}
className="filter date-filter-input form-control"
style={date}
type="date"
onChange={this.filter}
defaultValue={this.setDefaultDate()}
/>
</div>
);
}
}
DateFilter.propTypes = {
filterHandler: PropTypes.func.isRequired,
defaultValue: PropTypes.shape({
date: PropTypes.object,
comparator: PropTypes.oneOf(legalComparators),
}),
style: PropTypes.shape({
date: PropTypes.oneOfType([PropTypes.object]),
comparator: PropTypes.oneOfType([PropTypes.object]),
}),
dateComparators: function (props, propName) {
if (!props[propName]) {
return;
}
for (let i = 0; i < props[propName].length; i++) {
if (!legalComparators.includes(props[propName][i])) {
return new Error(
`Date comparator provided is not supported. Use only ${legalComparators}`
);
}
}
},
columnName: PropTypes.any,
};
DateFilter.defaultProps = {
style: {
date: null,
comparator: null,
},
};
export default DateFilter;