@momo-kits/calendar 0.150.1-rn80.9 → 0.150.2-beta.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/index.tsx +45 -34
- package/package.json +1 -1
package/index.tsx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable react-hooks/exhaustive-deps */
|
|
1
2
|
import React, {
|
|
2
3
|
useState,
|
|
3
4
|
useRef,
|
|
@@ -5,7 +6,7 @@ import React, {
|
|
|
5
6
|
useContext,
|
|
6
7
|
createContext,
|
|
7
8
|
} from 'react';
|
|
8
|
-
import {Dimensions, LayoutChangeEvent, View} from 'react-native';
|
|
9
|
+
import { Dimensions, LayoutChangeEvent, View } from 'react-native';
|
|
9
10
|
import moment from 'moment';
|
|
10
11
|
import {
|
|
11
12
|
ApplicationContext,
|
|
@@ -17,15 +18,15 @@ import {
|
|
|
17
18
|
} from '@momo-kits/foundation';
|
|
18
19
|
import CalendarPro from './CalendarPro';
|
|
19
20
|
import TabHeader from './TabHeader';
|
|
20
|
-
import {CalendarProps, CalendarProRef} from './types';
|
|
21
|
+
import { CalendarProps, CalendarProRef } from './types';
|
|
21
22
|
import styles from './styles';
|
|
22
23
|
|
|
23
24
|
const DOUBLE = 'doubleDate';
|
|
24
25
|
|
|
25
|
-
export const ContainerContext = createContext({width: 0, height: 0});
|
|
26
|
+
export const ContainerContext = createContext({ width: 0, height: 0 });
|
|
26
27
|
|
|
27
28
|
const Calendar: React.FC<CalendarProps> = props => {
|
|
28
|
-
const {translate, theme} = useContext(ApplicationContext);
|
|
29
|
+
const { translate, theme } = useContext(ApplicationContext);
|
|
29
30
|
|
|
30
31
|
// derive defaults for date bounds
|
|
31
32
|
const currentDate = new Date();
|
|
@@ -57,17 +58,17 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
57
58
|
} = props;
|
|
58
59
|
|
|
59
60
|
const [containerWidth, setContainerWidth] = useState<number>(
|
|
60
|
-
Dimensions.get('window').width
|
|
61
|
+
Dimensions.get('window').width,
|
|
61
62
|
);
|
|
62
63
|
const [isDoubleDateMode, setIsDoubleDateMode] = useState<boolean>(
|
|
63
|
-
mode === DOUBLE
|
|
64
|
+
mode === DOUBLE,
|
|
64
65
|
);
|
|
65
66
|
const [selectedDate, setSelectedDate] = useState<moment.Moment>(
|
|
66
67
|
initialSelectedDate
|
|
67
68
|
? moment(initialSelectedDate)
|
|
68
69
|
: mode === DOUBLE && initDoubleDate && initDoubleDate.first
|
|
69
70
|
? moment(initDoubleDate.first)
|
|
70
|
-
: moment()
|
|
71
|
+
: moment(),
|
|
71
72
|
);
|
|
72
73
|
|
|
73
74
|
const doubleDate = useRef<{
|
|
@@ -79,7 +80,7 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
79
80
|
first: initDoubleDate.first ? moment(initDoubleDate.first) : null,
|
|
80
81
|
second: initDoubleDate.second ? moment(initDoubleDate.second) : null,
|
|
81
82
|
}
|
|
82
|
-
: {first: null, second: null}
|
|
83
|
+
: { first: null, second: null },
|
|
83
84
|
);
|
|
84
85
|
const tabSelected = useRef<number>(id || 0);
|
|
85
86
|
|
|
@@ -105,13 +106,13 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
105
106
|
calendarPicker.current.setDoubleDateAndTabIndex(
|
|
106
107
|
doubleDate.current.first,
|
|
107
108
|
doubleDate.current.second,
|
|
108
|
-
tabSelected.current
|
|
109
|
+
tabSelected.current,
|
|
109
110
|
);
|
|
110
111
|
}
|
|
111
112
|
} else {
|
|
112
113
|
calendarPicker.current?.setDoubleDateAndTabIndex(selectedDate);
|
|
113
114
|
}
|
|
114
|
-
}, [
|
|
115
|
+
}, []);
|
|
115
116
|
|
|
116
117
|
const onLayout = (e: LayoutChangeEvent) =>
|
|
117
118
|
setContainerWidth(e.nativeEvent.layout.width);
|
|
@@ -124,13 +125,14 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
124
125
|
calendarPicker.current?.setDoubleDateAndTabIndex(
|
|
125
126
|
doubleDate.current.first,
|
|
126
127
|
doubleDate.current.second,
|
|
127
|
-
tabSelected.current
|
|
128
|
+
tabSelected.current,
|
|
128
129
|
);
|
|
129
130
|
};
|
|
130
131
|
|
|
131
132
|
const processDateFirst = () => {
|
|
132
|
-
const {onDateChange, onCTAStateChange} = props;
|
|
133
|
-
if (!cellHeader1.current || !cellHeader2.current || !calendarPicker.current)
|
|
133
|
+
const { onDateChange, onCTAStateChange } = props;
|
|
134
|
+
if (!cellHeader1.current || !cellHeader2.current || !calendarPicker.current)
|
|
135
|
+
return;
|
|
134
136
|
// set first date and reset second
|
|
135
137
|
doubleDate.current.first = selectedDate;
|
|
136
138
|
doubleDate.current.second = null;
|
|
@@ -142,7 +144,7 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
142
144
|
calendarPicker.current.setDoubleDateAndTabIndex(
|
|
143
145
|
doubleDate.current.first,
|
|
144
146
|
doubleDate.current.second,
|
|
145
|
-
tabSelected.current
|
|
147
|
+
tabSelected.current,
|
|
146
148
|
);
|
|
147
149
|
// notify parent
|
|
148
150
|
onDateChange?.({ first: doubleDate.current.first!, second: null });
|
|
@@ -150,8 +152,9 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
150
152
|
};
|
|
151
153
|
|
|
152
154
|
const processDateSecond = () => {
|
|
153
|
-
const {onDateChange, onCTAStateChange} = props;
|
|
154
|
-
if (!cellHeader1.current || !cellHeader2.current || !calendarPicker.current)
|
|
155
|
+
const { onDateChange, onCTAStateChange } = props;
|
|
156
|
+
if (!cellHeader1.current || !cellHeader2.current || !calendarPicker.current)
|
|
157
|
+
return;
|
|
155
158
|
// if selectedDate is before first, treat as new first date
|
|
156
159
|
if (selectedDate.isBefore(doubleDate.current.first!, 'day')) {
|
|
157
160
|
doubleDate.current.first = selectedDate;
|
|
@@ -162,7 +165,7 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
162
165
|
calendarPicker.current.setDoubleDateAndTabIndex(
|
|
163
166
|
doubleDate.current.first,
|
|
164
167
|
doubleDate.current.second,
|
|
165
|
-
tabSelected.current
|
|
168
|
+
tabSelected.current,
|
|
166
169
|
);
|
|
167
170
|
onDateChange?.({ first: doubleDate.current.first!, second: null });
|
|
168
171
|
onCTAStateChange?.(false);
|
|
@@ -175,9 +178,12 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
175
178
|
calendarPicker.current.setDoubleDateAndTabIndex(
|
|
176
179
|
doubleDate.current.first,
|
|
177
180
|
doubleDate.current.second,
|
|
178
|
-
tabSelected.current
|
|
181
|
+
tabSelected.current,
|
|
179
182
|
);
|
|
180
|
-
onDateChange?.({
|
|
183
|
+
onDateChange?.({
|
|
184
|
+
first: doubleDate.current.first!,
|
|
185
|
+
second: doubleDate.current.second!,
|
|
186
|
+
});
|
|
181
187
|
onCTAStateChange?.(true);
|
|
182
188
|
}
|
|
183
189
|
};
|
|
@@ -187,7 +193,7 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
187
193
|
};
|
|
188
194
|
|
|
189
195
|
const updateView = () => {
|
|
190
|
-
const {onDateChange} = props;
|
|
196
|
+
const { onDateChange } = props;
|
|
191
197
|
if (isDoubleDateMode) {
|
|
192
198
|
processDoubleDate();
|
|
193
199
|
} else {
|
|
@@ -210,14 +216,14 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
210
216
|
const onDateChangeHandler = (date: moment.Moment) => setSelectedDate(date);
|
|
211
217
|
|
|
212
218
|
const updateHeaderView = () => {
|
|
213
|
-
const {onDateChange, onCTAStateChange} = props;
|
|
219
|
+
const { onDateChange, onCTAStateChange } = props;
|
|
214
220
|
if (isDoubleDateMode) {
|
|
215
221
|
cellHeader1.current?.updateView(selectedDate, true);
|
|
216
222
|
cellHeader2.current?.updateView(null, false);
|
|
217
223
|
calendarPicker.current?.setDoubleDateAndTabIndex(
|
|
218
224
|
selectedDate,
|
|
219
225
|
null,
|
|
220
|
-
tabSelected.current
|
|
226
|
+
tabSelected.current,
|
|
221
227
|
);
|
|
222
228
|
doubleDate.current.first = moment(selectedDate);
|
|
223
229
|
doubleDate.current.second = null;
|
|
@@ -227,10 +233,10 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
227
233
|
calendarPicker.current?.setDoubleDateAndTabIndex(
|
|
228
234
|
doubleDate.current.first,
|
|
229
235
|
doubleDate.current.second,
|
|
230
|
-
tabSelected.current
|
|
236
|
+
tabSelected.current,
|
|
231
237
|
);
|
|
232
238
|
onCTAStateChange?.(false);
|
|
233
|
-
onDateChange?.({first: doubleDate.current.first!, second: null});
|
|
239
|
+
onDateChange?.({ first: doubleDate.current.first!, second: null });
|
|
234
240
|
} else {
|
|
235
241
|
// Reset second date when switching to single mode
|
|
236
242
|
doubleDate.current.second = null;
|
|
@@ -269,8 +275,9 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
269
275
|
<View
|
|
270
276
|
style={[
|
|
271
277
|
styles.viewSwitch,
|
|
272
|
-
{backgroundColor: theme.colors.background.surface},
|
|
273
|
-
]}
|
|
278
|
+
{ backgroundColor: theme.colors.background.surface },
|
|
279
|
+
]}
|
|
280
|
+
>
|
|
274
281
|
<Text typography="label_default_medium">
|
|
275
282
|
{props.titleHeader || translate?.('chooseRoundtrip')}
|
|
276
283
|
</Text>
|
|
@@ -285,8 +292,9 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
285
292
|
<View
|
|
286
293
|
style={[
|
|
287
294
|
styles.viewPanel,
|
|
288
|
-
{backgroundColor: theme.colors.background.default},
|
|
289
|
-
]}
|
|
295
|
+
{ backgroundColor: theme.colors.background.default },
|
|
296
|
+
]}
|
|
297
|
+
>
|
|
290
298
|
<TabHeader
|
|
291
299
|
id={0}
|
|
292
300
|
ref={cellHeader1}
|
|
@@ -295,7 +303,7 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
295
303
|
activeTab
|
|
296
304
|
date={doubleDate.current.first}
|
|
297
305
|
/>
|
|
298
|
-
<View style={{width: 4, height: '100%'}} />
|
|
306
|
+
<View style={{ width: 4, height: '100%' }} />
|
|
299
307
|
<TabHeader
|
|
300
308
|
id={1}
|
|
301
309
|
ref={cellHeader2}
|
|
@@ -309,8 +317,9 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
309
317
|
<View
|
|
310
318
|
style={[
|
|
311
319
|
styles.viewPanel,
|
|
312
|
-
{backgroundColor: theme.colors.background.default},
|
|
313
|
-
]}
|
|
320
|
+
{ backgroundColor: theme.colors.background.default },
|
|
321
|
+
]}
|
|
322
|
+
>
|
|
314
323
|
<TabHeader
|
|
315
324
|
id={0}
|
|
316
325
|
disabled
|
|
@@ -327,7 +336,8 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
327
336
|
value={{
|
|
328
337
|
width: containerWidth,
|
|
329
338
|
height: priceList ? scaleSize(48) : scaleSize(34),
|
|
330
|
-
}}
|
|
339
|
+
}}
|
|
340
|
+
>
|
|
331
341
|
<View onLayout={onLayout} style={[style, styles.scrollView]}>
|
|
332
342
|
<View
|
|
333
343
|
style={[
|
|
@@ -337,7 +347,8 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
337
347
|
marginBottom: Spacing.S,
|
|
338
348
|
},
|
|
339
349
|
hideHeaderTab && styles.invisible,
|
|
340
|
-
]}
|
|
350
|
+
]}
|
|
351
|
+
>
|
|
341
352
|
{renderSwitchReturnSelection()}
|
|
342
353
|
{renderHeaderPanel()}
|
|
343
354
|
</View>
|
|
@@ -365,4 +376,4 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
365
376
|
);
|
|
366
377
|
};
|
|
367
378
|
|
|
368
|
-
export {Calendar};
|
|
379
|
+
export { Calendar };
|