forked from stephy/CalendarPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
656 lines (602 loc) · 20.6 KB
/
Copy pathindex.js
File metadata and controls
656 lines (602 loc) · 20.6 KB
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import React, { Component } from 'react';
import { View, Dimensions, Text } from 'react-native';
import { makeStyles } from './makeStyles';
import { Utils } from './Utils';
import HeaderControls from './HeaderControls';
import Weekdays from './Weekdays';
import DaysGridView from './DaysGridView';
import MonthSelector from './MonthSelector';
import YearSelector from './YearSelector';
import Scroller from './Scroller';
import { addMonths } from 'date-fns/addMonths';
import { getMonth } from 'date-fns/getMonth';
import { getYear } from 'date-fns/getYear';
import { isAfter } from 'date-fns/isAfter';
import { isBefore } from 'date-fns/isBefore';
import { isSameDay } from 'date-fns/isSameDay';
import { isSameMonth } from 'date-fns/isSameMonth';
import { startOfMonth } from 'date-fns/startOfMonth';
import { subMonths } from 'date-fns/subMonths';
export default class CalendarPicker extends Component {
constructor(props) {
super(props);
this.numMonthsScroll = 60; // 5 years
this.state = {
currentMonth: null,
currentYear: null,
currentView: props.initialView || 'days',
selectedStartDate: props.selectedStartDate && new Date(props.selectedStartDate),
selectedEndDate: props.selectedEndDate && new Date(props.selectedEndDate),
minDate: props.minDate && new Date(props.minDate),
maxDate: props.maxDate && new Date(props.maxDate),
styles: {},
...this.updateScaledStyles(props),
...this.updateMonthYear(props.initialDate),
...this.updateDisabledDates(props.disabledDates),
...this.updateMinMaxRanges(props.minRangeDuration, props.maxRangeDuration),
...this.createMonths(props, {}),
};
this.state.renderMonthParams = this.createMonthProps(this.state);
Text.defaultProps = {
...Text.defaultProps,
allowFontScaling: props.fontScaling,
}
}
static defaultProps = {
initialDate: new Date(),
scaleFactor: 375,
scrollable: false,
scrollDecelerationRate: 'normal',
onDateChange: () => {
console.log('onDateChange() not provided');
},
enableDateChange: true,
headingLevel: 1,
sundayColor: '#FFFFFF',
customDatesStyles: [],
previousTitle: 'Previous',
nextTitle: 'Next',
selectMonthTitle: 'Select Month in ',
selectYearTitle: 'Select Year',
horizontal: true,
selectedDayStyle: null,
selectedRangeStartStyle: null,
selectedRangeEndStyle: null,
selectedRangeStyle: null,
fontScaling: true,
};
componentDidUpdate(prevProps) {
let doStateUpdate = false;
let newStyles = {};
if (
prevProps.width !== this.props.width ||
prevProps.height !== this.props.height
) {
newStyles = this.updateScaledStyles(this.props);
doStateUpdate = true;
}
let newMonthYear = {};
if (!isSameDay(prevProps.initialDate, this.props.initialDate)) {
newMonthYear = this.updateMonthYear(this.props.initialDate);
doStateUpdate = true;
}
let selectedDateRanges = {};
const { selectedStartDate, selectedEndDate } = this.props;
if (selectedStartDate !== prevProps.selectedStartDate ||
selectedEndDate !== prevProps.selectedEndDate
) {
selectedDateRanges = {
selectedStartDate: selectedStartDate && new Date(selectedStartDate),
selectedEndDate: selectedEndDate && new Date(selectedEndDate)
};
doStateUpdate = true;
}
let disabledDates = {};
if (prevProps.disabledDates !== this.props.disabledDates) {
disabledDates = this.updateDisabledDates(this.props.disabledDates);
doStateUpdate = true;
}
let rangeDurations = {};
if (prevProps.minRangeDuration !== this.props.minRangeDuration ||
prevProps.maxRangeDuration !== this.props.maxRangeDuration
) {
const { minRangeDuration, maxRangeDuration } = this.props;
rangeDurations = this.updateMinMaxRanges(minRangeDuration, maxRangeDuration);
doStateUpdate = true;
}
let minMaxDates = {};
if (prevProps.minDate !== this.props.minDate ||
prevProps.minDate !== this.props.minDate
) {
minMaxDates.minDate = this.props.minDate && new Date(this.props.minDate);
minMaxDates.maxDate = this.props.maxDate && new Date(this.props.maxDate);
doStateUpdate = true;
}
if (prevProps.customDatesStyles !== this.props.customDatesStyles) {
// Update renderMonthParams on customDatesStyles change
doStateUpdate = true;
}
if (doStateUpdate) {
const newState = {
...newStyles,
...newMonthYear,
...selectedDateRanges,
...disabledDates,
...rangeDurations,
...minMaxDates,
};
let renderMonthParams = {};
const _state = { ...this.state, ...newState };
renderMonthParams = this.createMonthProps(_state);
this.setState({ ...newState, renderMonthParams });
}
}
updateScaledStyles = props => {
const {
scaleFactor,
selectedDayColor,
selectedDayTextColor,
todayBackgroundColor,
width,
height,
dayShape
} = props;
// The styles in makeStyles are intially scaled to this width
const containerWidth = width ? width : Dimensions.get('window').width;
const containerHeight = height ? height : Dimensions.get('window').height;
return {
styles: makeStyles({
containerWidth,
containerHeight,
scaleFactor,
selectedDayColor,
selectedDayTextColor,
todayBackgroundColor,
dayShape
})
};
}
updateMonthYear = (initialDate = this.props.initialDate, updateState) => {
const newState = {
currentMonth: parseInt(getMonth(new Date(initialDate))),
currentYear: parseInt(getYear(new Date(initialDate)))
};
if (updateState) {
this.setState(newState);
}
return newState;
}
updateDisabledDates = (_disabledDates = []) => {
let disabledDates = [];
if (_disabledDates) {
if (Array.isArray(_disabledDates)) {
// Convert input date into timestamp
_disabledDates.map(date => {
let thisDate = new Date(date);
thisDate.setHours(12, 0, 0, 0);
disabledDates.push(thisDate.valueOf());
});
}
else if (_disabledDates instanceof Function) {
disabledDates = _disabledDates;
}
}
return { disabledDates };
}
updateMinMaxRanges = (_minRangeDuration, _maxRangeDuration) => {
let minRangeDuration = [];
let maxRangeDuration = [];
if (_minRangeDuration) {
if (Array.isArray(_minRangeDuration)) {
_minRangeDuration.map(mrd => {
let thisDate = new Date(mrd.date);
thisDate.setHours(12, 0, 0, 0);
minRangeDuration.push({
date: thisDate.valueOf(),
minDuration: mrd.minDuration
});
});
} else {
minRangeDuration = _minRangeDuration;
}
}
if (_maxRangeDuration) {
if (Array.isArray(_maxRangeDuration)) {
_maxRangeDuration.map(mrd => {
let thisDate = new Date(mrd.date);
thisDate.setHours(12, 0, 0, 0);
maxRangeDuration.push({
date: thisDate.valueOf(),
maxDuration: mrd.maxDuration
});
});
} else {
maxRangeDuration = _maxRangeDuration;
}
}
return { minRangeDuration, maxRangeDuration };
}
goToDate = (date = new Date(), options = {}) => {
if (this.props.scrollable) {
console.error('goToDate() is not supported when scrollable is true');
return;
}
const defaultOptions = {
/*
* isSelected - if true, the date will be shown as selected.
* useful when you have different style for today and selected date.
*/
isSelected: true,
...options,
}
const cloneDate = new Date(date);
const fullYear = cloneDate.getFullYear();
const fullMonth = cloneDate.getMonth();
const fullDay = cloneDate.getDate();
if (!Utils.isSameMonthAndYear(cloneDate, this.state.selectedStartDate)) {
if (defaultOptions.isSelected) {
this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay, setAsCurrentDate: true });
const currentMonthYear = new Date(fullYear, fullMonth, 1, 12);
this.props.onMonthChange && this.props.onMonthChange(currentMonthYear);
} else {
this.handleOnPressFinisher({ year: fullYear, month: fullMonth, scrollFinisher: null, extraState: null });
}
} else {
// the date is in the same month and year as the selected date
if (defaultOptions.isSelected) {
this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay });
}
}
}
handleOnPressDay = ({ year, month, day }) => {
const {
selectedStartDate: prevSelectedStartDate,
selectedEndDate: prevSelectedEndDate,
} = this.state;
const {
allowRangeSelection,
allowBackwardRangeSelect,
enableDateChange,
onDateChange,
} = this.props;
if (!enableDateChange) {
return;
}
const date = new Date(year, month, day, 12);
if (allowRangeSelection && prevSelectedStartDate && !prevSelectedEndDate) {
if (isAfter(date, prevSelectedStartDate)) {
const selectedStartDate = prevSelectedStartDate;
const selectedEndDate = date;
this.setState({
selectedEndDate,
renderMonthParams: this.createMonthProps({ ...this.state, selectedStartDate, selectedEndDate }),
});
// Sync end date with parent
onDateChange(date, Utils.END_DATE);
}
else if (allowBackwardRangeSelect) { // date is before selectedStartDate
// Flip dates so that start is always before end.
const selectedEndDate = new Date(prevSelectedStartDate);
const selectedStartDate = date;
this.setState({
selectedStartDate,
selectedEndDate,
renderMonthParams: this.createMonthProps({ ...this.state, selectedStartDate, selectedEndDate }),
}, () => {
// Sync both start and end dates with parent *after* state update.
onDateChange(this.state.selectedStartDate, Utils.START_DATE);
onDateChange(this.state.selectedEndDate, Utils.END_DATE);
});
}
} else {
const syncEndDate = !!prevSelectedEndDate;
const selectedStartDate = date;
const selectedEndDate = null;
this.setState({
selectedStartDate,
selectedEndDate,
renderMonthParams: this.createMonthProps({ ...this.state, selectedStartDate, selectedEndDate }),
}, () => {
// Sync start date with parent *after* state update.
onDateChange(this.state.selectedStartDate, Utils.START_DATE);
if (syncEndDate) {
// sync end date with parent - must be cleared if previously set.
onDateChange(null, Utils.END_DATE);
}
});
}
}
handleOnPressPrevious = () => {
const { currentMonth, currentYear } = this.state;
let previousMonth = currentMonth - 1;
let year = currentYear;
// if previousMonth is negative it means the current month is January,
// so we have to go back to previous year and set the current month to December
if (previousMonth < 0) {
previousMonth = 11;
year--;
}
const scrollFinisher = this.props.scrollable && this.scroller.scrollLeft;
this.handleOnPressFinisher({ year, month: previousMonth, scrollFinisher });
}
handleOnPressNext = () => {
const { currentMonth, currentYear } = this.state;
let nextMonth = currentMonth + 1;
let year = currentYear;
// if nextMonth is greater than 11 it means the current month is December,
// so we have to go forward to the next year and set the current month to January
if (nextMonth > 11) {
nextMonth = 0;
year++;
}
const scrollFinisher = this.props.scrollable && this.scroller.scrollRight;
this.handleOnPressFinisher({ year, month: nextMonth, scrollFinisher });
}
handleOnPressFinisher = ({ year, month, scrollFinisher, extraState }) => {
if (scrollFinisher) {
scrollFinisher();
}
else {
const currentMonth = parseInt(month);
const currentYear = parseInt(year);
const renderMonthParams = extraState || {
renderMonthParams: { ...this.state.renderMonthParams, month, year }
};
this.setState({ currentMonth, currentYear, ...renderMonthParams });
}
const currentMonthYear = new Date(year, month, 1, 12);
this.props.onMonthChange && this.props.onMonthChange(currentMonthYear);
}
handleOnPressYear = () => {
this.setState({
currentView: 'years'
});
}
handleOnPressMonth = () => {
this.setState({
currentView: 'months'
});
}
handleOnSelectMonthYear = ({ month, year }) => {
const currentYear = year;
const currentMonth = month;
const scrollableState = this.props.scrollable ? {
...this.createMonths(this.props, { currentYear, currentMonth }),
} : {};
const extraState = {
renderMonthParams: { ...this.state.renderMonthParams, month, year },
currentView: 'days',
...scrollableState,
};
this.handleOnPressFinisher({ month, year, extraState });
}
resetSelections = () => {
this.setState((state) => ({
selectedStartDate: null,
selectedEndDate: null,
renderMonthParams: {
...state.renderMonthParams,
selectedStartDate: null,
selectedEndDate: null,
}
}));
}
createMonthProps = state => {
return {
onPressDay: this.handleOnPressDay,
month: state.currentMonth,
year: state.currentYear,
styles: state.styles,
disabledDates: state.disabledDates,
minDate: state.minDate,
maxDate: state.maxDate,
minRangeDuration: state.minRangeDuration,
maxRangeDuration: state.maxRangeDuration,
selectedStartDate: state.selectedStartDate,
selectedEndDate: state.selectedEndDate,
enableDateChange: this.props.enableDateChange,
firstDay: this.props.startFromMonday ? 1 : this.props.firstDay,
allowRangeSelection: this.props.allowRangeSelection,
allowBackwardRangeSelect: this.props.allowBackwardRangeSelect,
showDayStragglers: this.props.showDayStragglers,
disabledDatesTextStyle: this.props.disabledDatesTextStyle,
textStyle: this.props.textStyle,
todayTextStyle: this.props.todayTextStyle,
selectedDayTextStyle: this.props.selectedDayTextStyle,
selectedRangeStartTextStyle: this.props.selectedRangeStartTextStyle,
selectedRangeEndTextStyle: this.props.selectedRangeEndTextStyle,
selectedDayStyle: this.props.selectedDayStyle,
selectedDisabledDatesTextStyle: this.props.selectedDisabledDatesTextStyle,
selectedRangeStartStyle: this.props.selectedRangeStartStyle,
selectedRangeStyle: this.props.selectedRangeStyle,
selectedRangeEndStyle: this.props.selectedRangeEndStyle,
customDatesStyles: this.props.customDatesStyles,
fontScaling: this.props.fontScaling,
};
}
createMonths = (props, { currentMonth, currentYear }) => {
if (!props.scrollable) {
return [];
}
const {
initialDate,
minDate,
maxDate,
restrictMonthNavigation,
} = props;
let monthsList = [];
let numMonths = this.numMonthsScroll;
let initialScrollerIndex = 0;
// Center start month in scroller. Visible month is either the initialDate
// prop, or the current month & year that has been selected.
let _initialDate = Number.isInteger(currentMonth) && Number.isInteger(currentYear) &&
new Date(currentYear, currentMonth, 1, 12);
_initialDate = _initialDate || initialDate;
let firstScrollerMonth = subMonths(_initialDate, numMonths / 2);
if (minDate && restrictMonthNavigation && isBefore(startOfMonth(firstScrollerMonth), startOfMonth(minDate))) {
firstScrollerMonth = new Date(minDate);
}
for (let i = 0; i < numMonths; i++) {
let month = addMonths(firstScrollerMonth, i);
if (maxDate && restrictMonthNavigation && isAfter(startOfMonth(month), startOfMonth(maxDate))) {
break;
}
if (isSameMonth(month, _initialDate)) {
initialScrollerIndex = i;
}
monthsList.push(month);
}
return {
monthsList,
initialScrollerIndex,
};
}
renderMonth(props) {
return (
<DaysGridView {...props} />
);
}
render() {
const {
currentView,
currentMonth,
currentYear,
minDate,
maxDate,
styles,
monthsList,
renderMonthParams,
initialScrollerIndex,
} = this.state;
const {
startFromMonday,
firstDay,
initialDate,
weekdays,
months,
previousComponent,
nextComponent,
previousTitle,
nextTitle,
previousTitleStyle,
nextTitleStyle,
monthTitleStyle,
yearTitleStyle,
textStyle,
restrictMonthNavigation,
headingLevel,
dayLabelsWrapper,
customDayHeaderStyles,
selectMonthTitle,
selectYearTitle,
monthYearHeaderWrapperStyle,
headerWrapperStyle,
onMonthChange,
scrollable,
horizontal,
scrollDecelarationRate,
} = this.props;
let content;
switch (currentView) {
case 'months':
content = (
<MonthSelector
styles={styles}
textStyle={textStyle}
title={selectMonthTitle}
currentYear={currentYear}
months={months}
minDate={minDate}
maxDate={maxDate}
onSelectMonth={this.handleOnSelectMonthYear}
headingLevel={headingLevel}
/>
);
break;
case 'years':
content = (
<YearSelector
styles={styles}
textStyle={textStyle}
title={selectYearTitle}
initialDate={new Date(initialDate)}
currentMonth={currentMonth}
currentYear={currentYear}
minDate={minDate}
maxDate={maxDate}
restrictNavigation={restrictMonthNavigation}
previousComponent={previousComponent}
nextComponent={nextComponent}
previousTitle={previousTitle}
nextTitle={nextTitle}
previousTitleStyle={previousTitleStyle}
nextTitleStyle={nextTitleStyle}
onSelectYear={this.handleOnSelectMonthYear}
headingLevel={headingLevel}
/>
);
break;
default:
content = (
<View styles={styles.calendar}>
<HeaderControls
styles={styles}
currentMonth={currentMonth}
currentYear={currentYear}
initialDate={new Date(initialDate)}
onPressPrevious={this.handleOnPressPrevious}
onPressNext={this.handleOnPressNext}
onPressMonth={this.handleOnPressMonth}
onPressYear={this.handleOnPressYear}
months={months}
previousComponent={previousComponent}
nextComponent={nextComponent}
previousTitle={previousTitle}
nextTitle={nextTitle}
previousTitleStyle={previousTitleStyle}
nextTitleStyle={nextTitleStyle}
monthTitleStyle={monthTitleStyle}
yearTitleStyle={yearTitleStyle}
textStyle={textStyle}
restrictMonthNavigation={restrictMonthNavigation}
minDate={minDate}
maxDate={maxDate}
headingLevel={headingLevel}
monthYearHeaderWrapperStyle={monthYearHeaderWrapperStyle}
headerWrapperStyle={headerWrapperStyle}
/>
<Weekdays
styles={styles}
firstDay={startFromMonday ? 1 : firstDay}
currentMonth={currentMonth}
currentYear={currentYear}
weekdays={weekdays}
textStyle={textStyle}
dayLabelsWrapper={dayLabelsWrapper}
customDayHeaderStyles={customDayHeaderStyles}
/>
{scrollable ?
<Scroller
ref={scroller => this.scroller = scroller}
data={monthsList}
renderMonth={this.renderMonth}
renderMonthParams={renderMonthParams}
maxSimultaneousMonths={this.numMonthsScroll}
initialRenderIndex={initialScrollerIndex}
minDate={minDate}
maxDate={maxDate}
restrictMonthNavigation={restrictMonthNavigation}
updateMonthYear={this.updateMonthYear}
onMonthChange={onMonthChange}
horizontal={horizontal}
scrollDecelarationRate={scrollDecelarationRate}
/>
:
this.renderMonth(renderMonthParams)
}
</View>
);
}
return content;
}
}