-
Notifications
You must be signed in to change notification settings - Fork 325
/
CalendarDay.js
518 lines (472 loc) · 15.8 KB
/
CalendarDay.js
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
/**
* Created by bogdanbegovic on 8/20/16.
*/
import React, { Component } from "react";
import PropTypes from "prop-types";
import moment from "moment";
import { Text, View, Animated, Easing, LayoutAnimation, TouchableOpacity } from "react-native";
import styles from "./Calendar.style.js";
class CalendarDay extends Component {
static propTypes = {
date: PropTypes.object.isRequired,
selectedDate: PropTypes.any,
onDateSelected: PropTypes.func.isRequired,
dayComponent: PropTypes.any,
datesWhitelist: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
datesBlacklist: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
markedDates: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
showDayName: PropTypes.bool,
showDayNumber: PropTypes.bool,
calendarColor: PropTypes.string,
size: PropTypes.number,
dateNameStyle: PropTypes.any,
dateNumberStyle: PropTypes.any,
dayContainerStyle: PropTypes.any,
weekendDateNameStyle: PropTypes.any,
weekendDateNumberStyle: PropTypes.any,
highlightDateContainerStyle: PropTypes.any,
highlightDateNameStyle: PropTypes.any,
highlightDateNumberStyle: PropTypes.any,
disabledDateNameStyle: PropTypes.any,
disabledDateNumberStyle: PropTypes.any,
disabledDateOpacity: PropTypes.number,
styleWeekend: PropTypes.bool,
customDatesStyles: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
markedDatesStyle: PropTypes.object,
allowDayTextScaling: PropTypes.bool,
calendarAnimation: PropTypes.object,
registerAnimation: PropTypes.func.isRequired,
daySelectionAnimation: PropTypes.object,
useNativeDriver: PropTypes.bool,
scrollable: PropTypes.bool,
};
// Reference: https://medium.com/@Jpoliachik/react-native-s-layoutanimation-is-awesome-4a4d317afd3e
static defaultProps = {
daySelectionAnimation: {
type: "", // animations disabled by default
duration: 300,
borderWidth: 1,
borderHighlightColor: "black",
highlightColor: "yellow",
animType: LayoutAnimation.Types.easeInEaseOut,
animUpdateType: LayoutAnimation.Types.easeInEaseOut,
animProperty: LayoutAnimation.Properties.opacity,
animSpringDamping: undefined // Only applicable for LayoutAnimation.Types.spring,
},
styleWeekend: true,
showDayName: true,
showDayNumber: true
};
constructor(props) {
super(props);
this.state = {
enabled: this.isDateAllowed(props.date, props.datesBlacklist, props.datesWhitelist),
selected: this.isDateSelected(props.date, props.selectedDate),
customStyle: this.getCustomDateStyle(props.date, props.customDatesStyles),
marking: this.getDateMarking(props.date, props.markedDates),
animatedValue: new Animated.Value(0),
...this.calcSizes(props)
};
if (!props.scrollable) {
props.registerAnimation(this.createAnimation());
}
}
componentDidUpdate(prevProps, prevState) {
let newState = {};
let doStateUpdate = false;
let hasDateChanged = prevProps.date !== this.props.date;
if ((this.props.selectedDate !== prevProps.selectedDate) || hasDateChanged) {
if (this.props.daySelectionAnimation.type !== "" && !this.props.scrollable) {
let configurableAnimation = {
duration: this.props.daySelectionAnimation.duration || 300,
create: {
type:
this.props.daySelectionAnimation.animType ||
LayoutAnimation.Types.easeInEaseOut,
property:
this.props.daySelectionAnimation.animProperty ||
LayoutAnimation.Properties.opacity
},
update: {
type:
this.props.daySelectionAnimation.animUpdateType ||
LayoutAnimation.Types.easeInEaseOut,
springDamping: this.props.daySelectionAnimation.animSpringDamping
},
delete: {
type:
this.props.daySelectionAnimation.animType ||
LayoutAnimation.Types.easeInEaseOut,
property:
this.props.daySelectionAnimation.animProperty ||
LayoutAnimation.Properties.opacity
}
};
LayoutAnimation.configureNext(configurableAnimation);
}
newState.selected = this.isDateSelected(this.props.date, this.props.selectedDate);
doStateUpdate = true;
}
if (prevProps.size !== this.props.size) {
newState = { ...newState, ...this.calcSizes(this.props) };
doStateUpdate = true;
}
if ((prevProps.customDatesStyles !== this.props.customDatesStyles) || hasDateChanged) {
newState = { ...newState, customStyle: this.getCustomDateStyle(this.props.date, this.props.customDatesStyles) };
doStateUpdate = true;
}
if ((prevProps.markedDates !== this.props.markedDates) || hasDateChanged) {
newState = { ...newState, marking: this.getDateMarking(this.props.date, this.props.markedDates) };
doStateUpdate = true;
}
if ((prevProps.datesBlacklist !== this.props.datesBlacklist) ||
(prevProps.datesWhitelist !== this.props.datesWhitelist) ||
hasDateChanged)
{
newState = { ...newState, enabled: this.isDateAllowed(this.props.date, this.props.datesBlacklist, this.props.datesWhitelist) };
doStateUpdate = true;
}
if (doStateUpdate) {
this.setState(newState);
}
}
calcSizes = props => {
return {
containerSize: Math.round(props.size),
containerBorderRadius: Math.round(props.size / 2),
dateNameFontSize: Math.round(props.size / 5),
dateNumberFontSize: Math.round(props.size / 2.9)
};
}
//Function to check if provided date is the same as selected one, hence date is selected
//using isSame moment query with "day" param so that it check years, months and day
isDateSelected = (date, selectedDate) => {
if (!date || !selectedDate) {
return date === selectedDate;
}
return date.isSame(selectedDate, "day");
}
// Check whether date is allowed
isDateAllowed = (date, datesBlacklist, datesWhitelist) => {
// datesBlacklist entries override datesWhitelist
if (Array.isArray(datesBlacklist)) {
for (let disallowed of datesBlacklist) {
// Blacklist start/end object
if (disallowed.start && disallowed.end) {
if (date.isBetween(disallowed.start, disallowed.end, "day", "[]")) {
return false;
}
} else {
if (date.isSame(disallowed, "day")) {
return false;
}
}
}
} else if (datesBlacklist instanceof Function) {
return !datesBlacklist(date);
}
// Whitelist
if (Array.isArray(datesWhitelist)) {
for (let allowed of datesWhitelist) {
// start/end object
if (allowed.start && allowed.end) {
if (date.isBetween(allowed.start, allowed.end, "day", "[]")) {
return true;
}
} else {
if (date.isSame(allowed, "day")) {
return true;
}
}
}
return false;
} else if (datesWhitelist instanceof Function) {
return datesWhitelist(date);
}
return true;
}
getCustomDateStyle = (date, customDatesStyles) => {
if (Array.isArray(customDatesStyles)) {
for (let customDateStyle of customDatesStyles) {
if (customDateStyle.endDate) {
// Range
if (
date.isBetween(
customDateStyle.startDate,
customDateStyle.endDate,
"day",
"[]"
)
) {
return customDateStyle;
}
} else {
// Single date
if (date.isSame(customDateStyle.startDate, "day")) {
return customDateStyle;
}
}
}
} else if (customDatesStyles instanceof Function) {
return customDatesStyles(date);
}
}
getDateMarking = (day, markedDates) => {
if (Array.isArray(markedDates)) {
if (markedDates.length === 0) {
return {};
}
return markedDates.find(md => moment(day).isSame(md.date, "day")) || {};
} else if (markedDates instanceof Function) {
return markedDates(day) || {};
}
}
createAnimation = () => {
const {
calendarAnimation,
useNativeDriver,
} = this.props
if (calendarAnimation) {
this.animation = Animated.timing(this.state.animatedValue, {
toValue: 1,
duration: calendarAnimation.duration,
easing: Easing.linear,
useNativeDriver,
});
// Individual CalendarDay animation starts have unpredictable timing
// when used with delays in RN Animated.
// Send animation to parent to collect and start together.
return this.animation;
}
}
renderMarking() {
if (!this.props.markedDates || this.props.markedDates.length === 0) {
return;
}
const marking = this.state.marking;
if (marking.dots && Array.isArray(marking.dots) && marking.dots.length > 0) {
return this.renderDots(marking);
}
if (marking.lines && Array.isArray(marking.lines) && marking.lines.length > 0) {
return this.renderLines(marking);
}
return ( // default empty spacer
<View style={styles.dotsContainer}>
<View style={[styles.dot]} />
</View>
);
}
renderDots(marking) {
const baseDotStyle = [styles.dot, styles.visibleDot];
const markedDatesStyle = this.props.markedDatesStyle || {};
const formattedDate = this.props.date.format('YYYY-MM-DD');
let validDots = <View style={[styles.dot]} />; // default empty view for no dots case
// Filter dots and process only those which have color property
validDots = marking.dots
.filter(d => (d && d.color))
.map((dot, index) => {
const selectedColor = dot.selectedColor || dot.selectedDotColor; // selectedDotColor deprecated
const backgroundColor = this.state.selected && selectedColor ? selectedColor : dot.color;
return (
<View
key={dot.key || (formattedDate + index)}
style={[
baseDotStyle,
{ backgroundColor },
markedDatesStyle
]}
/>
);
});
return (
<View style={styles.dotsContainer}>
{validDots}
</View>
);
}
renderLines(marking) {
const baseLineStyle = [styles.line, styles.visibleLine];
const markedDatesStyle = this.props.markedDatesStyle || {};
let validLines = <View style={[styles.line]} />; // default empty view
// Filter lines and process only those which have color property
validLines = marking.lines
.filter(d => (d && d.color))
.map((line, index) => {
const backgroundColor = this.state.selected && line.selectedColor ? line.selectedColor : line.color;
const width = this.props.size * 0.6;
return (
<View
key={line.key ? line.key : index}
style={[
baseLineStyle,
{ backgroundColor, width },
markedDatesStyle
]}
/>
);
});
return (
<View style={styles.linesContainer}>
{validLines}
</View>
);
}
render() {
// Defaults for disabled state
const {
date,
dateNameStyle,
dateNumberStyle,
dayContainerStyle,
disabledDateNameStyle,
disabledDateNumberStyle,
disabledDateOpacity,
calendarAnimation,
daySelectionAnimation,
highlightDateNameStyle,
highlightDateNumberStyle,
highlightDateContainerStyle,
styleWeekend,
weekendDateNameStyle,
weekendDateNumberStyle,
onDateSelected,
showDayName,
showDayNumber,
allowDayTextScaling,
dayComponent: DayComponent,
scrollable,
} = this.props;
const {
enabled,
selected,
containerSize,
containerBorderRadius,
customStyle,
dateNameFontSize,
dateNumberFontSize,
} = this.state;
let _dateNameStyle = [styles.dateName, enabled ? dateNameStyle : disabledDateNameStyle];
let _dateNumberStyle = [styles.dateNumber, enabled ? dateNumberStyle : disabledDateNumberStyle];
let _dateViewStyle = enabled
? [{ backgroundColor: "transparent" }]
: [{ opacity: disabledDateOpacity }];
let _customHighlightDateNameStyle;
let _customHighlightDateNumberStyle;
if (customStyle) {
_dateNameStyle.push(customStyle.dateNameStyle);
_dateNumberStyle.push(customStyle.dateNumberStyle);
_dateViewStyle.push(customStyle.dateContainerStyle);
_customHighlightDateNameStyle = customStyle.highlightDateNameStyle;
_customHighlightDateNumberStyle = customStyle.highlightDateNumberStyle;
}
if (enabled) {
// Enabled state
//The user can disable animation, so that is why I use selection type
//If it is background, the user have to input colors for animation
//If it is border, the user has to input color for border animation
switch (daySelectionAnimation.type) {
case "background":
_dateViewStyle.push({ backgroundColor: daySelectionAnimation.highlightColor });
break;
case "border":
_dateViewStyle.push({
borderColor: daySelectionAnimation.borderHighlightColor,
borderWidth: daySelectionAnimation.borderWidth
});
break;
default:
// No animation styling by default
break;
}
_dateNameStyle = [styles.dateName, dateNameStyle];
_dateNumberStyle = [styles.dateNumber, dateNumberStyle];
if (styleWeekend &&
(date.isoWeekday() === 6 || date.isoWeekday() === 7)
) {
_dateNameStyle = [
styles.weekendDateName,
weekendDateNameStyle
];
_dateNumberStyle = [
styles.weekendDateNumber,
weekendDateNumberStyle
];
}
if (selected) {
_dateViewStyle.push(highlightDateContainerStyle);
_dateNameStyle = [
styles.dateName,
highlightDateNameStyle,
_customHighlightDateNameStyle
];
_dateNumberStyle = [
styles.dateNumber,
highlightDateNumberStyle,
_customHighlightDateNumberStyle
];
}
}
let responsiveDateContainerStyle = {
width: containerSize,
height: containerSize,
borderRadius: containerBorderRadius,
};
let day;
if (DayComponent) {
day = (<DayComponent {...this.props} {...this.state}/>);
}
else {
day = (
<TouchableOpacity
onPress={onDateSelected.bind(this, date)}
disabled={!enabled}
>
<View
style={[
styles.dateContainer,
responsiveDateContainerStyle,
_dateViewStyle,
dayContainerStyle
]}
>
{showDayName && (
<Text
style={[{ fontSize: dateNameFontSize }, _dateNameStyle]}
allowFontScaling={allowDayTextScaling}
>
{date.format("ddd").toUpperCase()}
</Text>
)}
{showDayNumber && (
<View>
<Text
style={[
{ fontSize: dateNumberFontSize },
_dateNumberStyle
]}
allowFontScaling={allowDayTextScaling}
>
{date.date()}
</Text>
{ this.renderMarking() }
</View>
)}
</View>
</TouchableOpacity>
);
}
return calendarAnimation && !scrollable ? (
<Animated.View style={[
styles.dateRootContainer,
{opacity: this.state.animatedValue}
]}>
{day}
</Animated.View>
) : (
<View style={styles.dateRootContainer}>
{day}
</View>
);
}
}
export default CalendarDay;