This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathDatePickerRange.react.js
200 lines (177 loc) · 6.72 KB
/
DatePickerRange.react.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
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
198
199
200
import 'react-dates/initialize';
import {DateRangePicker} from 'react-dates';
import React, {Component} from 'react';
import uniqid from 'uniqid';
import {propTypes, defaultProps} from '../components/DatePickerRange.react';
import convertToMoment, {setLocaleGlobally} from '../utils/convertToMoment';
export default class DatePickerRange extends Component {
constructor(props) {
super(props);
setLocaleGlobally(this.props.language);
this.propsToState = this.propsToState.bind(this);
this.onDatesChange = this.onDatesChange.bind(this);
this.isOutsideRange = this.isOutsideRange.bind(this);
this.state = {
focused: false,
start_date_id: props.start_date_id || uniqid(),
end_date_id: props.end_date_id || uniqid(),
};
}
propsToState(newProps, force = false) {
const state = {};
if (force || newProps.start_date !== this.props.start_date) {
state.start_date = newProps.start_date;
}
if (force || newProps.end_date !== this.props.end_date) {
state.end_date = newProps.end_date;
}
if (Object.keys(state).length) {
this.setState(state);
}
}
componentWillReceiveProps(newProps) {
this.propsToState(newProps);
}
componentWillMount() {
this.propsToState(this.props, true);
}
onDatesChange({startDate: start_date, endDate: end_date}) {
const {setProps, updatemode, clearable} = this.props;
const oldMomentDates = convertToMoment(this.state, [
'start_date',
'end_date',
]);
if (start_date && !start_date.isSame(oldMomentDates.start_date)) {
if (updatemode === 'singledate') {
setProps({start_date: start_date.format('YYYY-MM-DD')});
} else {
this.setState({start_date: start_date.format('YYYY-MM-DD')});
}
}
if (end_date && !end_date.isSame(oldMomentDates.end_date)) {
if (updatemode === 'singledate') {
setProps({end_date: end_date.format('YYYY-MM-DD')});
} else if (updatemode === 'bothdates') {
setProps({
start_date: this.state.start_date,
end_date: end_date.format('YYYY-MM-DD'),
});
}
}
if (
clearable &&
!start_date &&
!end_date &&
(oldMomentDates.start_date !== start_date ||
oldMomentDates.end_date !== end_date)
) {
setProps({start_date: null, end_date: null});
}
}
isOutsideRange(date) {
const {min_date_allowed, max_date_allowed} = this.props;
return (
(min_date_allowed && date.isBefore(min_date_allowed)) ||
(max_date_allowed && date.isAfter(max_date_allowed))
);
}
render() {
const {focusedInput} = this.state;
const {
calendar_orientation,
clearable,
day_size,
disabled,
display_format,
end_date_placeholder_text,
first_day_of_week,
is_RTL,
minimum_nights,
month_format,
number_of_months_shown,
reopen_calendar_on_clear,
show_outside_days,
start_date_placeholder_text,
stay_open_on_select,
with_full_screen_portal,
with_portal,
loading_state,
id,
style,
className,
start_date_id,
end_date_id,
} = this.props;
const {initial_visible_month} = convertToMoment(this.props, [
'initial_visible_month',
]);
const {start_date, end_date} = convertToMoment(this.state, [
'start_date',
'end_date',
]);
const verticalFlag = calendar_orientation !== 'vertical';
const DatePickerWrapperStyles = {
position: 'relative',
display: 'inline-block',
...style,
};
// the height in px of the top part of the calendar (that holds
// the name of the month)
const baselineHeight = 145;
return (
<div
id={id}
style={DatePickerWrapperStyles}
className={className}
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
>
<DateRangePicker
daySize={day_size}
disabled={disabled}
displayFormat={display_format}
enableOutsideDays={show_outside_days}
endDate={end_date}
endDatePlaceholderText={end_date_placeholder_text}
firstDayOfWeek={first_day_of_week}
focusedInput={focusedInput}
initialVisibleMonth={() => {
if (initial_visible_month) {
return initial_visible_month;
} else if (end_date && focusedInput === 'endDate') {
return end_date;
} else if (start_date && focusedInput === 'startDate') {
return start_date;
}
return start_date;
}}
isOutsideRange={this.isOutsideRange}
isRTL={is_RTL}
keepOpenOnDateSelect={stay_open_on_select}
minimumNights={minimum_nights}
monthFormat={month_format}
numberOfMonths={number_of_months_shown}
onDatesChange={this.onDatesChange}
onFocusChange={focusedInput =>
this.setState({focusedInput})
}
orientation={calendar_orientation}
reopenPickerOnClearDates={reopen_calendar_on_clear}
showClearDates={clearable}
startDate={start_date}
startDatePlaceholderText={start_date_placeholder_text}
withFullScreenPortal={
with_full_screen_portal && verticalFlag
}
withPortal={with_portal && verticalFlag}
startDateId={start_date_id || this.state.start_date_id}
endDateId={end_date_id || this.state.end_date_id}
verticalHeight={baselineHeight + day_size * 6 + 'px'}
/>
</div>
);
}
}
DatePickerRange.propTypes = propTypes;
DatePickerRange.defaultProps = defaultProps;