1
- import React , { PureComponent } from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
- import { rangeShape } from '../DayCell' ;
4
- import Month from '../Month' ;
5
- import DateInput from '../DateInput' ;
6
- import { calcFocusDate , generateStyles , getMonthDisplayRange } from '../../utils' ;
7
1
import classnames from 'classnames' ;
8
- import ReactList from 'react-list' ;
9
- import { shallowEqualObjects } from 'shallow-equal' ;
10
2
import * as dateFns from 'date-fns' ;
11
3
import { enUS as defaultLocale } from 'date-fns/locale/en-US' ;
12
- import coreStyles from '../../styles' ;
4
+ import PropTypes from 'prop-types' ;
5
+ import React , { PureComponent } from 'react' ;
6
+ import ReactList from 'react-list' ;
7
+ import { shallowEqualObjects } from 'shallow-equal' ;
13
8
import { ariaLabelsShape } from '../../accessibility' ;
9
+ import coreStyles from '../../styles' ;
10
+ import { calcFocusDate , generateStyles , getMonthDisplayRange , restrictMinMaxDate } from '../../utils' ;
11
+ import DateInput from '../DateInput' ;
12
+ import { rangeShape } from '../DayCell' ;
13
+ import Month from '../Month' ;
14
14
15
15
class Calendar extends PureComponent {
16
16
constructor ( props , context ) {
17
17
super ( props , context ) ;
18
18
this . dateOptions = { locale : props . locale } ;
19
- if ( props . weekStartsOn !== undefined ) this . dateOptions . weekStartsOn = props . weekStartsOn ;
19
+ if ( props . weekStartsOn !== undefined ) {
20
+ this . dateOptions . weekStartsOn = props . weekStartsOn ;
21
+ }
20
22
this . styles = generateStyles ( [ coreStyles , props . classNames ] ) ;
21
23
this . listSizeCache = { } ;
22
24
this . isFirstRender = true ;
25
+ const ranges = restrictMinMaxDate ( props . ranges , props . minDate , props . maxDate ) ;
23
26
this . state = {
27
+ ranges,
24
28
monthNames : this . getMonthNames ( ) ,
25
29
focusedDate : calcFocusDate ( null , props ) ,
26
30
drag : {
@@ -70,11 +74,7 @@ class Calendar extends PureComponent {
70
74
this . setState ( { focusedDate : date } ) ;
71
75
return ;
72
76
}
73
- const targetMonthIndex = dateFns . differenceInCalendarMonths (
74
- date ,
75
- props . minDate ,
76
- this . dateOptions
77
- ) ;
77
+ const targetMonthIndex = dateFns . differenceInCalendarMonths ( date , props . minDate , this . dateOptions ) ;
78
78
const visibleMonths = this . list . getVisibleRange ( ) ;
79
79
if ( preventUnnecessary && visibleMonths . includes ( targetMonthIndex ) ) return ;
80
80
this . isFirstRender = true ;
@@ -120,13 +120,9 @@ class Calendar extends PureComponent {
120
120
this . updateShownDate ( this . props ) ;
121
121
}
122
122
123
- if (
124
- prevProps . locale !== this . props . locale ||
125
- prevProps . weekStartsOn !== this . props . weekStartsOn
126
- ) {
123
+ if ( prevProps . locale !== this . props . locale || prevProps . weekStartsOn !== this . props . weekStartsOn ) {
127
124
this . dateOptions = { locale : this . props . locale } ;
128
- if ( this . props . weekStartsOn !== undefined )
129
- this . dateOptions . weekStartsOn = this . props . weekStartsOn ;
125
+ if ( this . props . weekStartsOn !== undefined ) this . dateOptions . weekStartsOn = this . props . weekStartsOn ;
130
126
this . setState ( {
131
127
monthNames : this . getMonthNames ( ) ,
132
128
} ) ;
@@ -182,7 +178,8 @@ class Calendar extends PureComponent {
182
178
type = "button"
183
179
className = { classnames ( styles . nextPrevButton , styles . prevButton ) }
184
180
onClick = { ( ) => changeShownDate ( - 1 , 'monthOffset' ) }
185
- aria-label = { ariaLabels . prevButton } >
181
+ aria-label = { ariaLabels . prevButton }
182
+ >
186
183
< i />
187
184
</ button >
188
185
) : null }
@@ -192,7 +189,8 @@ class Calendar extends PureComponent {
192
189
< select
193
190
value = { focusedDate . getMonth ( ) }
194
191
onChange = { e => changeShownDate ( e . target . value , 'setMonth' ) }
195
- aria-label = { ariaLabels . monthPicker } >
192
+ aria-label = { ariaLabels . monthPicker }
193
+ >
196
194
{ this . state . monthNames . map ( ( monthName , i ) => (
197
195
< option key = { i } value = { i } >
198
196
{ monthName }
@@ -205,17 +203,16 @@ class Calendar extends PureComponent {
205
203
< select
206
204
value = { focusedDate . getFullYear ( ) }
207
205
onChange = { e => changeShownDate ( e . target . value , 'setYear' ) }
208
- aria-label = { ariaLabels . yearPicker } >
209
- { new Array ( upperYearLimit - lowerYearLimit + 1 )
210
- . fill ( upperYearLimit )
211
- . map ( ( val , i ) => {
212
- const year = val - i ;
213
- return (
214
- < option key = { year } value = { year } >
215
- { year }
216
- </ option >
217
- ) ;
218
- } ) }
206
+ aria-label = { ariaLabels . yearPicker }
207
+ >
208
+ { new Array ( upperYearLimit - lowerYearLimit + 1 ) . fill ( upperYearLimit ) . map ( ( val , i ) => {
209
+ const year = val - i ;
210
+ return (
211
+ < option key = { year } value = { year } >
212
+ { year }
213
+ </ option >
214
+ ) ;
215
+ } ) }
219
216
</ select >
220
217
</ span >
221
218
</ span >
@@ -229,7 +226,8 @@ class Calendar extends PureComponent {
229
226
type = "button"
230
227
className = { classnames ( styles . nextPrevButton , styles . nextButton ) }
231
228
onClick = { ( ) => changeShownDate ( + 1 , 'monthOffset' ) }
232
- aria-label = { ariaLabels . nextButton } >
229
+ aria-label = { ariaLabels . nextButton }
230
+ >
233
231
< i />
234
232
</ button >
235
233
) : null }
@@ -257,28 +255,24 @@ class Calendar extends PureComponent {
257
255
const {
258
256
focusedRange,
259
257
color,
260
- ranges,
261
258
rangeColors,
262
259
dateDisplayFormat,
263
260
editableDateInputs,
264
261
startDatePlaceholder,
265
262
endDatePlaceholder,
266
263
ariaLabels,
267
264
} = this . props ;
265
+ const { ranges } = this . state ;
268
266
269
267
const defaultColor = rangeColors [ focusedRange [ 0 ] ] || color ;
270
268
const styles = this . styles ;
271
269
272
270
return (
273
271
< div className = { styles . dateDisplayWrapper } >
274
272
{ ranges . map ( ( range , i ) => {
275
- if ( range . showDateDisplay === false || ( range . disabled && ! range . showDateDisplay ) )
276
- return null ;
273
+ if ( range . showDateDisplay === false || ( range . disabled && ! range . showDateDisplay ) ) return null ;
277
274
return (
278
- < div
279
- className = { styles . dateDisplay }
280
- key = { i }
281
- style = { { color : range . color || defaultColor } } >
275
+ < div className = { styles . dateDisplay } key = { i } style = { { color : range . color || defaultColor } } >
282
276
< DateInput
283
277
className = { classnames ( styles . dateDisplayItem , {
284
278
[ styles . dateDisplayItemActive ] : focusedRange [ 0 ] === i && focusedRange [ 1 ] === 0 ,
@@ -403,7 +397,7 @@ class Calendar extends PureComponent {
403
397
const isVertical = direction === 'vertical' ;
404
398
const monthAndYearRenderer = navigatorRenderer || this . renderMonthAndYear ;
405
399
406
- const ranges = this . props . ranges . map ( ( range , i ) => ( {
400
+ const ranges = this . state . ranges . map ( ( range , i ) => ( {
407
401
...range ,
408
402
color : range . color || rangeColors [ i ] || color ,
409
403
} ) ) ;
@@ -413,7 +407,8 @@ class Calendar extends PureComponent {
413
407
onMouseUp = { ( ) => this . setState ( { drag : { status : false , range : { } } } ) }
414
408
onMouseLeave = { ( ) => {
415
409
this . setState ( { drag : { status : false , range : { } } } ) ;
416
- } } >
410
+ } }
411
+ >
417
412
{ showDateDisplay && this . renderDateDisplay ( ) }
418
413
{ monthAndYearRenderer ( focusedDate , this . changeShownDate , this . props ) }
419
414
{ scroll . enabled ? (
@@ -429,7 +424,8 @@ class Calendar extends PureComponent {
429
424
width : scrollArea . calendarWidth + 11 ,
430
425
height : scrollArea . calendarHeight + 11 ,
431
426
} }
432
- onScroll = { this . handleScroll } >
427
+ onScroll = { this . handleScroll }
428
+ >
433
429
< ReactList
434
430
length = { dateFns . differenceInCalendarMonths (
435
431
dateFns . endOfMonth ( maxDate ) ,
@@ -478,7 +474,8 @@ class Calendar extends PureComponent {
478
474
className = { classnames (
479
475
this . styles . months ,
480
476
isVertical ? this . styles . monthsVertical : this . styles . monthsHorizontal
481
- ) } >
477
+ ) }
478
+ >
482
479
{ new Array ( this . props . months ) . fill ( null ) . map ( ( _ , i ) => {
483
480
let monthStep = dateFns . addMonths ( this . state . focusedDate , i ) ;
484
481
if ( this . props . calendarFocus === 'backwards' ) {
0 commit comments