@@ -101,12 +101,8 @@ const getDetailValueArray = (args) => {
101
101
return [ getDetailValueFrom , getDetailValueTo ] . map ( fn => fn ( args ) ) ;
102
102
} ;
103
103
104
- const getActiveStartDate = ( props ) => {
104
+ function getActiveStartDate ( props ) {
105
105
const {
106
- activeStartDate,
107
- defaultActiveStartDate,
108
- defaultValue,
109
- defaultView,
110
106
maxDate,
111
107
maxDetail,
112
108
minDate,
@@ -115,33 +111,62 @@ const getActiveStartDate = (props) => {
115
111
view,
116
112
} = props ;
117
113
118
- const rangeType = getView ( view || defaultView , minDetail , maxDetail ) ;
114
+ const rangeType = getView ( view , minDetail , maxDetail ) ;
119
115
const valueFrom = (
120
- activeStartDate || defaultActiveStartDate
121
- || getDetailValueFrom ( {
122
- value : value || defaultValue , minDate, maxDate, maxDetail,
116
+ getDetailValueFrom ( {
117
+ value, minDate, maxDate, maxDetail,
123
118
} )
124
119
|| new Date ( )
125
120
) ;
121
+
126
122
return getBegin ( rangeType , valueFrom ) ;
127
- } ;
123
+ }
124
+
125
+ function getInitialActiveStartDate ( props ) {
126
+ const {
127
+ activeStartDate,
128
+ defaultActiveStartDate,
129
+ defaultValue,
130
+ defaultView,
131
+ maxDetail,
132
+ minDetail,
133
+ value,
134
+ view,
135
+ ...otherProps
136
+ } = props ;
137
+
138
+ const rangeType = getView ( view , minDetail , maxDetail ) ;
139
+ const valueFrom = activeStartDate || defaultActiveStartDate ;
140
+
141
+ if ( valueFrom ) {
142
+ return getBegin ( rangeType , valueFrom ) ;
143
+ }
144
+
145
+ return getActiveStartDate ( {
146
+ maxDetail,
147
+ minDetail,
148
+ value : value || defaultValue ,
149
+ view : view || defaultView ,
150
+ ...otherProps ,
151
+ } ) ;
152
+ }
128
153
129
154
const isSingleValue = value => value && [ ] . concat ( value ) . length === 1 ;
130
155
131
156
export default class Calendar extends Component {
132
157
state = {
133
158
/* eslint-disable react/destructuring-assignment */
134
- activeStartDate : getActiveStartDate ( this . props ) ,
135
- view : this . props . defaultView ,
159
+ activeStartDate : this . props . defaultActiveStartDate ,
136
160
value : this . props . defaultValue ,
161
+ view : this . props . defaultView ,
137
162
/* eslint-enable react/destructuring-assignment */
138
163
} ;
139
164
140
165
get activeStartDate ( ) {
141
166
const { activeStartDate : activeStartDateProps } = this . props ;
142
167
const { activeStartDate : activeStartDateState } = this . state ;
143
168
144
- return activeStartDateProps || activeStartDateState ;
169
+ return activeStartDateProps || activeStartDateState || getInitialActiveStartDate ( this . props ) ;
145
170
}
146
171
147
172
get value ( ) {
@@ -220,37 +245,40 @@ export default class Calendar extends Component {
220
245
} ) ;
221
246
}
222
247
223
- /**
224
- * Called when the user uses navigation buttons.
225
- */
226
- setActiveStartDate = ( activeStartDate ) => {
227
- const { onActiveStartDateChange } = this . props ;
248
+ setStateAndCallCallbacks = ( nextState , callback ) => {
249
+ const {
250
+ onActiveStartDateChange, onChange, onViewChange, selectRange,
251
+ } = this . props ;
252
+
253
+ this . setState ( nextState , ( ) => {
254
+ const args = {
255
+ activeStartDate : nextState . activeStartDate || this . activeStartDate ,
256
+ view : nextState . view || this . view ,
257
+ } ;
258
+
259
+ if ( 'activeStartDate' in nextState ) {
260
+ callIfDefined ( onActiveStartDateChange , args ) ;
261
+ }
262
+
263
+ if ( 'view' in nextState ) {
264
+ callIfDefined ( onViewChange , args ) ;
265
+ }
228
266
229
- this . setState ( { activeStartDate } , ( ) => {
230
- const { view } = this ;
267
+ if ( 'value' in nextState ) {
268
+ if ( ! selectRange || ! isSingleValue ( nextState . value ) ) {
269
+ callIfDefined ( onChange , nextState . value ) ;
270
+ }
271
+ }
231
272
232
- callIfDefined ( onActiveStartDateChange , {
233
- activeStartDate,
234
- view,
235
- } ) ;
273
+ callIfDefined ( callback , args ) ;
236
274
} ) ;
237
275
}
238
276
239
277
/**
240
278
* Called when the user uses navigation buttons.
241
279
*/
242
- setActiveStartDateAndView = ( activeStartDate , view , callback ) => {
243
- const { onActiveStartDateChange, onViewChange } = this . props ;
244
-
245
- this . setState ( { activeStartDate, view } , ( ) => {
246
- const args = {
247
- activeStartDate,
248
- view,
249
- } ;
250
- callIfDefined ( onActiveStartDateChange , args ) ;
251
- callIfDefined ( onViewChange , args ) ;
252
- callIfDefined ( callback , args ) ;
253
- } ) ;
280
+ setActiveStartDate = ( activeStartDate ) => {
281
+ this . setStateAndCallCallbacks ( { activeStartDate } ) ;
254
282
}
255
283
256
284
drillDown = ( nextActiveStartDate , event ) => {
@@ -265,7 +293,10 @@ export default class Calendar extends Component {
265
293
266
294
const nextView = views [ views . indexOf ( view ) + 1 ] ;
267
295
268
- this . setActiveStartDateAndView ( nextActiveStartDate , nextView , onDrillDown ) ;
296
+ this . setStateAndCallCallbacks ( {
297
+ activeStartDate : nextActiveStartDate ,
298
+ view : nextView ,
299
+ } , onDrillDown ) ;
269
300
}
270
301
271
302
drillUp = ( ) => {
@@ -279,16 +310,18 @@ export default class Calendar extends Component {
279
310
const nextView = views [ views . indexOf ( view ) - 1 ] ;
280
311
const nextActiveStartDate = getBegin ( nextView , activeStartDate ) ;
281
312
282
- this . setActiveStartDateAndView ( nextActiveStartDate , nextView , onDrillUp ) ;
313
+ this . setStateAndCallCallbacks ( {
314
+ activeStartDate : nextActiveStartDate ,
315
+ view : nextView ,
316
+ } , onDrillUp ) ;
283
317
}
284
318
285
319
onChange = ( value , event ) => {
286
- const { onChange , selectRange } = this . props ;
320
+ const { selectRange } = this . props ;
287
321
288
322
this . onClickTile ( value , event ) ;
289
323
290
324
let nextValue ;
291
- let callback ;
292
325
if ( selectRange ) {
293
326
// Range selection turned on
294
327
const { value : previousValue , valueType } = this ;
@@ -299,15 +332,21 @@ export default class Calendar extends Component {
299
332
} else {
300
333
// Second value
301
334
nextValue = getValueRange ( valueType , previousValue , value ) ;
302
- callback = ( ) => callIfDefined ( onChange , nextValue ) ;
303
335
}
304
336
} else {
305
337
// Range selection turned off
306
338
nextValue = this . getProcessedValue ( value ) ;
307
- callback = ( ) => callIfDefined ( onChange , nextValue ) ;
308
339
}
309
340
310
- this . setState ( { value : nextValue } , callback ) ;
341
+ const nextActiveStartDate = getActiveStartDate ( {
342
+ ...this . props ,
343
+ value : nextValue ,
344
+ } ) ;
345
+
346
+ this . setStateAndCallCallbacks ( {
347
+ activeStartDate : nextActiveStartDate ,
348
+ value : nextValue ,
349
+ } ) ;
311
350
}
312
351
313
352
onClickTile = ( value , event ) => {
0 commit comments