/
DateRangePickerWrapper.jsx
148 lines (127 loc) · 3.8 KB
/
DateRangePickerWrapper.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import momentPropTypes from 'react-moment-proptypes';
import moment from 'moment';
import omit from 'lodash/omit';
import DateRangePicker from '../src/components/DateRangePicker';
import { DateRangePickerPhrases } from '../src/defaultPhrases';
import DateRangePickerShape from '../src/shapes/DateRangePickerShape';
import { START_DATE, END_DATE, HORIZONTAL_ORIENTATION, ANCHOR_LEFT } from '../src/constants';
import isInclusivelyAfterDay from '../src/utils/isInclusivelyAfterDay';
const propTypes = {
// example props for the demo
autoFocus: PropTypes.bool,
autoFocusEndDate: PropTypes.bool,
initialStartDate: momentPropTypes.momentObj,
initialEndDate: momentPropTypes.momentObj,
...omit(DateRangePickerShape, [
'startDate',
'endDate',
'onDatesChange',
'focusedInput',
'onFocusChange',
]),
};
const defaultProps = {
// example props for the demo
autoFocus: false,
autoFocusEndDate: false,
initialStartDate: null,
initialEndDate: null,
// input related props
startDateId: START_DATE,
startDatePlaceholderText: 'Start Date',
endDateId: END_DATE,
endDatePlaceholderText: 'End Date',
disabled: false,
required: false,
screenReaderInputMessage: '',
showClearDates: false,
showDefaultInputIcon: false,
customInputIcon: null,
customArrowIcon: null,
customCloseIcon: null,
block: false,
small: false,
// calendar presentation and interaction related props
renderMonth: null,
orientation: HORIZONTAL_ORIENTATION,
anchorDirection: ANCHOR_LEFT,
horizontalMargin: 0,
withPortal: false,
withFullScreenPortal: false,
initialVisibleMonth: null,
numberOfMonths: 2,
keepOpenOnDateSelect: false,
reopenPickerOnClearDates: false,
isRTL: false,
// navigation related props
navPrev: null,
navNext: null,
onPrevMonthClick() {},
onNextMonthClick() {},
onClose() {},
// day presentation and interaction related props
renderCalendarDay: undefined,
renderDayContents: null,
minimumNights: 1,
enableOutsideDays: false,
isDayBlocked: () => false,
isOutsideRange: day => !isInclusivelyAfterDay(day, moment()),
isDayHighlighted: () => false,
// internationalization
displayFormat: () => moment.localeData().longDateFormat('L'),
monthFormat: 'MMMM YYYY',
phrases: DateRangePickerPhrases,
};
class DateRangePickerWrapper extends React.Component {
constructor(props) {
super(props);
let focusedInput = null;
if (props.autoFocus) {
focusedInput = START_DATE;
} else if (props.autoFocusEndDate) {
focusedInput = END_DATE;
}
this.state = {
focusedInput,
startDate: props.initialStartDate,
endDate: props.initialEndDate,
};
this.onDatesChange = this.onDatesChange.bind(this);
this.onFocusChange = this.onFocusChange.bind(this);
}
onDatesChange({ startDate, endDate }) {
this.setState({ startDate, endDate });
}
onFocusChange(focusedInput) {
this.setState({ focusedInput });
}
render() {
const { focusedInput, startDate, endDate } = this.state;
// autoFocus, autoFocusEndDate, initialStartDate and initialEndDate are helper props for the
// example wrapper but are not props on the SingleDatePicker itself and
// thus, have to be omitted.
const props = omit(this.props, [
'autoFocus',
'autoFocusEndDate',
'initialStartDate',
'initialEndDate',
]);
return (
<div>
<DateRangePicker
{...props}
onDatesChange={this.onDatesChange}
onFocusChange={this.onFocusChange}
focusedInput={focusedInput}
startDate={startDate}
endDate={endDate}
/>
</div>
);
}
}
DateRangePickerWrapper.propTypes = propTypes;
DateRangePickerWrapper.defaultProps = defaultProps;
export default DateRangePickerWrapper;