1
- import 'react-dates/initialize' ;
2
- import { DateRangePicker } from 'react-dates' ;
3
1
import PropTypes from 'prop-types' ;
4
- import React , { Component } from 'react' ;
5
- import uniqid from 'uniqid ' ;
2
+ import React , { Component , lazy , Suspense } from 'react' ;
3
+ import LazyLoader from '../utils/LazyLoader ' ;
6
4
7
- import convertToMoment from '../utils/convertToMoment' ;
5
+ const RealDatePickerRange = lazy ( LazyLoader . datePickerRange ) ;
8
6
9
7
/**
10
8
* DatePickerRange is a tailor made component designed for selecting
@@ -18,176 +16,11 @@ import convertToMoment from '../utils/convertToMoment';
18
16
* which can be found here: https://github.com/airbnb/react-dates
19
17
*/
20
18
export default class DatePickerRange extends Component {
21
- constructor ( props ) {
22
- super ( props ) ;
23
- this . propsToState = this . propsToState . bind ( this ) ;
24
- this . onDatesChange = this . onDatesChange . bind ( this ) ;
25
- this . isOutsideRange = this . isOutsideRange . bind ( this ) ;
26
- this . state = {
27
- focused : false ,
28
- start_date_id : props . start_date_id || uniqid ( ) ,
29
- end_date_id : props . end_date_id || uniqid ( ) ,
30
- } ;
31
- }
32
-
33
- propsToState ( newProps ) {
34
- this . setState ( {
35
- start_date : newProps . start_date ,
36
- end_date : newProps . end_date ,
37
- } ) ;
38
- }
39
-
40
- componentWillReceiveProps ( newProps ) {
41
- this . propsToState ( newProps ) ;
42
- }
43
-
44
- componentWillMount ( ) {
45
- this . propsToState ( this . props ) ;
46
- }
47
-
48
- onDatesChange ( { startDate : start_date , endDate : end_date } ) {
49
- const { setProps, updatemode, clearable} = this . props ;
50
-
51
- const oldMomentDates = convertToMoment ( this . state , [
52
- 'start_date' ,
53
- 'end_date' ,
54
- ] ) ;
55
-
56
- if ( start_date && ! start_date . isSame ( oldMomentDates . start_date ) ) {
57
- if ( updatemode === 'singledate' ) {
58
- setProps ( { start_date : start_date . format ( 'YYYY-MM-DD' ) } ) ;
59
- } else {
60
- this . setState ( { start_date : start_date . format ( 'YYYY-MM-DD' ) } ) ;
61
- }
62
- }
63
-
64
- if ( end_date && ! end_date . isSame ( oldMomentDates . end_date ) ) {
65
- if ( updatemode === 'singledate' ) {
66
- setProps ( { end_date : end_date . format ( 'YYYY-MM-DD' ) } ) ;
67
- } else if ( updatemode === 'bothdates' ) {
68
- setProps ( {
69
- start_date : this . state . start_date ,
70
- end_date : end_date . format ( 'YYYY-MM-DD' ) ,
71
- } ) ;
72
- }
73
- }
74
-
75
- if (
76
- clearable &&
77
- ! start_date &&
78
- ! end_date &&
79
- ( oldMomentDates . start_date !== start_date ||
80
- oldMomentDates . end_date !== end_date )
81
- ) {
82
- setProps ( { start_date : null , end_date : null } ) ;
83
- }
84
- }
85
-
86
- isOutsideRange ( date ) {
87
- const { min_date_allowed, max_date_allowed} = this . props ;
88
-
89
- return (
90
- ( min_date_allowed && date . isBefore ( min_date_allowed ) ) ||
91
- ( max_date_allowed && date . isAfter ( max_date_allowed ) )
92
- ) ;
93
- }
94
-
95
19
render ( ) {
96
- const { focusedInput} = this . state ;
97
-
98
- const {
99
- calendar_orientation,
100
- clearable,
101
- day_size,
102
- disabled,
103
- display_format,
104
- end_date_placeholder_text,
105
- first_day_of_week,
106
- is_RTL,
107
- minimum_nights,
108
- month_format,
109
- number_of_months_shown,
110
- reopen_calendar_on_clear,
111
- show_outside_days,
112
- start_date_placeholder_text,
113
- stay_open_on_select,
114
- with_full_screen_portal,
115
- with_portal,
116
- loading_state,
117
- id,
118
- style,
119
- className,
120
- start_date_id,
121
- end_date_id,
122
- } = this . props ;
123
-
124
- const { initial_visible_month} = convertToMoment ( this . props , [
125
- 'initial_visible_month' ,
126
- ] ) ;
127
-
128
- const { start_date, end_date} = convertToMoment ( this . state , [
129
- 'start_date' ,
130
- 'end_date' ,
131
- ] ) ;
132
- const verticalFlag = calendar_orientation !== 'vertical' ;
133
-
134
- const DatePickerWrapperStyles = {
135
- position : 'relative' ,
136
- display : 'inline-block' ,
137
- ...style ,
138
- } ;
139
-
140
20
return (
141
- < div
142
- id = { id }
143
- style = { DatePickerWrapperStyles }
144
- className = { className }
145
- data-dash-is-loading = {
146
- ( loading_state && loading_state . is_loading ) || undefined
147
- }
148
- >
149
- < DateRangePicker
150
- daySize = { day_size }
151
- disabled = { disabled }
152
- displayFormat = { display_format }
153
- enableOutsideDays = { show_outside_days }
154
- endDate = { end_date }
155
- endDatePlaceholderText = { end_date_placeholder_text }
156
- firstDayOfWeek = { first_day_of_week }
157
- focusedInput = { focusedInput }
158
- initialVisibleMonth = { ( ) => {
159
- if ( initial_visible_month ) {
160
- return initial_visible_month ;
161
- } else if ( end_date && focusedInput === 'endDate' ) {
162
- return end_date ;
163
- } else if ( start_date && focusedInput === 'startDate' ) {
164
- return start_date ;
165
- }
166
- return start_date ;
167
- } }
168
- isOutsideRange = { this . isOutsideRange }
169
- isRTL = { is_RTL }
170
- keepOpenOnDateSelect = { stay_open_on_select }
171
- minimumNights = { minimum_nights }
172
- monthFormat = { month_format }
173
- numberOfMonths = { number_of_months_shown }
174
- onDatesChange = { this . onDatesChange }
175
- onFocusChange = { focusedInput =>
176
- this . setState ( { focusedInput} )
177
- }
178
- orientation = { calendar_orientation }
179
- reopenPickerOnClearDates = { reopen_calendar_on_clear }
180
- showClearDates = { clearable }
181
- startDate = { start_date }
182
- startDatePlaceholderText = { start_date_placeholder_text }
183
- withFullScreenPortal = {
184
- with_full_screen_portal && verticalFlag
185
- }
186
- withPortal = { with_portal && verticalFlag }
187
- startDateId = { start_date_id || this . state . start_date_id }
188
- endDateId = { end_date_id || this . state . end_date_id }
189
- />
190
- </ div >
21
+ < Suspense fallback = { null } >
22
+ < RealDatePickerRange { ...this . props } />
23
+ </ Suspense >
191
24
) ;
192
25
}
193
26
}
@@ -446,3 +279,6 @@ DatePickerRange.defaultProps = {
446
279
persisted_props : [ 'start_date' , 'end_date' ] ,
447
280
persistence_type : 'local' ,
448
281
} ;
282
+
283
+ export const propTypes = DatePickerRange . propTypes ;
284
+ export const defaultProps = DatePickerRange . defaultProps ;
0 commit comments