-
Notifications
You must be signed in to change notification settings - Fork 23
/
CalendarList.js
527 lines (465 loc) · 16.8 KB
/
CalendarList.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
519
520
521
522
523
524
525
526
527
import React, {Component} from 'react';
import {FlatList, View} from 'react-native';
import PropTypes from 'prop-types';
import * as Constants from "../contants";
import WeekBar from "./components/WeekBar";
import ToolBar from "../components/ToolBar";
import ListItem from "./components/ListItem";
import {calculateCalendarDefaultDates} from '../contants';
class CalendarList extends Component {
constructor(props) {
super(props);
const {
startDate,
endDate,
} = calculateCalendarDefaultDates(props.defaultDates);
this.state = {
dataSource: [],
startDate,
endDate,
};
}
componentDidMount() {
const {minDate, maxDate, firstDayOnWeeks} = this.props;
this.setState({
dataSource: Constants.getCalendarDates(Constants.validateDate(minDate), Constants.validateDate(maxDate), firstDayOnWeeks),
});
}
/**
* Select date call back with date parameter.
* @param date A date string representing the date selected such as '2020-5-11'.
* @param index The click index.
*/
_selectDate = (date, index) => {
this.props.onPressDate(Constants.toStandardStringWith(date), index);
const {startDate, endDate} = this.state;
if (startDate && endDate) {
this.setState({
startDate: date,
endDate: '',
});
return;
}
if (startDate) {
const isBigger = Constants.greaterThan(startDate, date);
this.setState({
startDate: isBigger ? date : startDate,
endDate: isBigger ? startDate : date,
});
} else {
this.setState({startDate: date});
}
};
_renderItem = ({item, index}) => {
const {
minDate,
maxDate,
headerTitleType,
listItemStyle,
selectedDateMarkColor,
selectedDateMarkRangeColor,
beyondDatesDisabled,
beyondDatesDisabledTextColor,
horizontal,
rightArrowClick,
leftArrowClick,
hideArrow,
arrowAlign,
arrowColor,
arrowSize,
} = this.props;
return <ListItem
{...this.props}
item={item}
startDate={this.state.startDate}
endDate={this.state.endDate}
minDate={Constants.validateDate(minDate)}
maxDate={Constants.validateDate(maxDate)}
selectDate={this._selectDate}
headerTitleType={headerTitleType}
listItemStyle={listItemStyle}
selectedDateMarkColor={selectedDateMarkColor}
selectedDateMarkRangeColor={selectedDateMarkRangeColor}
beyondDatesDisabled={beyondDatesDisabled}
beyondDatesDisabledTextColor={beyondDatesDisabledTextColor}
horizontal={horizontal}
leftArrowClick={() => {
if (leftArrowClick && typeof leftArrowClick === 'function') {
leftArrowClick(index);
return;
}
if (this.props.inverted) {
if (index !== this.state.dataSource.length - 1) {
this.flatList.scrollToIndex({index: index + 1});
}
} else {
if (index !== 0) {
this.flatList.scrollToIndex({index: index - 1});
}
}
}}
rightArrowClick={() => {
if (rightArrowClick && typeof rightArrowClick === 'function') {
rightArrowClick(index);
return;
}
if (this.props.inverted) {
if (index !== 0) {
this.flatList.scrollToIndex({index: index - 1});
}
} else {
if (index !== this.state.dataSource.length - 1) {
this.flatList.scrollToIndex({index: index + 1});
}
}
}}
hideArrow={hideArrow}
arrowColor={arrowColor}
arrowSize={arrowSize}
arrowAlign={arrowAlign}
/>;
};
/**
* See FlatList docs. If you'd like to do something outside. e.g: you maybe want to scroll to the end. Then you can
* use "<CalendarList ref={ref => this.calendarList = ref}; this.calendarList.scrollToEnd()" to make it work for you.
*/
scrollToIndex = params => this.flatList.scrollToIndex(params);
scrollToItem = params => this.flatList.scrollToItem(params);
scrollToOffset = params => this.flatList.scrollToOffset(params);
scrollToEnd = params => this.flatList.scrollToEnd(params);
render() {
const {
horizontal,
showsHorizontalScrollIndicator,
showsVerticalScrollIndicator,
scrollEnabled,
pagingEnabled,
containerStyle,
scrollContentStyle,
showToolBar,
toolBarPosition,
toolBarStyle,
toolBarCancelStyle,
toolBarConfirmStyle,
titleStyle,
titleText,
cancelText,
confirmText,
cancel,
confirm,
cancelDisabled,
confirmDisabled,
showWeeks,
weeks,
weeksChineseType,
firstDayOnWeeks,
weeksStyle,
weeksTextStyle,
} = this.props;
const _wks = weeksChineseType && weeks === Constants.DEFAULT_WEEK_EN ? Constants.DEFAULT_WEEK_ZH : weeks;
const _weeks = Constants.getWeekDays(_wks, firstDayOnWeeks);
const _toolBar = (<ToolBar
style={toolBarStyle}
cancelStyle={toolBarCancelStyle}
confirmStyle={toolBarConfirmStyle}
titleStyle={titleStyle}
titleText={titleText}
cancelText={cancelText}
cancel={() => cancel && typeof cancel === 'function' && cancel()}
confirm={() => {
const {startDate, endDate} = this.state;
const dates = [Constants.toStandardStringWith(startDate), Constants.toStandardStringWith(endDate)];
confirm && typeof confirm === 'function' && confirm(dates);
}}
confirmText={confirmText}
cancelDisabled={cancelDisabled}
confirmDisabled={confirmDisabled}
/>);
return (
<View style={containerStyle}>
{showToolBar && toolBarPosition === Constants.DEFAULT_TOOL_BAR_POSITION.TOP && _toolBar}
{showWeeks && !horizontal && <WeekBar
weeks={_weeks}
style={weeksStyle}
textStyle={weeksTextStyle}
/>}
<FlatList
style={scrollContentStyle}
automaticallyAdjustContentInsets={false}
ref={ref => this.flatList = ref}
keyExtractor={item => `${item.year}-${item.month}`}
data={this.state.dataSource}
extraData={this.state}
horizontal={horizontal}
showsHorizontalScrollIndicator={showsHorizontalScrollIndicator}
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
scrollEnabled={scrollEnabled}
pagingEnabled={pagingEnabled}
initialNumToRender={5}
{...this.props}
renderItem={this._renderItem}
/>
{showToolBar && toolBarPosition === Constants.DEFAULT_TOOL_BAR_POSITION.BOTTOM && _toolBar}
</View>
);
}
}
CalendarList.propTypes = {
/**
* Seen as FlatList component.
*/
horizontal: PropTypes.bool,
/**
* Seen as FlatList component.
*/
showsHorizontalScrollIndicator: PropTypes.bool,
/**
* Seen as FlatList component.
*/
showsVerticalScrollIndicator: PropTypes.bool,
/**
* Seen as FlatList component. Default is true.
*/
scrollEnabled: PropTypes.bool,
/**
* Seen as FlatList component. Default is false.
*/
pagingEnabled: PropTypes.bool,
/**
* Styles for container, you can set it as any view prop styles such as {backgroundColor: 'red'}
*/
containerStyle: PropTypes.object,
/**
* Styles for scroll list - FlatList, you can set it as any view prop styles such as {backgroundColor: 'red'}
*/
scrollContentStyle: PropTypes.object,
/**
* Whether to show tool bar, default is true. If false, hide tool bar on top.
*/
showToolBar: PropTypes.bool,
/**
* The position of tool bar, default is 'top' that is at the top of screen. So far, just both 'top' and 'bottom'
* are supported.
*/
toolBarPosition: PropTypes.string,
/**
* tool bar view styles, passed like {backgroundColor: 'red'} as you like.
*/
toolBarStyle: PropTypes.object,
/**
* tool bar cancel button text styles, passed like {color: 'red', fontSize: 15} as you like.
* Note that you can control the active opacity of the button through {activeOpacity: 1}.
*/
toolBarCancelStyle: PropTypes.object,
/**
* tool bar confirm button text styles, passed like {color: 'red', fontSize: 15} as you like.
* Note that you can control the active opacity of the button through {activeOpacity: 1}.
*/
toolBarConfirmStyle: PropTypes.object,
/**
* tool bar title text style.
*/
titleStyle: PropTypes.object,
/**
* tool bar title text, default is "".
*/
titleText: PropTypes.string,
/**
* tool bar cancel button text, default is "Cancel".
*/
cancelText: PropTypes.string,
/**
* tool bar confirm button text, default is "Confirm".
*/
confirmText: PropTypes.string,
/**
* tool bar cancel button callback.
*/
cancel: PropTypes.func,
/**
* tool bar confirm button callback with a date array like ["2016-01-09", "2019-09-18"]. "2016-01-09" is the
* start date(min date) you selected. "2019-09-18" is the end date(max date) you selected. If nothing is selected,
* the array's elements will be empty string like ["", ""].
*/
confirm: PropTypes.func,
/**
* Whether to disable the cancel button. Default is false.
*/
cancelDisabled: PropTypes.bool,
/**
* Whether to disable the confirm button. Default is false.
*/
confirmDisabled: PropTypes.bool,
/**
* A callback with a date parameter(like "2019-08-09") and row index when the user presses some date item.
*/
onPressDate: PropTypes.func,
/**
* Min date to limit, default is "2000-01-01". Other supported formats: "2000-1-1", "2000/01/01", "2000/1/1".
* Note: Date type is also supported.
*/
minDate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.instanceOf(Date),
]),
/**
* Max date to limit, default is today calculated by new Date(). Other supported formats: "2015-1-1", "2015/01/01", "2015/1/1".
* Note: Date type is also supported.
*/
maxDate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.instanceOf(Date),
]),
/**
* The default dates for the initial selected values.
* Note: the count of defaultDates must be equal to 2. That is to say, you must pass two elements for it.
* Both string and Date type are also supported like minDate or maxDate above.
* For instance: defaultDates={['2015-10-10', '2020-01-01']} or defaultDates={['2015-10-10', new Date()]}.
* Attention! The first element must be greater than or equal to the minDate. The second element must be
* less than or equal to the maxDate. Or it will not work as you expected.
*/
defaultDates: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.string,
PropTypes.instanceOf(Date),
]),
),
/**
* Whether to show weeks, default is true.
*/
showWeeks: PropTypes.bool,
/**
* Week days to show, default is from Sunday to Saturday, namely ['Su','Mo','Tu','We','Th','Fr','Sa'].
* Note that if you want to custom "weeks", then you have to accomplish "firstDayOnWeeks" at the same time.
* For example, you passed "['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']" to "weeks", you must pass 1 to
* "firstDayOnWeeks" equal to "firstDayOnWeeks={1}". What's more, 1 means Monday, 2 means Tuesday, ..., 0 Means Sunday.
*/
weeks: PropTypes.array,
/**
* Weeks type. Default is false, namely ['Su','Mo','Tu','We','Th','Fr','Sa']. If you want to use chinese, such as
* ['日','一','二','三','四','五','六'], just setting "weeksChineseType={true}" is okay. But the precondition is that
* "weeks" above uses the default value. Or it will be invalid.
*/
weeksChineseType: PropTypes.bool,
/**
* The first day for weeks. Default is 0 equal to Sunday. If you'd like to start with Saturday, "firstDayOnWeeks={6}"
* will work. The value ranges from 0 to 6.
*/
firstDayOnWeeks: PropTypes.number,
/**
* For week days, set the container styles like {backgroundColor: 'red'}.
*/
weeksStyle: PropTypes.object,
/**
* For week days, set the week day text styles like {color: 'blue', fontSize: 14}.
*/
weeksTextStyle: PropTypes.object,
/**
* Display form of the header title. Default is 0. Take "2020-04" date as an example:
* 0: "2020-04"
* 1: "2020年4月"
* 2: "Apr 2020"
* 3: "April 2020"
* 4: "2020/04"
* 5: "04/2020"
*/
headerTitleType: PropTypes.number,
/**
* Content styles containing header title and days content. This is a nesting object style. So if you want to set
* some specific style such as "headerTitle", you can set it to {headerTitle: {fontSize: 18, color: 'red'}}. Details
* are as follows:
{
container: {
flex: 1,
},
headerTitleContainer: {
height: 50,
},
headerTitle: {
fontSize: 17,
},
dayContent: {
backgroundColor: 'red',
},
day: {
color: '#3e3e3e',
},
};
*/
listItemStyle: PropTypes.object,
/**
* Selected date mark type. Default is 'ellipse', other choices: 'semiellipse'、'rectangle'、'circle'、 'square'、'dot'.
*/
selectedDateMarkType: PropTypes.string,
/**
* Selected date mark background color for start date and end date. Default is 'magenta'.
*/
selectedDateMarkColor: PropTypes.string,
/**
* Selected date mark background color between start date and end date. Default is 'skyblue'.
*/
selectedDateMarkRangeColor: PropTypes.string,
/**
* When the date is out of minDate or maxDate, whether to disable the button. Default is true.
*/
beyondDatesDisabled: PropTypes.bool,
/**
* When the date is out of minDate or maxDate, the button text color. Default is '#b9b9b9'.
*/
beyondDatesDisabledTextColor: PropTypes.string,
/**
* Left arrow click callback with current date index parameter. Only when "horizontal={true}".
*/
leftArrowClick: PropTypes.func,
/**
* Right arrow click callback with current date index parameter. Only when "horizontal={true}".
*/
rightArrowClick: PropTypes.func,
/**
* Whether to hide arrow. Default is false. Only when "horizontal={true}".
*/
hideArrow: PropTypes.bool,
/**
* Arrow color. Default is 'gray'. Only when "horizontal={true}".
*/
arrowColor: PropTypes.string,
/**
* Arrow size. Default is 8. Only when "horizontal={true}".
*/
arrowSize: PropTypes.number,
/**
* Arrow align. Default is 'left'. One of ['left', 'center', 'right']. Only when "horizontal={true}".
*/
arrowAlign: PropTypes.string,
};
CalendarList.defaultProps = {
horizontal: false,
scrollEnabled: true,
pagingEnabled: false,
showToolBar: true,
toolBarPosition: Constants.DEFAULT_TOOL_BAR_POSITION.TOP,
cancelText: Constants.DEFAULT_CANCEL_TEXT,
confirmText: Constants.DEFAULT_CONFIRM_TEXT,
cancel: () => {
},
confirm: () => {
},
onPressDate: () => {
},
cancelDisabled: false,
confirmDisabled: false,
minDate: Constants.DEFAULT_MIN_DATE,
maxDate: Constants.DEFAULT_MAX_DATE,
showWeeks: true,
weeks: Constants.DEFAULT_WEEK_EN,
weeksChineseType: false,
firstDayOnWeeks: 0,
headerTitleType: 0,
listItemStyle: {},
selectedDateMarkType: Constants.DEFAULT_DATE_MARK_TYPE.ELLIPSE,
selectedDateMarkColor: 'magenta',
selectedDateMarkRangeColor: 'skyblue',
beyondDatesDisabled: true,
beyondDatesDisabledTextColor: '#b9b9b9',
};
export default CalendarList;