@momo-kits/calendar 0.73.3-beta.5 → 0.74.2-react-native.1

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.
@@ -1,23 +1,33 @@
1
- import PropTypes from 'prop-types';
2
- import React, {Component} from 'react';
3
- import {Dimensions, ScrollView, StyleSheet, View} from 'react-native';
1
+ import React, {Component, createContext, RefObject} from 'react';
2
+ import {Dimensions, LayoutChangeEvent, View} from 'react-native';
4
3
  import moment from 'moment';
5
4
  import {
6
- Colors,
5
+ ApplicationContext,
7
6
  Radius,
8
- ScaleSize,
9
- SwitchLanguage,
7
+ scaleSize,
8
+ Spacing,
9
+ Switch,
10
10
  Text,
11
- } from '@momo-kits/core-v2';
12
- import Switch from '@momo-kits/switch';
11
+ } from '@momo-kits/foundation';
13
12
  import CalendarPro from './CalendarPro';
14
13
  import TabHeader from './TabHeader';
14
+ import {CalendarProps, CalendarState} from './types';
15
+ import styles from './styles';
15
16
 
16
17
  const DOUBLE = 'doubleDate';
17
18
 
18
- // const SIGNLE = 'signleDate';
19
- class Calendar extends Component {
20
- constructor(props) {
19
+ export const ContainerContext = createContext({width: 0, height: 0});
20
+
21
+ class Calendar extends Component<CalendarProps, CalendarState> {
22
+ static contextType = ApplicationContext;
23
+ doubleDate: any;
24
+ tabSelected;
25
+ selectedDate: moment.Moment;
26
+ calendarPicker: RefObject<CalendarPro>;
27
+ cellHeader1: RefObject<TabHeader>;
28
+ cellHeader2: RefObject<TabHeader>;
29
+ cellHeaderSingle: RefObject<TabHeader>;
30
+ constructor(props: CalendarProps) {
21
31
  super(props);
22
32
  this.doubleDate = props.doubleDate
23
33
  ? {
@@ -35,11 +45,12 @@ class Calendar extends Component {
35
45
 
36
46
  this.state = {
37
47
  isDoubleDateMode: props.mode === DOUBLE,
48
+ containerWidth: Dimensions.get('window').width,
38
49
  };
39
- this.calendarPicker = React.createRef();
40
- this.cellHeader1 = React.createRef();
41
- this.cellHeader2 = React.createRef();
42
- this.cellHeaderSingle = React.createRef();
50
+ this.calendarPicker = React.createRef<CalendarPro>();
51
+ this.cellHeader1 = React.createRef<TabHeader>();
52
+ this.cellHeader2 = React.createRef<TabHeader>();
53
+ this.cellHeaderSingle = React.createRef<TabHeader>();
43
54
  }
44
55
 
45
56
  componentDidMount() {
@@ -71,10 +82,10 @@ class Calendar extends Component {
71
82
  }
72
83
  }
73
84
 
74
- onChangeTab = idTab => {
75
- this.props.onChangeTab && this.props.onChangeTab(idTab);
76
-
85
+ onChangeTab = (idTab: number) => {
86
+ this.props.onChangeTab?.(idTab);
77
87
  this.tabSelected = idTab;
88
+
78
89
  if (this.cellHeader1.current && this.cellHeader2.current) {
79
90
  this.cellHeader1.current.setActiveTab(idTab === 0);
80
91
  this.cellHeader2.current.setActiveTab(idTab === 1);
@@ -122,12 +133,6 @@ class Calendar extends Component {
122
133
  this.tabSelected,
123
134
  );
124
135
  if (onDateChange) {
125
- // const cloned = {
126
- // first: this.doubleDate.first ? this.doubleDate.first.toDate() : null,
127
- // this.doubleDate.first ? new Date(this.doubleDate.first.year(), this.doubleDate.first.month(), this.doubleDate.first.date()) : null,
128
- // second: this.doubleDate.second ? this.doubleDate.second.toDate() : null
129
- // this.doubleDate.second ? new Date(this.doubleDate.second.year(), this.doubleDate.second.month(), this.doubleDate.second.date()) : null
130
- // };
131
136
  onDateChange({
132
137
  first: this.doubleDate.first
133
138
  ? this.doubleDate.first.toDate()
@@ -157,11 +162,6 @@ class Calendar extends Component {
157
162
  ? this.doubleDate.second.toDate()
158
163
  : null,
159
164
  });
160
- // const cloned = {
161
- // first: this.doubleDate.first ? new Date(this.doubleDate.first.year(), this.doubleDate.first.month(), this.doubleDate.first.date()) : null,
162
- // second: this.doubleDate.second ? new Date(this.doubleDate.second.year(), this.doubleDate.second.month(), this.doubleDate.second.date()) : null
163
- // };
164
- // onDateChange(cloned);
165
165
  }
166
166
  }
167
167
  }
@@ -178,9 +178,9 @@ class Calendar extends Component {
178
178
  ) {
179
179
  this.doubleDate.second = this.selectedDate;
180
180
  this.cellHeader2.current.updateView(this.selectedDate, false);
181
- this.cellHeader1.current.setActiveTab(true);
181
+ this.cellHeader1?.current?.setActiveTab(true);
182
182
  this.tabSelected = 0;
183
- this.calendarPicker.current.setDoubleDateAndTabIndex(
183
+ this.calendarPicker?.current?.setDoubleDateAndTabIndex(
184
184
  this.doubleDate.first,
185
185
  this.doubleDate.second,
186
186
  this.tabSelected,
@@ -195,18 +195,13 @@ class Calendar extends Component {
195
195
  ? this.doubleDate.second.toDate()
196
196
  : null,
197
197
  });
198
- // const cloned = {
199
- // first: this.doubleDate.first ? new Date(this.doubleDate.first.year(), this.doubleDate.first.month(), this.doubleDate.first.date()) : null,
200
- // second: this.doubleDate.second ? new Date(this.doubleDate.second.year(), this.doubleDate.second.month(), this.doubleDate.second.date()) : null
201
- // };
202
- // onDateChange(cloned);
203
198
  }
204
199
  } else {
205
200
  this.doubleDate.first = this.selectedDate;
206
201
  this.doubleDate.second = null;
207
202
 
208
- this.cellHeader1.current.updateView(this.selectedDate, false);
209
- this.cellHeader2.current.updateView(null, false);
203
+ this.cellHeader1?.current?.updateView(this.selectedDate, false);
204
+ this.cellHeader2?.current?.updateView(null, false);
210
205
  }
211
206
  }
212
207
 
@@ -235,7 +230,7 @@ class Calendar extends Component {
235
230
  }
236
231
  }
237
232
 
238
- onDateChange = date => {
233
+ onDateChange = (date: moment.Moment) => {
239
234
  this.selectedDate = date;
240
235
  this.updateView();
241
236
  };
@@ -247,14 +242,22 @@ class Calendar extends Component {
247
242
  if (this.cellHeader1.current && this.cellHeader2.current) {
248
243
  this.cellHeader1.current.updateView(this.selectedDate, true);
249
244
  this.cellHeader2.current.updateView(null, false);
250
- this.calendarPicker.current.setDoubleDateAndTabIndex(
245
+ this.calendarPicker.current?.setDoubleDateAndTabIndex(
251
246
  this.selectedDate,
252
247
  null,
253
248
  this.tabSelected,
254
249
  );
255
250
  this.doubleDate.first = moment(this.selectedDate);
256
251
  this.doubleDate.second = null;
257
- this.tabSelected = 0;
252
+
253
+ this.tabSelected = 1;
254
+ this.cellHeader2.current?.setActiveTab(true);
255
+ this.cellHeader1.current?.setActiveTab(false);
256
+ this.calendarPicker?.current?.setDoubleDateAndTabIndex(
257
+ this.doubleDate.first,
258
+ this.doubleDate.second,
259
+ this.tabSelected,
260
+ );
258
261
 
259
262
  if (onCTAStateChange) {
260
263
  onCTAStateChange(false);
@@ -268,11 +271,6 @@ class Calendar extends Component {
268
271
  ? this.doubleDate.second.toDate()
269
272
  : null,
270
273
  });
271
- // const cloned = {
272
- // first: this.doubleDate.first ? new Date(this.doubleDate.first.year(), this.doubleDate.first.month(), this.doubleDate.first.date()) : null,
273
- // second: this.doubleDate.second ? new Date(this.doubleDate.second.year(), this.doubleDate.second.month(), this.doubleDate.second.date()) : null
274
- // };
275
- // onDateChange(cloned);
276
274
  }
277
275
  }
278
276
  } else {
@@ -286,10 +284,7 @@ class Calendar extends Component {
286
284
  } else {
287
285
  this.cellHeaderSingle.current.updateView(this.selectedDate, true);
288
286
  }
289
-
290
287
  if (onDateChange) {
291
- // const clone = moment(this.selectedDate);
292
- // const cloned = new Date(clone.year(), clone.month(), clone.date());
293
288
  onDateChange(this.selectedDate.toDate());
294
289
  }
295
290
  }
@@ -312,57 +307,64 @@ class Calendar extends Component {
312
307
 
313
308
  renderSwitchReturnSelection = () => {
314
309
  const {isDoubleDateMode} = this.state;
310
+ const {translate, theme} = this.context;
315
311
  return (
316
312
  <View style={styles.headerContainer}>
317
- <View style={styles.viewSwitch}>
318
- <Text.Label1 weight="medium" style={styles.textSwitch}>
319
- {SwitchLanguage.chooseRoundtrip}
320
- </Text.Label1>
313
+ <View
314
+ style={[
315
+ styles.viewSwitch,
316
+ {backgroundColor: theme.colors.background.surface},
317
+ ]}>
318
+ <Text typography={'label_default_medium'}>
319
+ {translate('chooseRoundtrip')}
320
+ </Text>
321
321
  <Switch
322
322
  value={isDoubleDateMode}
323
323
  onChange={this.toggleSelectionDateMode}
324
- thumbColor={isDoubleDateMode ? '#78ca32' : 'white'}
325
- trackColor={{
326
- false: '#e5e7ec',
327
- true: '#e5e7ec',
328
- }}
329
324
  />
330
325
  </View>
331
- <View style={styles.seperator} />
332
326
  </View>
333
327
  );
334
328
  };
335
329
 
336
- renderHeaderPanel = () => {
330
+ renderHeaderPanel = (theme: {colors: {background: {default: string}}}) => {
337
331
  const {isDoubleDateMode} = this.state;
338
332
  const {headerFrom, headerTo} = this.props;
339
333
  return isDoubleDateMode ? (
340
- <View style={styles.viewPanel}>
334
+ <View
335
+ style={[
336
+ styles.viewPanel,
337
+ {backgroundColor: theme.colors.background.default},
338
+ ]}>
341
339
  <TabHeader
342
340
  id={0}
343
341
  ref={this.cellHeader1}
344
342
  onChangeTab={this.onChangeTab}
345
- label={headerFrom || SwitchLanguage.depart}
343
+ label={headerFrom || 'depart'}
346
344
  activeTab
347
345
  date={this.doubleDate.first}
348
346
  />
349
-
347
+ <View style={{width: 4, height: '100%'}} />
350
348
  <TabHeader
351
349
  id={1}
352
350
  ref={this.cellHeader2}
353
351
  onChangeTab={this.onChangeTab}
354
- label={headerTo || SwitchLanguage.return}
352
+ label={headerTo || 'return'}
355
353
  activeTab={false}
356
354
  date=""
357
355
  />
358
356
  </View>
359
357
  ) : (
360
- <View style={styles.viewPanel_2}>
358
+ <View
359
+ style={[
360
+ styles.viewPanel,
361
+ {backgroundColor: theme.colors.background.default},
362
+ ]}>
361
363
  <TabHeader
362
- id={1}
364
+ id={0}
363
365
  disabled
364
366
  ref={this.cellHeaderSingle}
365
- label={headerFrom || SwitchLanguage.depart}
367
+ label={headerFrom || 'depart'}
366
368
  activeTab
367
369
  date={this.selectedDate}
368
370
  />
@@ -370,14 +372,20 @@ class Calendar extends Component {
370
372
  );
371
373
  };
372
374
 
373
- setDateRange = (dateRange, isScrollToStartDate) => {
375
+ setDateRange = (
376
+ dateRange: {
377
+ startDate: moment.Moment | undefined;
378
+ endDate: moment.Moment | undefined;
379
+ },
380
+ isScrollToStartDate: any,
381
+ ) => {
374
382
  const {mode, doubleDate = {}} = this.props;
375
383
  if (mode === 'doubleDate') {
376
- this.cellHeader1.current.updateView(
384
+ this.cellHeader1?.current?.updateView(
377
385
  dateRange.startDate,
378
386
  this.tabSelected === 0,
379
387
  );
380
- this.cellHeader2.current.updateView(
388
+ this.cellHeader2?.current?.updateView(
381
389
  dateRange.endDate,
382
390
  this.tabSelected === 1,
383
391
  );
@@ -394,134 +402,77 @@ class Calendar extends Component {
394
402
  }
395
403
  };
396
404
 
405
+ onLayout = (e: LayoutChangeEvent) => {
406
+ this.setState({containerWidth: e.nativeEvent.layout.width});
407
+ };
408
+
397
409
  render() {
410
+ const currentDate = new Date();
411
+ const minDateDefault = new Date(currentDate);
412
+ const maxDateDefault = new Date(currentDate);
413
+ minDateDefault.setFullYear(minDateDefault.getFullYear() - 1);
414
+ maxDateDefault.setFullYear(maxDateDefault.getFullYear() + 1);
415
+
398
416
  const {
399
417
  isOffLunar,
400
418
  isHideHoliday,
401
- isHiddenSwitch,
402
419
  isShowLunar,
403
420
  onCallbackCalendar,
404
421
  priceList,
405
422
  labelFrom,
406
423
  labelTo,
407
424
  isHideLabel,
408
- minDate,
409
- maxDate,
425
+ minDate = minDateDefault,
426
+ maxDate = maxDateDefault,
410
427
  doubleDate,
411
- isHideHeaderPanel,
412
- disabledWeekend,
413
428
  style,
429
+ disabledDays = [],
430
+ hideHeaderTab = false,
414
431
  } = this.props;
415
- const {isDoubleDateMode} = this.state;
416
- const {width} = Dimensions.get('window');
432
+ const {isDoubleDateMode, containerWidth} = this.state;
433
+ const {theme} = this.context;
417
434
  return (
418
- <ScrollView
419
- style={[
420
- styles.scrollView,
421
- {
422
- width: width - ScaleSize(24),
423
- },
424
- style,
425
- ]}>
426
- <View
427
- style={{
428
- backgroundColor: Colors.black_01,
429
- borderRadius: Radius.XS,
430
- }}>
431
- {!isHiddenSwitch && this.renderSwitchReturnSelection()}
432
- {!isHideHeaderPanel && this.renderHeaderPanel()}
435
+ <ContainerContext.Provider
436
+ value={{
437
+ width: containerWidth,
438
+ height: priceList ? scaleSize(48) : scaleSize(34),
439
+ }}>
440
+ <View onLayout={this.onLayout} style={[style, styles.scrollView]}>
441
+ <View
442
+ style={[
443
+ {
444
+ backgroundColor: theme.colors.background.surface,
445
+ borderRadius: Radius.XS,
446
+ marginBottom: Spacing.S,
447
+ },
448
+ hideHeaderTab && styles.invisible,
449
+ ]}>
450
+ {this.renderSwitchReturnSelection()}
451
+ {this.renderHeaderPanel(theme)}
452
+ </View>
453
+ <CalendarPro
454
+ priceList={priceList}
455
+ ref={this.calendarPicker}
456
+ startDate={doubleDate?.first}
457
+ endDate={doubleDate?.second}
458
+ onDateChange={this.onDateChange}
459
+ isDoubleDateMode={isDoubleDateMode}
460
+ selectedDate={this.selectedDate}
461
+ isShowLunar={isShowLunar}
462
+ onCallbackCalendar={onCallbackCalendar}
463
+ labelFrom={labelFrom}
464
+ labelTo={labelTo}
465
+ isHideLabel={isHideLabel}
466
+ minDate={minDate}
467
+ maxDate={maxDate}
468
+ isHideHoliday={isHideHoliday}
469
+ isOffLunar={isOffLunar}
470
+ disabledDays={disabledDays}
471
+ />
433
472
  </View>
434
- <CalendarPro
435
- disabledWeekend={disabledWeekend}
436
- priceList={priceList}
437
- ref={this.calendarPicker}
438
- startDate={doubleDate?.first}
439
- endDate={doubleDate?.second}
440
- onDateChange={this.onDateChange}
441
- isDoubleDateMode={isDoubleDateMode}
442
- selectedDate={this.selectedDate}
443
- isShowLunar={isShowLunar}
444
- onCallbackCalendar={onCallbackCalendar}
445
- labelFrom={labelFrom}
446
- labelTo={labelTo}
447
- isHideLabel={isHideLabel}
448
- minDate={minDate}
449
- maxDate={maxDate}
450
- isHideHoliday={isHideHoliday}
451
- isOffLunar={isOffLunar}
452
- />
453
- </ScrollView>
473
+ </ContainerContext.Provider>
454
474
  );
455
475
  }
456
476
  }
457
477
 
458
- export default Calendar;
459
-
460
- Calendar.propTypes = {
461
- doubleDate: PropTypes.shape({
462
- first: PropTypes.any,
463
- second: PropTypes.any,
464
- }),
465
- id: PropTypes.any,
466
- isHiddenSwitch: PropTypes.bool,
467
- isShowLunar: PropTypes.bool,
468
- mode: PropTypes.string,
469
- onCTAStateChange: PropTypes.func,
470
- onCallbackCalendar: PropTypes.func,
471
- onDateChange: PropTypes.func,
472
- selectedDate: PropTypes.any,
473
- priceList: PropTypes.any,
474
- labelFrom: PropTypes.string,
475
- labelTo: PropTypes.string,
476
- headerFrom: PropTypes.string,
477
- headerTo: PropTypes.string,
478
- isHideLabel: PropTypes.bool,
479
- isHideHoliday: PropTypes.bool,
480
- minDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
481
- maxDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
482
- style: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
483
- };
484
-
485
- Calendar.defaultProps = {
486
- mode: 'single',
487
- isHiddenSwitch: false,
488
- };
489
-
490
- const styles = StyleSheet.create({
491
- viewPanel_2: {
492
- height: 50,
493
- marginHorizontal: 12,
494
- marginTop: 6,
495
- flexDirection: 'row',
496
- },
497
- viewPanel: {
498
- height: 50,
499
- marginHorizontal: 12,
500
- marginTop: 6,
501
- flexDirection: 'row',
502
- },
503
- textSwitch: {
504
- color: Colors.black_15,
505
- },
506
- viewSwitch: {
507
- flexDirection: 'row',
508
- justifyContent: 'space-between',
509
- alignItems: 'center',
510
- backgroundColor: Colors.white,
511
- height: 42,
512
- },
513
- seperator: {
514
- height: 1,
515
- width: '100%',
516
- backgroundColor: Colors.black_04,
517
- },
518
- headerContainer: {
519
- paddingHorizontal: 12,
520
- paddingVertical: 8,
521
- },
522
- scrollView: {
523
- flex: 1,
524
- backgroundColor: 'transparent',
525
- alignSelf: 'center',
526
- },
527
- });
478
+ export {Calendar};
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@momo-kits/calendar",
3
- "version": "0.73.3-beta.5",
3
+ "version": "0.74.2-react-native.1",
4
4
  "private": false,
5
- "main": "index.js",
6
- "dependencies": {
7
- "@momo-kits/date-picker": "latest",
8
- "moment": "^2.24.0",
9
- "@momo-platform/versions": "4.0.2"
10
- },
5
+ "main": "index.tsx",
11
6
  "peerDependencies": {
12
- "react": "16.9.0",
13
- "react-native": ">=0.55",
14
- "prop-types": "^15.7.2",
15
- "@momo-kits/core-v2": ">=0.0.5-beta"
7
+ "@momo-kits/foundation": "latest",
8
+ "react": "*",
9
+ "react-native": "*",
10
+ "prop-types": "15.7.2"
16
11
  },
17
- "devDependencies": {},
18
- "license": "MoMo"
12
+ "devDependencies": {
13
+ "@momo-platform/versions": "4.1.11"
14
+ },
15
+ "license": "MoMo",
16
+ "dependencies": {
17
+ "moment": "^2.24.0"
18
+ }
19
19
  }
package/styles.ts ADDED
@@ -0,0 +1,123 @@
1
+ import {StyleSheet} from 'react-native';
2
+ import {Colors, Radius, Spacing} from '@momo-kits/foundation';
3
+
4
+ export default StyleSheet.create({
5
+ //calendar
6
+ viewPanel: {
7
+ flexDirection: 'row',
8
+ marginHorizontal: Spacing.M,
9
+ marginVertical: Spacing.S,
10
+ borderRadius: Radius.S,
11
+ padding: Spacing.XXS,
12
+ },
13
+ viewSwitch: {
14
+ flexDirection: 'row',
15
+ justifyContent: 'space-between',
16
+ alignItems: 'center',
17
+ marginBottom: Spacing.XS,
18
+ },
19
+ headerContainer: {
20
+ paddingHorizontal: Spacing.M,
21
+ marginTop: Spacing.S,
22
+ },
23
+ scrollView: {
24
+ backgroundColor: 'transparent',
25
+ alignSelf: 'center',
26
+ width: '100%',
27
+ },
28
+
29
+ //calendar pro
30
+ row: {flexDirection: 'row'},
31
+ txtMonthLunar: {
32
+ marginRight: Spacing.S,
33
+ },
34
+ subTextLunar: {
35
+ marginRight: Spacing.XS,
36
+ },
37
+ contentScroll: {
38
+ paddingHorizontal: Spacing.M,
39
+ paddingTop: Spacing.S,
40
+ paddingBottom: Spacing.M,
41
+ },
42
+ viewLunar: {
43
+ flexDirection: 'row',
44
+ alignItems: 'center',
45
+ marginHorizontal: Spacing.M,
46
+ borderTopWidth: 1,
47
+ paddingVertical: Spacing.S,
48
+ },
49
+ textDay: {
50
+ textAlign: 'center',
51
+ },
52
+ viewDay: {
53
+ flexDirection: 'row',
54
+ justifyContent: 'space-between',
55
+ paddingHorizontal: Spacing.S,
56
+ paddingVertical: Spacing.S,
57
+ marginTop: Spacing.S,
58
+ },
59
+ container: {
60
+ backgroundColor: 'white',
61
+ borderRadius: Radius.XS,
62
+ },
63
+ blueSeperator: {
64
+ height: 1,
65
+ width: '100%',
66
+ backgroundColor: Colors.blue_05,
67
+ },
68
+
69
+ //headerControl
70
+ txtHeader: {
71
+ textAlign: 'center',
72
+ },
73
+ btnRight: {
74
+ width: 36,
75
+ height: 36,
76
+ justifyContent: 'center',
77
+ alignItems: 'center',
78
+ },
79
+ btnLeft: {
80
+ width: 36,
81
+ height: 36,
82
+ justifyContent: 'center',
83
+ alignItems: 'center',
84
+ },
85
+ headerControlContainer: {
86
+ flexDirection: 'row',
87
+ justifyContent: 'space-between',
88
+ backgroundColor: Colors.blue_10,
89
+ alignItems: 'center',
90
+ borderRadius: Radius.XS,
91
+ padding: Spacing.S,
92
+ },
93
+
94
+ //day
95
+ dayContainer: {
96
+ borderRadius: Radius.XS,
97
+ alignItems: 'center',
98
+ },
99
+ day: {
100
+ justifyContent: 'center',
101
+ alignItems: 'center',
102
+ borderRadius: Radius.XS,
103
+ },
104
+ dayText: {
105
+ textAlign: 'center',
106
+ },
107
+ dayStartContainer: {
108
+ borderTopLeftRadius: Radius.XS,
109
+ borderBottomLeftRadius: Radius.XS,
110
+ },
111
+ dayEndContainer: {
112
+ borderTopRightRadius: Radius.XS,
113
+ borderBottomRightRadius: Radius.XS,
114
+ },
115
+
116
+ invisible: {
117
+ height: 0,
118
+ width: 0,
119
+ padding: 0,
120
+ marginBottom: 0,
121
+ overflow: 'hidden',
122
+ },
123
+ });