-
Notifications
You must be signed in to change notification settings - Fork 37
/
DateRangeWrapper.js
98 lines (83 loc) · 2.54 KB
/
DateRangeWrapper.js
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
import React from 'react';
import PropTypes from 'prop-types';
const callAll = (...funcs) => (...args) => funcs.forEach(fn => fn && fn(...args));
const ISOFormat = 'YYYY-MM-DDTHH:mm:ss.sssZ';
class DateRangeWrapper extends React.Component {
static propTypes = {
children: PropTypes.func,
endExcluder: PropTypes.func,
endValueGetter: PropTypes.func,
initialEndDate: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
]),
initialStartDate: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
]),
startExcluder: PropTypes.func,
startValueGetter: PropTypes.func,
}
static defaultProps = {
endExcluder: (day, startDate) => day.isBefore(startDate, ISOFormat),
startExcluder: (day, endDate) => day.isAfter(endDate, ISOFormat),
startValueGetter: (e) => e.target.value,
endValueGetter: (e) => e.target.value,
}
constructor(props) {
super(props);
this.state = {
startDate: this.props.initialStartDate || null,
endDate: this.props.initialEndDate || null,
};
this.endDateExclude = this.endDateExclude.bind(this);
this.startDateExclude = this.startDateExclude.bind(this);
this.startDateOnChange = this.startDateOnChange.bind(this);
this.endDateOnChange = this.endDateOnChange.bind(this);
this.getStartInputProps = this.getStartInputProps.bind(this);
this.getEndInputProps = this.getEndInputProps.bind(this);
}
startDateOnChange(...args) {
const newStartDateValue = this.props.startValueGetter(...args);
this.setState({ startDate: newStartDateValue });
}
endDateOnChange(...args) {
const newEndDateValue = this.props.endValueGetter(...args);
this.setState({ endDate: newEndDateValue });
}
endDateExclude(day) {
const isExcluded = this.props.endExcluder(day, this.state.startDate);
return isExcluded;
}
startDateExclude(day) {
const isExcluded = this.props.startExcluder(day, this.state.endDate);
return isExcluded;
}
getStartInputProps(props = {}) {
return {
...props,
onChange: callAll(props.onChange, this.startDateOnChange),
};
}
getEndInputProps(props = {}) {
return {
...props,
onChange: callAll(props.onChange, this.endDateOnChange),
};
}
render() {
const {
getStartInputProps,
getEndInputProps,
endDateExclude,
startDateExclude,
} = this;
return this.props.children({
getStartInputProps,
getEndInputProps,
endDateExclude,
startDateExclude,
});
}
}
export default DateRangeWrapper;