@momo-kits/calendar 0.0.74-beta → 0.72.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.
package/src/MonthList.js CHANGED
@@ -1,253 +1,248 @@
1
1
  /* eslint-disable react/no-did-update-set-state */
2
- import React, { Component } from 'react';
3
- import {
4
- FlatList,
5
- Dimensions,
6
- Platform
7
- } from 'react-native';
2
+ import React, {Component} from 'react';
3
+ import {Dimensions, FlatList, Platform} from 'react-native';
8
4
  import moment from 'moment';
9
5
  import Month from './Month';
10
6
  import style from './Day/style';
11
- import { convertSolar2Lunar } from './LunarService';
7
+ import LunarDateConverter from './LunarDateConverter';
8
+ import {ScaleSize} from '@momo-kits/core-v2';
12
9
 
13
- const ITEM_WIDTH = Dimensions.get('window').width - 24;
10
+ const ITEM_WIDTH = Dimensions.get('window').width - ScaleSize(24);
14
11
  const MAX_RENDER_PER_BATCH = Platform.OS === 'android' ? 1 : 12;
12
+
13
+ const converter = new LunarDateConverter();
15
14
  export default class MonthList extends Component {
16
- constructor(props) {
17
- super(props);
18
- // this.state = {
19
- // // data: [],
20
- // isDoubleDateMode: props.isDoubleDateMode
21
- // };
22
- this.currentDate = moment();
23
- this.data = this.getMonthList();
24
- this.currentScrollIndex = this.getIndexOfMonth(moment(props.selectedDate), this.data);
25
- this.list = React.createRef();
26
- this.heightStyle = {};
27
- this.holidays = props.holidays;
15
+ constructor(props) {
16
+ super(props);
17
+ // this.state = {
18
+ // // data: [],
19
+ // isDoubleDateMode: props.isDoubleDateMode
20
+ // };
21
+ this.currentDate = moment();
22
+ this.data = this.getMonthList();
23
+ this.currentScrollIndex = this.getIndexOfMonth(
24
+ moment(props.selectedDate),
25
+ this.data,
26
+ );
27
+ this.list = React.createRef();
28
+ this.heightStyle = {};
29
+ this.holidays = props.holidays;
30
+ }
31
+
32
+ renderMonth = ({item, index}) => {
33
+ const {isDoubleDateMode, priceList, disabledWeekend} = this.props;
34
+ const keyMonth = moment(item.date)?.format('YYYY-MM');
35
+ const priceListDate = priceList?.[keyMonth]?.day;
36
+
37
+ return (
38
+ <Month
39
+ disabledWeekend={disabledWeekend}
40
+ {...this.props}
41
+ key={index}
42
+ month={item.date || {}}
43
+ dateList={item.dateList || []}
44
+ priceListDate={priceListDate}
45
+ isDoubleDateMode={isDoubleDateMode}
46
+ />
47
+ );
48
+ };
49
+
50
+ checkRange = (date, start, end) => {
51
+ if (!date || !start) return false;
52
+ if (!end) {
53
+ return date.year() === start.year() && date.month() === start.month();
28
54
  }
29
-
30
- // componentDidUpdate(prevProps) {
31
- // const { isDoubleDateMode } = this.props;
32
- // if (isDoubleDateMode !== prevProps.isDoubleDateMode) {
33
- // this.setState({
34
- // // data: this.getMonthList(this.props),
35
- // isDoubleDateMode
36
- // });
37
- // }
38
- // }
39
-
40
- renderMonth = ({ item, index }) => {
41
- const { isDoubleDateMode, priceList } = this.props;
42
- const keyMonth = moment(item.date)?.format('YYYY-MM');
43
- const priceListDate = priceList?.[keyMonth]?.day;
44
- return (
45
- <Month
46
- {...this.props}
47
- key={index}
48
- month={item.date || {}}
49
- dateList={item.dateList || []}
50
- priceListDate={priceListDate}
51
- isDoubleDateMode={isDoubleDateMode}
52
- />
53
- );
54
- };
55
-
56
- checkRange = (date, start, end) => {
57
- if (!date || !start) return false;
58
- if (!end) return date.year() === start.year() && date.month() === start.month();
59
- if (date.year() < start.year() || (date.year() === start.year() && date.month() < start.month())) return false;
60
- return !(date.year() > end.year() || (date.year() === end.year() && date.month() > end.month()));
61
- };
62
-
63
- shouldUpdate = (month, props) => {
64
- if (!props) return false;
65
- const {
66
- startDate,
67
- endDate
68
- } = props;
69
- const { startDate: curStartDate, endDate: curEndDate } = this.props;
70
- const {
71
- date
72
- } = month;
73
- const next = this.checkRange(date, startDate, endDate);
74
- const prev = this.checkRange(date, curStartDate, curEndDate);
75
- return !!(prev || next);
76
- };
77
-
78
- convertLunarToSolar(date) {
79
- return date && convertSolar2Lunar ? convertSolar2Lunar(
80
- date.date(),
81
- date.month() + 1,
82
- date.year(),
83
- 7
84
- ) : {};
55
+ if (
56
+ date.year() < start.year() ||
57
+ (date.year() === start.year() && date.month() < start.month())
58
+ ) {
59
+ return false;
85
60
  }
86
-
87
- findHoliday = (date) => {
88
- const { holidays } = this.props;
89
- if (date && holidays && holidays.length > 0) {
90
- const day = date.date();
91
- const month = date.month() + 1;
92
- return holidays.find((item) => item.day === day && item.month === month);
93
- }
94
- return null;
95
- };
96
-
97
- checkHoliday = (date, holidays) => {
98
- const holiday = this.findHoliday(date, holidays);
99
- return {
100
- isSolarHoliday: !!(holiday && !holiday.lunar),
101
- isLunarHoliday: !!(holiday && holiday.lunar)
102
- };
103
- };
104
-
105
- getDayList = (date) => {
106
- let dayList;
107
- const month = date.month();
108
- let weekday = date.isoWeekday();
109
- if (weekday === 1) {
110
- dayList = [];
111
- } else {
112
- dayList = new Array(weekday - 1).fill({
113
- empty: moment(date).subtract(1, 'h'),
114
- lunarDate: this.convertLunarToSolar(date),
115
- ...this.checkHoliday(date)
116
- });
117
- }
118
- while (date.month() === month) {
119
- const cloned = moment(date);
120
- dayList.push({
121
- date: cloned,
122
- lunarDate: this.convertLunarToSolar(cloned),
123
- ...this.checkHoliday(date)
124
- });
125
- date.add(1, 'days');
126
- }
127
- date.subtract(1, 'days');
128
- weekday = date.isoWeekday();
129
- if (weekday === 1) {
130
- return dayList.concat(new Array(6).fill({
131
- empty: moment(date).hour(1),
132
- lunarDate: this.convertLunarToSolar(date)
133
- }));
134
- }
135
- return dayList.concat(new Array(Math.abs(7 - weekday)).fill({
136
- empty: moment(date).hour(1),
137
- lunarDate: this.convertLunarToSolar(date)
138
- }));
139
- };
140
-
141
- getMonthList = (props) => {
142
- const minDate = moment((props || this.props).minDate).date(1);
143
- const maxDate = moment((props || this.props).maxDate);
144
- const monthList = [];
145
- if (!maxDate || !minDate) return monthList;
146
- while (maxDate > minDate || (
147
- maxDate.year() === minDate.year()
148
- && maxDate.month() === minDate.month()
149
- )) {
150
- const d = moment(minDate);
151
- const month = {
152
- date: d,
153
- dateList: this.getDayList(d)
154
- };
155
- month.shouldUpdate = this.shouldUpdate(month, props);
156
- monthList.push(month);
157
- minDate.add(1, 'month');
158
- }
159
- return monthList;
61
+ return !(
62
+ date.year() > end.year() ||
63
+ (date.year() === end.year() && date.month() > end.month())
64
+ );
65
+ };
66
+
67
+ shouldUpdate = (month, props) => {
68
+ if (!props) return false;
69
+ const {startDate, endDate} = props;
70
+ const {startDate: curStartDate, endDate: curEndDate} = this.props;
71
+ const {date} = month;
72
+ const next = this.checkRange(date, startDate, endDate);
73
+ const prev = this.checkRange(date, curStartDate, curEndDate);
74
+ return !!(prev || next);
75
+ };
76
+
77
+ convertLunarToSolar(date) {
78
+ return date
79
+ ? converter.SolarToLunar({
80
+ solarDay: date.date(),
81
+ solarMonth: date.month() + 1,
82
+ solarYear: date.year(),
83
+ })
84
+ : {};
85
+ }
86
+
87
+ findHoliday = date => {
88
+ const {holidays} = this.props;
89
+ if (date && holidays && holidays.length > 0) {
90
+ const day = date.date();
91
+ const month = date.month() + 1;
92
+ return holidays.find(item => item.day === day && item.month === month);
93
+ }
94
+ return null;
95
+ };
96
+
97
+ checkHoliday = (date, holidays) => {
98
+ const holiday = this.findHoliday(date, holidays);
99
+ return {
100
+ isSolarHoliday: !!(holiday && !holiday.lunar),
101
+ isLunarHoliday: !!(holiday && holiday.lunar),
160
102
  };
161
-
162
- getIndexOfMonth = (month, data) => data.findIndex((item) => item.date.isSame(month, 'month'));
163
-
164
- // componentDidMount() {
165
- // const {
166
- // selectedDate
167
- // } = this.props;
168
- // const data = this.getMonthList();
169
- // this.currentScrollIndex = this.getIndexOfMonth(moment(selectedDate), data);
170
- // this.setState({ data });
171
- // }
172
- // componentDidMount() {
173
- // if (this.list.current) {
174
- // this.scrollToMonth(moment(this.props.selectedDate));
175
- // }
176
- // }
177
-
178
- // componentWillUnmount() {
179
- // const { isDoubleDateMode } = this.props;
180
- // this.setState({
181
- // // data: [],
182
- // isDoubleDateMode
183
- // });
184
- // }
185
-
186
- getCurrentVisibleMonth = (info) => {
187
- const { changed } = info;
188
- const { onScrollCalendar } = this.props;
189
- if (changed && changed.length > 0) {
190
- const { item, key, index } = changed[0];
191
- if (onScrollCalendar && item && this.currentKey !== key) {
192
- this.currentKey = key;
193
- try {
194
- this.heightStyle = this.data && this.data[index] && this.data[index].dateList
195
- ? {
196
- height: (style.containerDayHeight * this.data[index].dateList.length / 7) + 10
197
- }
198
- : {};
199
- } catch (e) {
200
- this.heightStyle = {};
201
- }
202
- if (onScrollCalendar) {
203
- onScrollCalendar({ date: item.date, key, currentIndex: index });
103
+ };
104
+
105
+ getDayList = date => {
106
+ let dayList;
107
+ const month = date.month();
108
+ let weekday = date.isoWeekday();
109
+ if (weekday === 1) {
110
+ dayList = [];
111
+ } else {
112
+ dayList = new Array(weekday - 1).fill({
113
+ empty: moment(date).subtract(1, 'h'),
114
+ lunarDate: this.convertLunarToSolar(date),
115
+ ...this.checkHoliday(date),
116
+ });
117
+ }
118
+ while (date.month() === month) {
119
+ const cloned = moment(date);
120
+ dayList.push({
121
+ date: cloned,
122
+ lunarDate: this.convertLunarToSolar(cloned),
123
+ ...this.checkHoliday(date),
124
+ });
125
+ date.add(1, 'days');
126
+ }
127
+ date.subtract(1, 'days');
128
+ weekday = date.isoWeekday();
129
+ if (weekday === 1) {
130
+ return dayList.concat(
131
+ new Array(6).fill({
132
+ empty: moment(date).hour(1),
133
+ lunarDate: this.convertLunarToSolar(date),
134
+ }),
135
+ );
136
+ }
137
+ return dayList.concat(
138
+ new Array(Math.abs(7 - weekday)).fill({
139
+ empty: moment(date).hour(1),
140
+ lunarDate: this.convertLunarToSolar(date),
141
+ }),
142
+ );
143
+ };
144
+
145
+ getMonthList = props => {
146
+ const minDate = moment((props || this.props).minDate).date(1);
147
+ const maxDate = moment((props || this.props).maxDate);
148
+ const monthList = [];
149
+ if (!maxDate || !minDate) return monthList;
150
+ while (
151
+ maxDate > minDate ||
152
+ (maxDate.year() === minDate.year() && maxDate.month() === minDate.month())
153
+ ) {
154
+ const d = moment(minDate);
155
+ const month = {
156
+ date: d,
157
+ dateList: this.getDayList(d),
158
+ };
159
+ month.shouldUpdate = this.shouldUpdate(month, props);
160
+ monthList.push(month);
161
+ minDate.add(1, 'month');
162
+ }
163
+ return monthList;
164
+ };
165
+
166
+ getIndexOfMonth = (month, data) =>
167
+ data.findIndex(item => item.date.isSame(month, 'month'));
168
+
169
+ getCurrentVisibleMonth = info => {
170
+ const {changed} = info;
171
+ const {onScrollCalendar} = this.props;
172
+ if (changed && changed.length > 0) {
173
+ const {item, key, index} = changed[0];
174
+ if (onScrollCalendar && item && this.currentKey !== key) {
175
+ this.currentKey = key;
176
+ try {
177
+ this.heightStyle =
178
+ this.data && this.data[index] && this.data[index].dateList
179
+ ? {
180
+ height:
181
+ (style.containerDayHeight *
182
+ this.data[index].dateList.length) /
183
+ 7 +
184
+ 10,
204
185
  }
205
- }
186
+ : {};
187
+ } catch (e) {
188
+ this.heightStyle = {};
206
189
  }
207
- };
208
-
209
- keyExtractor = (item) => `${item.date.month() + 1}/${item.date.year()}`;
210
-
211
- scrollToMonth = (month) => {
212
- const index = this.getIndexOfMonth(month, this.data);
213
- if (this.list.current && index !== -1) {
214
- this.list.current.scrollToIndex({
215
- index,
216
- animated: true
217
- });
190
+ if (onScrollCalendar) {
191
+ onScrollCalendar({
192
+ date: item.date,
193
+ key,
194
+ currentIndex: index,
195
+ });
218
196
  }
219
- };
197
+ }
198
+ }
199
+ };
200
+
201
+ keyExtractor = item => `${item.date.month() + 1}/${item.date.year()}`;
220
202
 
221
- getItemLayout = (data, index) => (
222
- { length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index }
223
- )
224
-
225
- render() {
226
- const { priceList } = this.props;
227
- return (
228
- <FlatList
229
- extraData={priceList}
230
- style={this.heightStyle}
231
- horizontal
232
- pagingEnabled
233
- ref={this.list}
234
- data={this.data}
235
- renderItem={this.renderMonth}
236
- showsHorizontalScrollIndicator={false}
237
- keyExtractor={this.keyExtractor}
238
- onViewableItemsChanged={this.getCurrentVisibleMonth}
239
- onScrollToIndexFailed={() => {}}
240
- removeClippedSubviews
241
- initialNumToRender={1}
242
- maxToRenderPerBatch={MAX_RENDER_PER_BATCH}
243
- windowSize={3}
244
- contentContainerStyle={{ paddingTop: 5 }}
245
- initialScrollIndex={this.currentScrollIndex}
246
- getItemLayout={this.getItemLayout}
247
- viewabilityConfig={{
248
- itemVisiblePercentThreshold: 50
249
- }}
250
- />
251
- );
203
+ scrollToMonth = month => {
204
+ const index = this.getIndexOfMonth(month, this.data);
205
+ if (this.list.current && index !== -1) {
206
+ this.list.current.scrollToIndex({
207
+ index,
208
+ animated: true,
209
+ });
252
210
  }
211
+ };
212
+
213
+ getItemLayout = (data, index) => ({
214
+ length: ITEM_WIDTH,
215
+ offset: ITEM_WIDTH * index,
216
+ index,
217
+ });
218
+
219
+ render() {
220
+ const {priceList} = this.props;
221
+ return (
222
+ <FlatList
223
+ extraData={priceList}
224
+ style={this.heightStyle}
225
+ horizontal
226
+ pagingEnabled
227
+ ref={this.list}
228
+ data={this.data}
229
+ renderItem={this.renderMonth}
230
+ showsHorizontalScrollIndicator={false}
231
+ keyExtractor={this.keyExtractor}
232
+ onViewableItemsChanged={this.getCurrentVisibleMonth}
233
+ onScrollToIndexFailed={() => {}}
234
+ // removeClippedSubviews
235
+ scrollEnabled
236
+ initialNumToRender={1}
237
+ maxToRenderPerBatch={MAX_RENDER_PER_BATCH}
238
+ windowSize={3}
239
+ contentContainerStyle={{paddingTop: 5}}
240
+ initialScrollIndex={this.currentScrollIndex}
241
+ getItemLayout={this.getItemLayout}
242
+ viewabilityConfig={{
243
+ itemVisiblePercentThreshold: 50,
244
+ }}
245
+ />
246
+ );
247
+ }
253
248
  }
package/src/TabHeader.js CHANGED
@@ -1,79 +1,72 @@
1
1
  import React from 'react';
2
- import { TouchableWithoutFeedback, View, Text } from 'react-native';
2
+ import {TouchableWithoutFeedback, View} from 'react-native';
3
3
  import Moment from 'moment';
4
4
  import Util from './Util';
5
+ import {Colors, Text} from '@momo-kits/core-v2';
5
6
 
6
7
  export default class TabHeader extends React.Component {
7
- constructor(props) {
8
- super(props);
9
- this.state = {
10
- active: props.activeTab
11
- };
12
- this.label = props.label;
13
- this.defaultDate = props.date ? Moment(props.date) : '';
14
- }
15
-
16
- onChangeTab = () => {
17
- const { onChangeTab, id } = this.props;
18
- if (onChangeTab) {
19
- onChangeTab(id);
20
- }
21
- };
22
-
23
- updateView = (date, activeTab) => {
24
- this.setState({
25
- date: date ? Moment(date) : '',
26
- active: activeTab
27
- });
8
+ constructor(props) {
9
+ super(props);
10
+ this.state = {
11
+ active: props.activeTab,
28
12
  };
13
+ this.label = props.label;
14
+ this.defaultDate = props.date ? Moment(props.date) : '';
15
+ }
29
16
 
30
- setActiveTab = (active) => {
31
- this.setState({ active });
32
- };
17
+ onChangeTab = () => {
18
+ const {onChangeTab, id} = this.props;
19
+ if (onChangeTab) {
20
+ onChangeTab(id);
21
+ }
22
+ };
33
23
 
34
- render() {
35
- const { label, disabled } = this.props;
36
- const { date, active } = this.state;
37
- const formattedDateFromState = date ? date.format('DD/MM/YYYY') : '';
38
- const formattedDateFromDefault = this.defaultDate ? this.defaultDate.format('DD/MM/YYYY') : '';
39
- const dayOfWeekFromState = date ? `${Util.WEEKDAYSFROMMOMENT[date.day()]} -` : '';
40
- const dayOfWeekFromDefault = this.defaultDate ? `${Util.WEEKDAYSFROMMOMENT[this.defaultDate.day()]} -` : '';
24
+ updateView = (date, activeTab) => {
25
+ this.setState({
26
+ date: date ? Moment(date) : '',
27
+ active: activeTab,
28
+ });
29
+ };
41
30
 
42
- return (
43
- <TouchableWithoutFeedback disabled={disabled} onPress={this.onChangeTab}>
44
- <View style={{
45
- flex: 1,
46
- justifyContent: 'center',
47
- alignItems: 'center',
48
- backgroundColor: active ? 'white' : '#eeeeef',
49
- borderRadius: 8
50
- }}
51
- >
52
- <Text style={{
53
- fontSize: 12,
54
- // lineHeight: 14,
55
- color: active ? '#222222' : '#8d919d',
56
- fontWeight: 'bold'
57
- }}
58
- >
59
- {label}
31
+ setActiveTab = active => {
32
+ this.setState({active});
33
+ };
60
34
 
61
- </Text>
62
- <Text style={{
63
- marginTop: 3,
64
- fontSize: 12,
65
- // lineHeight: 14,
66
- color: '#8d919d'
67
- }}
68
- >
69
- <Text style={{ color: active ? '#b0006d' : '#8d919d', fontWeight: 'bold' }}>
70
- {`${dayOfWeekFromState || dayOfWeekFromDefault || '--'} `}
35
+ render() {
36
+ const {label, disabled} = this.props;
37
+ const {date, active} = this.state;
38
+ const formattedDateFromState = date ? date.format('DD/MM/YYYY') : '';
39
+ const formattedDateFromDefault = this.defaultDate
40
+ ? this.defaultDate.format('DD/MM/YYYY')
41
+ : '';
42
+ const dayOfWeekFromState = date
43
+ ? `${Util.WEEKDAYSFROMMOMENT[date.day()]} -`
44
+ : '';
45
+ const dayOfWeekFromDefault = this.defaultDate
46
+ ? `${Util.WEEKDAYSFROMMOMENT[this.defaultDate.day()]} -`
47
+ : '';
71
48
 
72
- </Text>
73
- {formattedDateFromState || formattedDateFromDefault || '--/--/----'}
74
- </Text>
75
- </View>
76
- </TouchableWithoutFeedback>
77
- );
78
- }
49
+ return (
50
+ <TouchableWithoutFeedback disabled={disabled} onPress={this.onChangeTab}>
51
+ <View
52
+ style={{
53
+ flex: 1,
54
+ backgroundColor: Colors.white,
55
+ }}>
56
+ <Text.Description2
57
+ style={{
58
+ fontSize: 12,
59
+ lineHeight: 16,
60
+ color: Colors.black_12,
61
+ }}>
62
+ {label}
63
+ </Text.Description2>
64
+ <Text.Description1 style={{fontWeight: active ? '600' : '400'}}>
65
+ {`${dayOfWeekFromState || dayOfWeekFromDefault || '--'} `}
66
+ {formattedDateFromState || formattedDateFromDefault || '--/--/----'}
67
+ </Text.Description1>
68
+ </View>
69
+ </TouchableWithoutFeedback>
70
+ );
71
+ }
79
72
  }