@momo-kits/calendar 0.79.6 → 0.80.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types.ts CHANGED
@@ -1,70 +1,198 @@
1
1
  import Moment from 'moment';
2
2
  import {ViewStyle} from 'react-native';
3
3
 
4
+ export type Holidays = {
5
+ solar: object;
6
+ lunar: any[];
7
+ };
8
+
9
+ export type PriceInfo = {
10
+ priceAsString: string;
11
+ isBestPrice: boolean;
12
+ };
13
+
14
+ export type PriceListData = {
15
+ inbound: {
16
+ [keyMonth: string]: {
17
+ [keyDay: string]: PriceInfo;
18
+ };
19
+ };
20
+ outbound: {
21
+ [keyMonth: string]: {
22
+ [keyDay: string]: PriceInfo;
23
+ };
24
+ };
25
+ };
26
+
4
27
  export type CalendarProProps = {
5
- startDate: Moment.Moment | number | null;
6
- endDate: Moment.Moment | number | null;
28
+ startDate: Date | null;
29
+ endDate: Date | null;
7
30
  maxDate: Date;
8
31
  minDate: Date;
9
- format: string;
10
32
  isShowLunar?: boolean;
11
- selectedDate: Date;
12
- isDoubleDateMode?: boolean;
13
- onDateChange: (day: number) => void;
14
- priceList: any;
33
+ selectedDate: Moment.Moment;
34
+ isDoubleDateMode: boolean;
35
+ onDateChange: (day: Moment.Moment) => void;
36
+ priceList: PriceListData;
15
37
  labelFrom?: string;
16
38
  labelTo?: string;
17
39
  isHideLabel?: boolean;
18
40
  isHideHoliday?: boolean;
19
41
  isOffLunar?: boolean;
20
- disabledWeekend?: boolean;
21
42
  onCallbackCalendar?: (lunar: string, nextStateShowLunar: boolean) => void;
22
- containerWidth: number;
43
+ disabledDays?: Date[];
23
44
  };
24
45
 
25
46
  export type CalendarProState = {
26
- startDate?: Moment.Moment | number | null;
27
- endDate?: Moment.Moment | number | null;
47
+ startDate: Date | null;
48
+ endDate: Date | null;
28
49
  showLunar?: boolean;
29
50
  tabSelected?: number;
30
- holidays?: any[];
51
+ holidays: {lunar: number; mixedLabel: string}[];
31
52
  ownUpdate?: boolean;
32
53
  temp?: any;
33
54
  headerKey?: string;
34
55
  };
35
56
 
36
57
  export type DoubleDate = {
37
- first: Moment.Moment | Date | null;
38
- second: Moment.Moment | Date | null;
58
+ first: Date | null;
59
+ second: Date | null;
39
60
  };
40
61
 
41
62
  export type CalendarProps = {
42
- doubleDate: DoubleDate;
43
- selectedDate: Moment.Moment;
44
- mode: 'doubleDate' | 'singleDate';
45
- id: number;
46
- onChangeTab: (id: number) => void;
47
- onDateChange: (date: DoubleDate | Date) => void;
48
- onCTAStateChange: (b: boolean) => void;
49
- onCallbackCalendar: (type: string, isDoubleDateMode: boolean) => void;
63
+ doubleDate?: DoubleDate;
64
+ selectedDate?: Moment.Moment;
65
+ mode?: 'doubleDate' | 'singleDate';
66
+ id?: number;
67
+ onChangeTab?: (id: number) => void;
68
+ onDateChange?: (date: DoubleDate | Date) => void;
69
+ onCTAStateChange?: (b: boolean) => void;
70
+ onCallbackCalendar?: (type: string, isDoubleDateMode: boolean) => void;
50
71
  headerFrom?: string;
51
72
  headerTo?: string;
52
73
  isOffLunar?: boolean;
53
74
  isHideHoliday?: boolean;
54
75
  isHiddenSwitch?: boolean;
55
76
  isShowLunar?: boolean;
56
- priceList?: any;
77
+ priceList?: PriceListData;
57
78
  labelFrom?: string;
58
79
  labelTo?: string;
59
80
  isHideLabel?: boolean;
60
- minDate: Date;
61
- maxDate: Date;
81
+ minDate?: Date;
82
+ maxDate?: Date;
62
83
  isHideHeaderPanel?: boolean;
63
- disabledWeekend?: boolean;
64
- style: ViewStyle | ViewStyle[];
84
+ style?: ViewStyle | ViewStyle[];
85
+ disabledDays?: Date[];
86
+ };
87
+
88
+ export type CalendarProRef = {
89
+ setDoubleDateAndTabIndex: (
90
+ firstDate?: Moment.Moment | null,
91
+ secondDate?: Moment.Moment | null,
92
+ tabSelected?: number,
93
+ ) => void;
94
+ setDateRange: (
95
+ dateRange: {startDate: any; endDate: any},
96
+ isScrollToStartDate: any,
97
+ ) => void;
65
98
  };
66
99
 
67
100
  export type CalendarState = {
68
101
  isDoubleDateMode: boolean;
69
102
  containerWidth: number;
70
103
  };
104
+
105
+ export type HeaderControlRef = {
106
+ onUpdateInfo: (date: {date: Moment.Moment}) => void;
107
+ };
108
+
109
+ export type HeaderControlProps = {
110
+ onPressBackArrow: () => void;
111
+ onPressNextArrow: () => void;
112
+ selectedDate: Moment.Moment;
113
+ };
114
+
115
+ export type MonthListProps = {
116
+ isDoubleDateMode: boolean;
117
+ selectedDate: Moment.Moment;
118
+ holidays?: {lunar: number; mixedLabel: string}[];
119
+ priceList?: Record<string, any>;
120
+ minDate: Moment.Moment;
121
+ maxDate: Moment.Moment;
122
+ startDate: Date | null;
123
+ endDate: Date | null;
124
+ isShowLunar: boolean;
125
+ tabSelected?: number;
126
+ today: Moment.Moment;
127
+ onChoose: (day: Date) => void;
128
+ onScrollCalendar?: (info: {
129
+ date: Moment.Moment;
130
+ key: string;
131
+ currentIndex: number;
132
+ }) => void;
133
+ lunarConverter: any;
134
+ disabledDays?: Date[];
135
+ };
136
+
137
+ export type MonthListState = {
138
+ data?: any;
139
+ isDoubleDateMode?: boolean;
140
+ };
141
+
142
+ export type MonthListRef = {
143
+ scrollToMonth: () => void;
144
+ };
145
+
146
+ export type MonthProps = {
147
+ dateList: any[];
148
+ holidays: Holidays;
149
+ priceListDate?: Record<string, PriceInfo>;
150
+ month?: any;
151
+ minDate: Moment.Moment;
152
+ maxDate: Moment.Moment;
153
+ startDate: Moment.Moment;
154
+ endDate: Moment.Moment;
155
+ isShowLunar: boolean;
156
+ isDoubleDateMode: boolean;
157
+ disabledDays?: Date[];
158
+ };
159
+
160
+ export type DayProps = {
161
+ onChoose?: (date: Moment.Moment) => void;
162
+ date: Moment.Moment;
163
+ minDate: Moment.Moment;
164
+ maxDate: Moment.Moment;
165
+ empty: Moment.Moment;
166
+ index: number;
167
+ isShowLunar: boolean;
168
+ isDoubleDateMode: boolean;
169
+ lunarDate?: any;
170
+ isSolarHoliday: boolean;
171
+ isLunarHoliday: boolean;
172
+ tabSelected?: number;
173
+ startDate: Moment.Moment;
174
+ endDate: Moment.Moment;
175
+ price?: string;
176
+ isBestPrice?: boolean;
177
+ havePriceList: boolean;
178
+ isDisabled: boolean;
179
+ };
180
+
181
+ export type TabHeaderState = {
182
+ active: boolean;
183
+ date?: Moment.Moment;
184
+ };
185
+
186
+ export type TabHeaderProps = {
187
+ activeTab: boolean;
188
+ label: string;
189
+ date?: Moment.Moment | string | null;
190
+ onChangeTab?: (id: number) => void;
191
+ id: number;
192
+ disabled?: boolean;
193
+ };
194
+
195
+ export type TabHeaderRef = {
196
+ updateView: (date?: Moment.Moment | null, activeTab?: boolean) => void;
197
+ setActiveTab: (active: boolean) => void;
198
+ };
package/src/Calendar.tsx DELETED
@@ -1,488 +0,0 @@
1
- import React, {Component, useCallback} from 'react';
2
- import {
3
- Dimensions,
4
- LayoutChangeEvent,
5
- ScrollView,
6
- StyleSheet,
7
- View,
8
- } from 'react-native';
9
- import moment from 'moment';
10
- import {
11
- ApplicationContext,
12
- Colors,
13
- Radius,
14
- Switch,
15
- Text,
16
- } from '@momo-kits/foundation';
17
- import CalendarPro from './CalendarPro';
18
- import TabHeader from './TabHeader';
19
- import {CalendarProps, CalendarState} from '../types';
20
-
21
- const DOUBLE = 'doubleDate';
22
- const SINGLE = 'singleDate';
23
-
24
- class Calendar extends Component<CalendarProps, CalendarState> {
25
- static contextType = ApplicationContext;
26
- doubleDate;
27
- tabSelected;
28
- selectedDate;
29
- calendarPicker;
30
- cellHeader1;
31
- cellHeader2;
32
- cellHeaderSingle;
33
- constructor(props: CalendarProps) {
34
- super(props);
35
- this.doubleDate = props.doubleDate
36
- ? {
37
- first: props.doubleDate.first ? moment(props.doubleDate.first) : null,
38
- second: props.doubleDate.second
39
- ? moment(props.doubleDate.second)
40
- : null,
41
- }
42
- : {};
43
- this.tabSelected = 0;
44
-
45
- this.selectedDate = props.selectedDate
46
- ? moment(props.selectedDate)
47
- : moment();
48
-
49
- this.state = {
50
- isDoubleDateMode: props.mode === DOUBLE,
51
- containerWidth: 0,
52
- };
53
- this.calendarPicker = React.createRef();
54
- this.cellHeader1 = React.createRef();
55
- this.cellHeader2 = React.createRef();
56
- this.cellHeaderSingle = React.createRef();
57
- }
58
-
59
- componentDidMount() {
60
- const {id} = this.props;
61
- this.tabSelected = id || 0;
62
- this.viewInit();
63
- }
64
-
65
- viewInit() {
66
- const {mode} = this.props;
67
- if (mode === DOUBLE) {
68
- if (
69
- this.cellHeader1.current &&
70
- this.cellHeader2.current &&
71
- this.calendarPicker.current
72
- ) {
73
- const start = this.doubleDate.first ? this.doubleDate.first : null;
74
- const end = this.doubleDate.second ? this.doubleDate.second : null;
75
- this.cellHeader1.current.updateView(start, this.tabSelected === 0);
76
- this.cellHeader2.current.updateView(end, this.tabSelected === 1);
77
- this.calendarPicker.current.setDoubleDateAndTabIndex(
78
- this.doubleDate.first,
79
- this.doubleDate.second,
80
- this.tabSelected,
81
- );
82
- }
83
- } else if (this.calendarPicker.current) {
84
- this.calendarPicker.current.setDoubleDateAndTabIndex(this.selectedDate);
85
- }
86
- }
87
-
88
- onChangeTab = (idTab: number) => {
89
- this.props.onChangeTab && this.props.onChangeTab(idTab);
90
-
91
- this.tabSelected = idTab;
92
- if (this.cellHeader1.current && this.cellHeader2.current) {
93
- this.cellHeader1.current.setActiveTab(idTab === 0);
94
- this.cellHeader2.current.setActiveTab(idTab === 1);
95
- }
96
- this.updateViewFlowPicker();
97
- };
98
-
99
- updateViewFlowPicker() {
100
- if (this.calendarPicker.current) {
101
- this.calendarPicker.current.setDoubleDateAndTabIndex(
102
- this.doubleDate.first,
103
- this.doubleDate.second,
104
- this.tabSelected,
105
- );
106
- }
107
- }
108
-
109
- processDateFirst() {
110
- const {onDateChange, onCTAStateChange} = this.props;
111
- if (
112
- this.cellHeader1.current &&
113
- this.cellHeader2.current &&
114
- this.calendarPicker.current
115
- ) {
116
- if (
117
- this.doubleDate.first &&
118
- this.doubleDate.second &&
119
- this.selectedDate <= this.doubleDate.first
120
- ) {
121
- this.doubleDate.first = this.selectedDate;
122
- this.doubleDate.second = null;
123
- this.tabSelected = 1;
124
- this.cellHeader1.current.updateView(
125
- this.selectedDate,
126
- this.tabSelected === 0,
127
- );
128
- this.cellHeader2.current.updateView(
129
- this.doubleDate.second,
130
- this.tabSelected === 1,
131
- );
132
- this.cellHeader2.current.setActiveTab(this.tabSelected === 1);
133
- this.calendarPicker.current.setDoubleDateAndTabIndex(
134
- this.doubleDate.first,
135
- this.doubleDate.second,
136
- this.tabSelected,
137
- );
138
- if (onDateChange) {
139
- onDateChange({
140
- first: this.doubleDate.first
141
- ? this.doubleDate.first.toDate()
142
- : null,
143
- second: this.doubleDate.second
144
- ? this.doubleDate.second.toDate()
145
- : null,
146
- });
147
- }
148
- } else {
149
- this.doubleDate.first = this.selectedDate;
150
- this.doubleDate.second = null;
151
- this.cellHeader1.current.updateView(this.selectedDate, false);
152
- this.cellHeader2.current.updateView(null, true);
153
- this.tabSelected = 1;
154
- this.calendarPicker.current.setDoubleDateAndTabIndex(
155
- this.doubleDate.first,
156
- this.doubleDate.second,
157
- this.tabSelected,
158
- );
159
- if (onDateChange) {
160
- onDateChange({
161
- first: this.doubleDate.first
162
- ? this.doubleDate.first.toDate()
163
- : null,
164
- second: this.doubleDate.second
165
- ? this.doubleDate.second.toDate()
166
- : null,
167
- });
168
- }
169
- }
170
- }
171
- if (onCTAStateChange) {
172
- onCTAStateChange(!!this.doubleDate.second);
173
- }
174
- }
175
-
176
- processDateSecond() {
177
- const {onDateChange, onCTAStateChange} = this.props;
178
- if (
179
- this.cellHeader2.current &&
180
- this.selectedDate >= this.doubleDate.first
181
- ) {
182
- this.doubleDate.second = this.selectedDate;
183
- this.cellHeader2.current.updateView(this.selectedDate, false);
184
- this.cellHeader1.current.setActiveTab(true);
185
- this.tabSelected = 0;
186
- this.calendarPicker.current.setDoubleDateAndTabIndex(
187
- this.doubleDate.first,
188
- this.doubleDate.second,
189
- this.tabSelected,
190
- );
191
- if (onCTAStateChange) {
192
- onCTAStateChange(!!this.doubleDate.second);
193
- }
194
- if (onDateChange) {
195
- onDateChange({
196
- first: this.doubleDate.first ? this.doubleDate.first.toDate() : null,
197
- second: this.doubleDate.second
198
- ? this.doubleDate.second.toDate()
199
- : null,
200
- });
201
- }
202
- } else {
203
- this.doubleDate.first = this.selectedDate;
204
- this.doubleDate.second = null;
205
-
206
- this.cellHeader1.current.updateView(this.selectedDate, false);
207
- this.cellHeader2.current.updateView(null, false);
208
- }
209
- }
210
-
211
- processDoubleDate() {
212
- if (this.tabSelected === 0) {
213
- this.processDateFirst();
214
- } else {
215
- this.processDateSecond();
216
- }
217
- }
218
-
219
- updateView() {
220
- const {onDateChange} = this.props;
221
- const {isDoubleDateMode} = this.state;
222
-
223
- if (isDoubleDateMode) {
224
- this.processDoubleDate();
225
- } else {
226
- if (this.cellHeaderSingle.current) {
227
- this.cellHeaderSingle.current.updateView(this.selectedDate, true);
228
- }
229
- if (onDateChange) {
230
- const date = new Date(this.selectedDate.toDate());
231
- onDateChange(date);
232
- }
233
- }
234
- }
235
-
236
- onDateChange = (date: moment.Moment) => {
237
- this.selectedDate = date;
238
- this.updateView();
239
- };
240
-
241
- updateHeaderView = () => {
242
- const {onDateChange, onCTAStateChange} = this.props;
243
- const {isDoubleDateMode} = this.state;
244
- if (isDoubleDateMode) {
245
- if (this.cellHeader1.current && this.cellHeader2.current) {
246
- this.cellHeader1.current.updateView(this.selectedDate, true);
247
- this.cellHeader2.current.updateView(null, false);
248
- this.calendarPicker.current.setDoubleDateAndTabIndex(
249
- this.selectedDate,
250
- null,
251
- this.tabSelected,
252
- );
253
- this.doubleDate.first = moment(this.selectedDate);
254
- this.doubleDate.second = null;
255
- this.tabSelected = 0;
256
-
257
- if (onCTAStateChange) {
258
- onCTAStateChange(false);
259
- }
260
- if (onDateChange) {
261
- onDateChange({
262
- first: this.doubleDate.first
263
- ? this.doubleDate.first.toDate()
264
- : null,
265
- second: this.doubleDate.second
266
- ? this.doubleDate.second.toDate()
267
- : null,
268
- });
269
- }
270
- }
271
- } else {
272
- if (onCTAStateChange) {
273
- onCTAStateChange(true);
274
- }
275
- if (this.cellHeaderSingle.current) {
276
- if (this.doubleDate.first) {
277
- this.cellHeaderSingle.current.updateView(this.doubleDate.first, true);
278
- this.selectedDate = this.doubleDate.first;
279
- } else {
280
- this.cellHeaderSingle.current.updateView(this.selectedDate, true);
281
- }
282
- if (onDateChange) {
283
- onDateChange(this.selectedDate.toDate());
284
- }
285
- }
286
- }
287
- };
288
-
289
- toggleSelectionDateMode = () => {
290
- const {onCallbackCalendar} = this.props;
291
- this.setState(
292
- preState => ({isDoubleDateMode: !preState.isDoubleDateMode}),
293
- () => {
294
- const {isDoubleDateMode} = this.state;
295
- this.updateHeaderView();
296
- if (onCallbackCalendar && typeof onCallbackCalendar === 'function') {
297
- onCallbackCalendar('switch', isDoubleDateMode);
298
- }
299
- },
300
- );
301
- };
302
-
303
- renderSwitchReturnSelection = () => {
304
- const {isDoubleDateMode} = this.state;
305
- const {translate} = this.context;
306
- return (
307
- <View style={styles.headerContainer}>
308
- <View style={styles.viewSwitch}>
309
- <Text typography={'label_default'} style={styles.textSwitch}>
310
- {translate('chooseRoundtrip')}
311
- </Text>
312
- <Switch
313
- value={isDoubleDateMode}
314
- onChange={this.toggleSelectionDateMode}
315
- />
316
- </View>
317
- <View style={styles.seperator} />
318
- </View>
319
- );
320
- };
321
-
322
- renderHeaderPanel = () => {
323
- const {isDoubleDateMode} = this.state;
324
- const {headerFrom, headerTo} = this.props;
325
- return isDoubleDateMode ? (
326
- <View style={styles.viewPanel}>
327
- <TabHeader
328
- id={0}
329
- ref={this.cellHeader1}
330
- onChangeTab={this.onChangeTab}
331
- label={headerFrom || 'depart'}
332
- activeTab
333
- date={this.doubleDate.first}
334
- />
335
-
336
- <TabHeader
337
- id={1}
338
- ref={this.cellHeader2}
339
- onChangeTab={this.onChangeTab}
340
- label={headerTo || 'return'}
341
- activeTab={false}
342
- date=""
343
- />
344
- </View>
345
- ) : (
346
- <View style={styles.viewPanel_2}>
347
- <TabHeader
348
- id={1}
349
- disabled
350
- ref={this.cellHeaderSingle}
351
- label={headerFrom || 'depart'}
352
- activeTab
353
- date={this.selectedDate}
354
- />
355
- </View>
356
- );
357
- };
358
-
359
- setDateRange = (
360
- dateRange: {
361
- startDate: moment.MomentInput | undefined;
362
- endDate: moment.MomentInput | undefined;
363
- },
364
- isScrollToStartDate: any,
365
- ) => {
366
- const {mode, doubleDate = {}} = this.props;
367
- if (mode === 'doubleDate') {
368
- this.cellHeader1.current.updateView(
369
- dateRange.startDate,
370
- this.tabSelected === 0,
371
- );
372
- this.cellHeader2.current.updateView(
373
- dateRange.endDate,
374
- this.tabSelected === 1,
375
- );
376
- this.calendarPicker?.current?.setDateRange(
377
- dateRange,
378
- isScrollToStartDate,
379
- );
380
- this.doubleDate = doubleDate
381
- ? {
382
- first: dateRange.startDate ? moment(dateRange.startDate) : null,
383
- second: dateRange.endDate ? moment(dateRange.endDate) : null,
384
- }
385
- : {};
386
- }
387
- };
388
-
389
- onLayout = (e: LayoutChangeEvent) => {
390
- this.setState({containerWidth: e.nativeEvent.layout.width});
391
- };
392
-
393
- render() {
394
- const {
395
- isOffLunar,
396
- isHideHoliday,
397
- isHiddenSwitch,
398
- isShowLunar,
399
- onCallbackCalendar,
400
- priceList,
401
- labelFrom,
402
- labelTo,
403
- isHideLabel,
404
- minDate,
405
- maxDate,
406
- doubleDate,
407
- isHideHeaderPanel,
408
- disabledWeekend,
409
- style,
410
- } = this.props;
411
-
412
- const {isDoubleDateMode, containerWidth} = this.state;
413
- return (
414
- <ScrollView onLayout={this.onLayout} style={[styles.scrollView, style]}>
415
- <View
416
- style={{
417
- backgroundColor: Colors.black_01,
418
- borderRadius: Radius.XS,
419
- }}>
420
- {!isHiddenSwitch && this.renderSwitchReturnSelection()}
421
- {!isHideHeaderPanel && this.renderHeaderPanel()}
422
- </View>
423
- <CalendarPro
424
- containerWidth={containerWidth}
425
- disabledWeekend={disabledWeekend}
426
- priceList={priceList}
427
- ref={this.calendarPicker}
428
- startDate={doubleDate?.first}
429
- endDate={doubleDate?.second}
430
- onDateChange={this.onDateChange}
431
- isDoubleDateMode={isDoubleDateMode}
432
- selectedDate={this.selectedDate}
433
- isShowLunar={isShowLunar}
434
- onCallbackCalendar={onCallbackCalendar}
435
- labelFrom={labelFrom}
436
- labelTo={labelTo}
437
- isHideLabel={isHideLabel}
438
- minDate={minDate}
439
- maxDate={maxDate}
440
- isHideHoliday={isHideHoliday}
441
- isOffLunar={isOffLunar}
442
- />
443
- </ScrollView>
444
- );
445
- }
446
- }
447
-
448
- export default Calendar;
449
-
450
- const styles = StyleSheet.create({
451
- viewPanel_2: {
452
- height: 50,
453
- marginHorizontal: 12,
454
- marginTop: 6,
455
- flexDirection: 'row',
456
- },
457
- viewPanel: {
458
- height: 50,
459
- marginHorizontal: 12,
460
- marginTop: 6,
461
- flexDirection: 'row',
462
- },
463
- textSwitch: {
464
- color: Colors.black_15,
465
- },
466
- viewSwitch: {
467
- flexDirection: 'row',
468
- justifyContent: 'space-between',
469
- alignItems: 'center',
470
- backgroundColor: Colors.black_01,
471
- height: 42,
472
- },
473
- seperator: {
474
- height: 1,
475
- width: '100%',
476
- backgroundColor: Colors.black_04,
477
- },
478
- headerContainer: {
479
- paddingHorizontal: 12,
480
- paddingVertical: 8,
481
- },
482
- scrollView: {
483
- flex: 1,
484
- backgroundColor: 'transparent',
485
- alignSelf: 'center',
486
- width: '100%',
487
- },
488
- });