-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DayPickerRangeControllerWrapper.jsx
197 lines (170 loc) · 5.83 KB
/
DayPickerRangeControllerWrapper.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* eslint-disable react/no-unused-prop-types */
import React from 'react';
import PropTypes from 'prop-types';
import momentPropTypes from 'react-moment-proptypes';
import { forbidExtraProps } from 'airbnb-prop-types';
import moment from 'moment';
import omit from 'lodash/omit';
import DayPickerRangeController from '../src/components/DayPickerRangeController';
import ScrollableOrientationShape from '../src/shapes/ScrollableOrientationShape';
import { START_DATE, END_DATE, HORIZONTAL_ORIENTATION } from '../src/constants';
import isInclusivelyAfterDay from '../src/utils/isInclusivelyAfterDay';
const propTypes = forbidExtraProps({
// example props for the demo
autoFocusEndDate: PropTypes.bool,
initialStartDate: momentPropTypes.momentObj,
initialEndDate: momentPropTypes.momentObj,
startDateOffset: PropTypes.func,
endDateOffset: PropTypes.func,
showInputs: PropTypes.bool,
minDate: momentPropTypes.momentObj,
maxDate: momentPropTypes.momentObj,
keepOpenOnDateSelect: PropTypes.bool,
minimumNights: PropTypes.number,
isOutsideRange: PropTypes.func,
isDayBlocked: PropTypes.func,
isDayHighlighted: PropTypes.func,
daysViolatingMinNightsCanBeClicked: PropTypes.bool,
// DayPicker props
enableOutsideDays: PropTypes.bool,
numberOfMonths: PropTypes.number,
orientation: ScrollableOrientationShape,
verticalHeight: PropTypes.number,
withPortal: PropTypes.bool,
initialVisibleMonth: PropTypes.func,
renderCalendarInfo: PropTypes.func,
renderMonthElement: PropTypes.func,
renderMonthText: PropTypes.func,
navPrev: PropTypes.node,
navNext: PropTypes.node,
renderNavPrevButton: PropTypes.func,
renderNavNextButton: PropTypes.func,
onPrevMonthClick: PropTypes.func,
onNextMonthClick: PropTypes.func,
onOutsideClick: PropTypes.func,
renderCalendarDay: PropTypes.func,
renderDayContents: PropTypes.func,
renderKeyboardShortcutsButton: PropTypes.func,
renderKeyboardShortcutsPanel: PropTypes.func,
// i18n
monthFormat: PropTypes.string,
isRTL: PropTypes.bool,
});
const defaultProps = {
// example props for the demo
autoFocusEndDate: false,
initialStartDate: null,
initialEndDate: null,
startDateOffset: undefined,
endDateOffset: undefined,
showInputs: false,
minDate: null,
maxDate: null,
// day presentation and interaction related props
renderCalendarDay: undefined,
renderDayContents: null,
minimumNights: 1,
isDayBlocked: () => false,
isOutsideRange: day => !isInclusivelyAfterDay(day, moment()),
isDayHighlighted: () => false,
enableOutsideDays: false,
daysViolatingMinNightsCanBeClicked: false,
// calendar presentation and interaction related props
orientation: HORIZONTAL_ORIENTATION,
verticalHeight: undefined,
withPortal: false,
initialVisibleMonth: null,
numberOfMonths: 2,
onOutsideClick() {},
keepOpenOnDateSelect: false,
renderCalendarInfo: null,
isRTL: false,
renderMonthText: null,
renderMonthElement: null,
renderKeyboardShortcutsButton: undefined,
renderKeyboardShortcutsPanel: undefined,
// navigation related props
navPrev: null,
navNext: null,
renderNavPrevButton: null,
renderNavNextButton: null,
onPrevMonthClick() {},
onNextMonthClick() {},
// internationalization
monthFormat: 'MMMM YYYY',
};
class DayPickerRangeControllerWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
errorMessage: null,
focusedInput: props.autoFocusEndDate ? END_DATE : START_DATE,
startDate: props.initialStartDate,
endDate: props.initialEndDate,
};
this.onDatesChange = this.onDatesChange.bind(this);
this.onFocusChange = this.onFocusChange.bind(this);
}
onDatesChange({ startDate, endDate }) {
const { daysViolatingMinNightsCanBeClicked, minimumNights } = this.props;
let doesNotMeetMinNights = false;
if (daysViolatingMinNightsCanBeClicked && startDate && endDate) {
const dayDiff = endDate.diff(startDate.clone().startOf('day').hour(12), 'days');
doesNotMeetMinNights = dayDiff < minimumNights && dayDiff >= 0;
}
this.setState({
startDate,
endDate: doesNotMeetMinNights ? null : endDate,
errorMessage: doesNotMeetMinNights
? 'That day does not meet the minimum nights requirement'
: null,
});
}
onFocusChange(focusedInput) {
this.setState({
// Force the focusedInput to always be truthy so that dates are always selectable
focusedInput: !focusedInput ? START_DATE : focusedInput,
});
}
render() {
const { renderCalendarInfo: renderCalendarInfoProp, showInputs } = this.props;
const {
errorMessage,
focusedInput,
startDate,
endDate,
} = this.state;
const props = omit(this.props, [
'autoFocus',
'autoFocusEndDate',
'initialStartDate',
'initialEndDate',
'showInputs',
]);
const startDateString = startDate && startDate.format('YYYY-MM-DD');
const endDateString = endDate && endDate.format('YYYY-MM-DD');
const renderCalendarInfo = errorMessage ? () => <div>{errorMessage}</div> : renderCalendarInfoProp;
return (
<div style={{ height: '100%' }}>
{showInputs && (
<div style={{ marginBottom: 16 }}>
<input type="text" name="start date" value={startDateString} readOnly />
<input type="text" name="end date" value={endDateString} readOnly />
</div>
)}
<DayPickerRangeController
{...props}
onDatesChange={this.onDatesChange}
onFocusChange={this.onFocusChange}
focusedInput={focusedInput}
startDate={startDate}
endDate={endDate}
renderCalendarInfo={renderCalendarInfo}
/>
</div>
);
}
}
DayPickerRangeControllerWrapper.propTypes = propTypes;
DayPickerRangeControllerWrapper.defaultProps = defaultProps;
export default DayPickerRangeControllerWrapper;