-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.jsx
635 lines (542 loc) · 16 KB
/
Calendar.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { polyfill } from 'react-lifecycles-compat';
import mergeClassNames from 'merge-class-names';
import Navigation from './Calendar/Navigation';
import CenturyView from './CenturyView';
import DecadeView from './DecadeView';
import YearView from './YearView';
import MonthView from './MonthView';
import { getBegin, getEnd, getValueRange } from './shared/dates';
import {
isCalendarType, isClassName, isMaxDate, isMinDate, isValue,
} from './shared/propTypes';
import { between, callIfDefined, mergeFunctions, getCurrentDate } from './shared/utils';
const baseClassName = 'react-calendar';
const allViews = ['century', 'decade', 'year', 'month'];
const allValueTypes = [...allViews.slice(1), 'day'];
const datesAreDifferent = (date1, date2) => (
(date1 && !date2)
|| (!date1 && date2)
|| (date1 && date2 && date1.getTime() !== date2.getTime())
);
/**
* Returns views array with disallowed values cut off.
*/
const getLimitedViews = (minDetail, maxDetail) => allViews
.slice(allViews.indexOf(minDetail), allViews.indexOf(maxDetail) + 1);
/**
* Determines whether a given view is allowed with currently applied settings.
*/
const isViewAllowed = (view, minDetail, maxDetail) => {
const views = getLimitedViews(minDetail, maxDetail);
return views.indexOf(view) !== -1;
};
/**
* Gets either provided view if allowed by minDetail and maxDetail, or gets
* the default view if not allowed.
*/
const getView = (view, minDetail, maxDetail) => {
if (isViewAllowed(view, minDetail, maxDetail)) {
return view;
}
return getLimitedViews(minDetail, maxDetail).pop();
};
/**
* Returns value type that can be returned with currently applied settings.
*/
const getValueType = maxDetail => allValueTypes[allViews.indexOf(maxDetail)];
const getValueFrom = (value) => {
if (!value) {
return null;
}
const rawValueFrom = value instanceof Array && value.length === 2 ? value[0] : value;
if (!rawValueFrom) {
return null;
}
const valueFromDate = new Date(rawValueFrom);
if (isNaN(valueFromDate.getTime())) {
throw new Error(`Invalid date: ${value}`);
}
return valueFromDate;
};
const getDetailValueFrom = (value, minDate, maxDate, maxDetail) => {
const valueFrom = getValueFrom(value);
if (!valueFrom) {
return null;
}
const detailValueFrom = getBegin(getValueType(maxDetail), valueFrom);
return between(detailValueFrom, minDate, maxDate);
};
const getValueTo = (value) => {
if (!value) {
return null;
}
const rawValueTo = value instanceof Array && value.length === 2 ? value[1] : value;
if (!rawValueTo) {
return null;
}
const valueToDate = new Date(rawValueTo);
if (isNaN(valueToDate.getTime())) {
throw new Error(`Invalid date: ${value}`);
}
return valueToDate;
};
const getDetailValueTo = (value, minDate, maxDate, maxDetail) => {
const valueTo = getValueTo(value);
if (!valueTo) {
return null;
}
const detailValueTo = getEnd(getValueType(maxDetail), valueTo);
return between(detailValueTo, minDate, maxDate);
};
const getDetailValueArray = (value, minDate, maxDate, maxDetail) => {
if (value instanceof Array) {
return value;
}
return [
getDetailValueFrom(value, minDate, maxDate, maxDetail),
getDetailValueTo(value, minDate, maxDate, maxDetail),
];
};
const getActiveStartDate = (props) => {
const {
activeStartDate,
maxDate,
maxDetail,
minDate,
minDetail,
value,
view,
} = props;
const rangeType = getView(view, minDetail, maxDetail);
const valueFrom = (
getDetailValueFrom(value, minDate, maxDate, maxDetail)
|| activeStartDate
|| new Date()
);
return getBegin(rangeType, valueFrom);
};
export default class Calendar extends Component {
static getDerivedStateFromProps(nextProps, prevState) {
const {
minDate, maxDate, minDetail, maxDetail,
} = nextProps;
const nextState = {};
/**
* If the next activeStartDate is different from the current one, update
* activeStartDate (for display) and activeStartDateProps (for future comparisons)
*/
const nextActiveStartDate = getActiveStartDate(nextProps);
if (datesAreDifferent(nextActiveStartDate, prevState.activeStartDateProps)) {
nextState.activeStartDate = nextActiveStartDate;
nextState.activeStartDateProps = nextActiveStartDate;
}
/**
* If the next view is different from the current one, and the previously set view is not
* valid based on minDetail and maxDetail, get a new one.
*/
const nextView = getView(nextProps.view, minDetail, maxDetail);
if (nextView !== prevState.viewProps && !isViewAllowed(prevState.view, minDetail, maxDetail)) {
nextState.view = nextView;
nextState.viewProps = nextView;
}
/**
* If the next value is different from the current one (with an exception of situation in
* which values provided are limited by minDate and maxDate so that the dates are the same),
* get a new one.
*/
const values = [nextProps.value, prevState.valueProps];
if (
nextState.view // Allowed view changed
|| datesAreDifferent(
...values.map(value => getValueFrom(value, minDate, maxDate, maxDetail)),
)
|| datesAreDifferent(
...values.map(value => getValueTo(value, minDate, maxDate, maxDetail)),
)
) {
nextState.value = nextProps.value;
nextState.valueProps = nextProps.value;
}
if (!nextProps.selectRange && prevState.hover) {
nextState.hover = null;
}
return nextState;
}
state = {
itemVisibilityClass: this.props.oneWeekCalendar ? 'calendar-item-hidden' : '',
arrowIconClass: 'down'
};
get drillDownAvailable() {
const { maxDetail, minDetail } = this.props;
const { view } = this.state;
const views = getLimitedViews(minDetail, maxDetail);
return views.indexOf(view) < views.length - 1;
}
get drillUpAvailable() {
const { maxDetail, minDetail } = this.props;
const { view } = this.state;
const views = getLimitedViews(minDetail, maxDetail);
return views.indexOf(view) > 0;
}
get valueType() {
const { maxDetail } = this.props;
return getValueType(maxDetail);
}
/**
* Gets current value in a desired format.
*/
getProcessedValue(value) {
const {
minDate, maxDate, maxDetail, returnValue,
} = this.props;
const processFunction = (() => {
switch (returnValue) {
case 'start':
return getDetailValueFrom;
case 'end':
return getDetailValueTo;
case 'range':
return getDetailValueArray;
default:
throw new Error('Invalid returnValue.');
}
})();
return processFunction(value, minDate, maxDate, maxDetail);
}
/**
* Called when the user uses navigation buttons.
*/
setActiveStartDate = (activeStartDate) => {
const { onActiveDateChange } = this.props;
this.setState({ activeStartDate }, () => {
const { view } = this.state;
callIfDefined(onActiveDateChange, {
activeStartDate,
view,
});
});
}
drillDown = (activeStartDate) => {
if (!this.drillDownAvailable) {
return;
}
const { maxDetail, minDetail, onDrillDown } = this.props;
const views = getLimitedViews(minDetail, maxDetail);
this.setState((prevState) => {
const nextView = views[views.indexOf(prevState.view) + 1];
return {
activeStartDate,
view: nextView,
};
}, () => {
const { view } = this.state;
callIfDefined(onDrillDown, {
activeStartDate,
view,
});
});
}
drillUp = () => {
if (!this.drillUpAvailable) {
return;
}
const { maxDetail, minDetail, onDrillUp } = this.props;
const views = getLimitedViews(minDetail, maxDetail);
this.setState((prevState) => {
const nextView = views[views.indexOf(prevState.view) - 1];
const activeStartDate = getBegin(nextView, prevState.activeStartDate);
return {
activeStartDate,
view: nextView,
};
}, () => {
const { activeStartDate, view } = this.state;
callIfDefined(onDrillUp, {
activeStartDate,
view,
});
});
}
onChange = (value) => {
const { onChange, selectRange } = this.props;
let nextValue;
let callback;
if (selectRange) {
const { value: previousValue } = this.state;
// Range selection turned on
if (
!previousValue
|| [].concat(previousValue).length !== 1 // 0 or 2 - either way we're starting a new array
) {
// First value
nextValue = getBegin(this.valueType, value);
} else {
// Second value
nextValue = getValueRange(this.valueType, previousValue, value);
callback = () => callIfDefined(onChange, nextValue);
}
} else {
// Range selection turned off
nextValue = this.getProcessedValue(value);
callback = () => callIfDefined(onChange, nextValue);
}
this.setState({ value: nextValue }, callback);
}
onMouseOver = (value) => {
this.setState((prevState) => {
if (prevState.hover && (prevState.hover.getTime() === value.getTime())) {
return null;
}
return { hover: value };
});
}
onMouseLeave = () => {
this.setState({ hover: null });
}
renderContent() {
const {
calendarType,
locale,
maxDate,
minDate,
renderChildren,
selectRange,
tileClassName,
tileContent,
tileDisabled,
} = this.props;
const {
activeStartDate, hover, value, view, itemVisibilityClass,
} = this.state;
const { onMouseOver, valueType } = this;
const commonProps = {
activeStartDate,
hover,
locale,
maxDate,
minDate,
onMouseOver: selectRange ? onMouseOver : null,
tileClassName,
tileContent: tileContent || renderChildren, // For backwards compatibility
tileDisabled,
value,
valueType,
};
const clickAction = this.drillDownAvailable ? this.drillDown : this.onChange;
switch (view) {
case 'century': {
const { onClickDecade } = this.props;
return (
<CenturyView
onClick={mergeFunctions(clickAction, onClickDecade)}
{...commonProps}
/>
);
}
case 'decade': {
const { onClickYear } = this.props;
return (
<DecadeView
onClick={mergeFunctions(clickAction, onClickYear)}
{...commonProps}
/>
);
}
case 'year': {
const { formatMonth, onClickMonth } = this.props;
return (
<YearView
formatMonth={formatMonth}
onClick={mergeFunctions(clickAction, onClickMonth)}
{...commonProps}
/>
);
}
case 'month': {
const {
formatShortWeekday,
onClickDay,
onClickWeekNumber,
showFixedNumberOfWeeks,
showNeighboringMonth,
showWeekNumbers,
} = this.props;
const { onMouseLeave } = this;
return (
<MonthView
calendarType={calendarType}
formatShortWeekday={formatShortWeekday}
itemVisibilityClass={itemVisibilityClass}
onClick={mergeFunctions(clickAction, onClickDay)}
onClickWeekNumber={onClickWeekNumber}
onMouseLeave={onMouseLeave}
showFixedNumberOfWeeks={showFixedNumberOfWeeks}
showNeighboringMonth={showNeighboringMonth}
showWeekNumbers={showWeekNumbers}
{...commonProps}
/>
);
}
default:
throw new Error(`Invalid view: ${view}.`);
}
}
renderNavigation() {
const { showNavigation } = this.props;
if (!showNavigation) {
return null;
}
const {
formatMonthYear,
locale,
maxDate,
maxDetail,
minDate,
minDetail,
navigationAriaLabel,
navigationLabel,
next2AriaLabel,
next2Label,
nextAriaLabel,
nextLabel,
prev2AriaLabel,
prev2Label,
prevAriaLabel,
prevLabel,
} = this.props;
const { activeStartDate, view } = this.state;
return (
<Navigation
activeStartDate={activeStartDate}
drillUp={this.drillUp}
formatMonthYear={formatMonthYear}
locale={locale}
maxDate={maxDate}
minDate={minDate}
navigationAriaLabel={navigationAriaLabel}
navigationLabel={navigationLabel}
next2AriaLabel={next2AriaLabel}
next2Label={next2Label}
nextAriaLabel={nextAriaLabel}
nextLabel={nextLabel}
prev2AriaLabel={prev2AriaLabel}
prev2Label={prev2Label}
prevAriaLabel={prevAriaLabel}
prevLabel={prevLabel}
setActiveStartDate={this.setActiveStartDate}
view={view}
views={getLimitedViews(minDetail, maxDetail)}
/>
);
}
colapseExpandCalendar = () => {
let visibilityClass = 'calendar-item-hidden';
let iconClass = 'down';
if (this.state.itemVisibilityClass) {
visibilityClass = '';
iconClass = 'up';
}
this.setState({ itemVisibilityClass: visibilityClass, arrowIconClass: iconClass });
}
render() {
const { className, selectRange, oneWeekCalendar } = this.props;
const { value, arrowIconClass } = this.state;
const { onMouseLeave } = this;
const valueArray = [].concat(value);
return (
<div
className={mergeClassNames(
baseClassName,
selectRange && valueArray.length === 1 && `${baseClassName}--selectRange`,
className,
)}
>
{this.renderNavigation()}
<div
className={`${baseClassName}__viewContainer`}
onBlur={selectRange ? onMouseLeave : null}
onMouseLeave={selectRange ? onMouseLeave : null}
>
{this.renderContent()}
</div>
{
oneWeekCalendar && (
<div className="calendar-footer">
<div>{getCurrentDate()}</div>
<div>
<a className="arrow-down-link" onClick={this.colapseExpandCalendar}>
<i className={arrowIconClass} />
</a>
</div>
</div>
)
}
</div>
);
}
}
Calendar.defaultProps = {
maxDetail: 'month',
minDetail: 'century',
returnValue: 'start',
showNavigation: true,
showNeighboringMonth: true,
view: 'month',
};
Calendar.propTypes = {
activeStartDate: PropTypes.instanceOf(Date),
calendarType: isCalendarType,
className: isClassName,
formatMonth: PropTypes.func,
formatMonthYear: PropTypes.func,
formatShortWeekday: PropTypes.func,
locale: PropTypes.string,
maxDate: isMaxDate,
maxDetail: PropTypes.oneOf(allViews),
minDate: isMinDate,
minDetail: PropTypes.oneOf(allViews),
navigationAriaLabel: PropTypes.string,
navigationLabel: PropTypes.func,
next2AriaLabel: PropTypes.string,
next2Label: PropTypes.node,
nextAriaLabel: PropTypes.string,
nextLabel: PropTypes.node,
onActiveDateChange: PropTypes.func,
onChange: PropTypes.func,
onClickDay: PropTypes.func,
onClickDecade: PropTypes.func,
onClickMonth: PropTypes.func,
onClickWeekNumber: PropTypes.func,
onClickYear: PropTypes.func,
onDrillDown: PropTypes.func,
onDrillUp: PropTypes.func,
oneWeekCalendar: PropTypes.bool,
prev2AriaLabel: PropTypes.string,
prev2Label: PropTypes.node,
prevAriaLabel: PropTypes.string, // For backwards compatibility
prevLabel: PropTypes.node,
renderChildren: PropTypes.func,
returnValue: PropTypes.oneOf(['start', 'end', 'range']),
selectRange: PropTypes.bool,
showFixedNumberOfWeeks: PropTypes.bool,
showNavigation: PropTypes.bool,
showNeighboringMonth: PropTypes.bool,
showWeekNumbers: PropTypes.bool,
tileClassName: PropTypes.oneOfType([
PropTypes.func,
isClassName,
]),
tileContent: PropTypes.oneOfType([
PropTypes.func,
PropTypes.node,
]),
tileDisabled: PropTypes.func,
value: PropTypes.oneOfType([
PropTypes.string,
isValue,
]),
view: PropTypes.oneOf(allViews),
};
polyfill(Calendar);