@momo-kits/date-picker 0.169.4-beta.3 → 0.169.5
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/WheelPicker.tsx +24 -36
- package/WheelPickerItem.tsx +4 -12
- package/index.tsx +35 -22
- package/package.json +1 -1
- package/styles.ts +1 -3
- package/types.ts +6 -11
- package/utils.ts +6 -0
package/WheelPicker.tsx
CHANGED
|
@@ -5,11 +5,9 @@ import React, {
|
|
|
5
5
|
useContext,
|
|
6
6
|
useEffect,
|
|
7
7
|
useRef,
|
|
8
|
-
useState,
|
|
9
8
|
} from 'react';
|
|
10
9
|
import {
|
|
11
10
|
FlatList,
|
|
12
|
-
LayoutChangeEvent,
|
|
13
11
|
ListRenderItemInfo,
|
|
14
12
|
NativeScrollEvent,
|
|
15
13
|
NativeSyntheticEvent,
|
|
@@ -25,13 +23,10 @@ import { WheelPickerProps } from './types';
|
|
|
25
23
|
import { debounce, itemSize } from './utils';
|
|
26
24
|
import WheelPickerItem from './WheelPickerItem';
|
|
27
25
|
|
|
28
|
-
const VISIBLE_ROWS = 5;
|
|
29
|
-
|
|
30
26
|
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
|
|
31
|
-
|
|
32
27
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
28
|
+
* Snapping 5-row wheel; reports the centered item to `onChange` on scroll end.
|
|
29
|
+
* `data` carries two empty padding items at each end so any real row can center.
|
|
35
30
|
*/
|
|
36
31
|
const WheelPicker: FC<WheelPickerProps> = ({
|
|
37
32
|
name,
|
|
@@ -45,10 +40,9 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
45
40
|
const scrollAnimatedValue = useSharedValue(0);
|
|
46
41
|
const canMomentum = useRef(false);
|
|
47
42
|
const initItemNum = 15;
|
|
48
|
-
const [rowSize, setRowSize] = useState(itemSize);
|
|
49
43
|
|
|
50
44
|
const onScroll = useAnimatedScrollHandler({
|
|
51
|
-
onScroll:
|
|
45
|
+
onScroll: event => {
|
|
52
46
|
scrollAnimatedValue.value = event.contentOffset.y;
|
|
53
47
|
},
|
|
54
48
|
});
|
|
@@ -59,7 +53,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
59
53
|
debouncedScrollEnd.cancel();
|
|
60
54
|
};
|
|
61
55
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- debouncedScrollEnd is recreated every render; re-center only when the list length changes
|
|
62
|
-
}, [data.length
|
|
56
|
+
}, [data.length]);
|
|
63
57
|
|
|
64
58
|
const findClosestIndex = (array: string[], value: string) => {
|
|
65
59
|
let closest = array.reduce((a, b) => {
|
|
@@ -96,17 +90,19 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
96
90
|
});
|
|
97
91
|
}, 100);
|
|
98
92
|
|
|
99
|
-
const renderItem = useCallback(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
93
|
+
const renderItem = useCallback(
|
|
94
|
+
({ item, index }: ListRenderItemInfo<any>) => {
|
|
95
|
+
return (
|
|
96
|
+
<WheelPickerItem
|
|
97
|
+
item={item}
|
|
98
|
+
index={index}
|
|
99
|
+
scrollAnimatedValue={scrollAnimatedValue}
|
|
100
|
+
itemSize={itemSize}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
[scrollAnimatedValue],
|
|
105
|
+
);
|
|
110
106
|
|
|
111
107
|
const renderOverlay = () => {
|
|
112
108
|
return (
|
|
@@ -119,11 +115,9 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
119
115
|
const getItemLayout = (
|
|
120
116
|
_data: ArrayLike<unknown> | null | undefined,
|
|
121
117
|
index: number,
|
|
122
|
-
) =>
|
|
123
|
-
length:
|
|
124
|
-
|
|
125
|
-
index,
|
|
126
|
-
});
|
|
118
|
+
) => {
|
|
119
|
+
return { length: itemSize, offset: itemSize * index, index };
|
|
120
|
+
};
|
|
127
121
|
|
|
128
122
|
const onMomentumScrollBegin = () => {
|
|
129
123
|
canMomentum.current = true;
|
|
@@ -131,31 +125,25 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
131
125
|
|
|
132
126
|
const onMomentumScrollEnd = (e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
133
127
|
if (canMomentum.current) {
|
|
134
|
-
let index = Math.round(e.nativeEvent.contentOffset.y /
|
|
128
|
+
let index = Math.round(e.nativeEvent.contentOffset.y / itemSize);
|
|
135
129
|
onChange?.(name, data[index + 2]);
|
|
136
130
|
}
|
|
137
131
|
canMomentum.current = false;
|
|
138
132
|
};
|
|
139
133
|
|
|
140
|
-
const handleLayout = (e: LayoutChangeEvent) => {
|
|
141
|
-
const measured = e.nativeEvent.layout.height / VISIBLE_ROWS;
|
|
142
|
-
if (measured > 0 && measured !== rowSize) setRowSize(measured);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
134
|
const onScrollToIndexFailed = (error: any) => {
|
|
146
135
|
console.warn('FlatList scrollToIndex failed', error);
|
|
147
136
|
};
|
|
148
137
|
|
|
149
138
|
return (
|
|
150
139
|
<View
|
|
151
|
-
onLayout={handleLayout}
|
|
152
140
|
style={[
|
|
153
141
|
style,
|
|
154
142
|
styles.wheelPicker,
|
|
155
143
|
{
|
|
156
144
|
borderColor: theme.colors.border.default,
|
|
157
145
|
backgroundColor: theme.colors.background.surface,
|
|
158
|
-
maxHeight: itemSize *
|
|
146
|
+
maxHeight: itemSize * 5,
|
|
159
147
|
},
|
|
160
148
|
]}
|
|
161
149
|
>
|
|
@@ -164,8 +152,8 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
164
152
|
windowSize={7}
|
|
165
153
|
initialNumToRender={initItemNum}
|
|
166
154
|
maxToRenderPerBatch={initItemNum}
|
|
167
|
-
snapToInterval={
|
|
168
|
-
snapToOffsets={data.map((_, index) => index *
|
|
155
|
+
snapToInterval={itemSize}
|
|
156
|
+
snapToOffsets={data.map((_, index) => index * itemSize)}
|
|
169
157
|
keyExtractor={(item, index) => `Wheel picker item ${item}-${index}`}
|
|
170
158
|
decelerationRate={'fast'}
|
|
171
159
|
getItemLayout={getItemLayout}
|
package/WheelPickerItem.tsx
CHANGED
|
@@ -8,23 +8,19 @@ import styles from './styles';
|
|
|
8
8
|
import { exportFontFamily, useScaleSize } from '@momo-kits/foundation';
|
|
9
9
|
import { WheelPickerItemProps } from './types';
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* One wheel row; fades and shrinks with its distance from the centre, counted in
|
|
13
|
-
* rendered rows (`rowSize`) because the scroll offset is in rendered points.
|
|
14
|
-
*/
|
|
11
|
+
/** Wheel row that fades and shrinks with its distance from the centered row. */
|
|
15
12
|
const WheelPickerItem: FC<WheelPickerItemProps> = ({
|
|
16
13
|
item,
|
|
17
14
|
index,
|
|
18
15
|
scrollAnimatedValue,
|
|
19
16
|
itemSize,
|
|
20
|
-
rowSize,
|
|
21
17
|
}) => {
|
|
22
18
|
const scaledFontSize = useScaleSize(16);
|
|
23
19
|
const scaledLineHeight = useScaleSize(22);
|
|
24
20
|
const itemFontFamily = exportFontFamily('semibold');
|
|
25
21
|
|
|
26
22
|
const containerStyle = useAnimatedStyle(() => {
|
|
27
|
-
const distance = index - scrollAnimatedValue.value /
|
|
23
|
+
const distance = index - scrollAnimatedValue.value / itemSize;
|
|
28
24
|
const opacity = interpolate(
|
|
29
25
|
distance,
|
|
30
26
|
[0, 1, 2, 3, 4],
|
|
@@ -35,7 +31,7 @@ const WheelPickerItem: FC<WheelPickerItemProps> = ({
|
|
|
35
31
|
});
|
|
36
32
|
|
|
37
33
|
const textStyle = useAnimatedStyle(() => {
|
|
38
|
-
const distance = index - scrollAnimatedValue.value /
|
|
34
|
+
const distance = index - scrollAnimatedValue.value / itemSize;
|
|
39
35
|
const scale = interpolate(
|
|
40
36
|
distance,
|
|
41
37
|
[0, 1, 2, 3, 4],
|
|
@@ -47,11 +43,7 @@ const WheelPickerItem: FC<WheelPickerItemProps> = ({
|
|
|
47
43
|
|
|
48
44
|
return (
|
|
49
45
|
<Animated.View
|
|
50
|
-
style={[
|
|
51
|
-
styles.wheelItem,
|
|
52
|
-
{ height: itemSize },
|
|
53
|
-
containerStyle,
|
|
54
|
-
]}
|
|
46
|
+
style={[styles.wheelItem, { height: itemSize }, containerStyle]}
|
|
55
47
|
>
|
|
56
48
|
<Animated.Text
|
|
57
49
|
allowFontScaling={false}
|
package/index.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { FC, useEffect, useMemo, useState } from 'react';
|
|
2
|
-
import { View } from 'react-native';
|
|
3
|
-
import { Text } from '@momo-kits/foundation';
|
|
2
|
+
import { Dimensions, LayoutChangeEvent, View } from 'react-native';
|
|
3
|
+
import { Spacing, Text } from '@momo-kits/foundation';
|
|
4
4
|
import WheelPicker from './WheelPicker';
|
|
5
5
|
import {
|
|
6
6
|
getDaysInMonth,
|
|
@@ -19,10 +19,11 @@ const maxDateDefault = new Date(date);
|
|
|
19
19
|
minDateDefault.setFullYear(minDateDefault.getFullYear() - 10);
|
|
20
20
|
maxDateDefault.setFullYear(maxDateDefault.getFullYear() + 10);
|
|
21
21
|
date.setHours(0, 0);
|
|
22
|
+
const screenWidth = Dimensions.get('window').width;
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
25
|
+
* Wheel date/time picker, one column per `format` token, clamped to min/max.
|
|
26
|
+
* `onChange` also fires on mount with the clamped initial value.
|
|
26
27
|
*/
|
|
27
28
|
const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
28
29
|
format = 'DD-MM-YYYY',
|
|
@@ -42,6 +43,7 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
42
43
|
hour: selectedValue.getHours(),
|
|
43
44
|
min: selectedValue.getMinutes(),
|
|
44
45
|
});
|
|
46
|
+
const [containerWidth, setContainerWidth] = useState(screenWidth);
|
|
45
47
|
|
|
46
48
|
useEffect(() => {
|
|
47
49
|
setupData();
|
|
@@ -63,11 +65,11 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
63
65
|
|
|
64
66
|
const formatParts = format.split(/[^A-Za-z]+/);
|
|
65
67
|
const isOnlyHour = formatParts.length === 1 && formatParts[0] === 'HH';
|
|
66
|
-
const dayData = useMemo(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
);
|
|
68
|
+
const dayData = useMemo(() => {
|
|
69
|
+
const year = +currentDate.year;
|
|
70
|
+
const month = +currentDate.month;
|
|
71
|
+
return getDaysInMonth(year, month, minDate, maxDate);
|
|
72
|
+
}, [currentDate.year, currentDate.month, minDate, maxDate]);
|
|
71
73
|
const monthData = useMemo(
|
|
72
74
|
() => getMonths(minDate, maxDate, +currentDate.year),
|
|
73
75
|
[currentDate.year, minDate, maxDate],
|
|
@@ -114,6 +116,8 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
114
116
|
setData(initialData);
|
|
115
117
|
};
|
|
116
118
|
|
|
119
|
+
const numOfColumns = data.length;
|
|
120
|
+
|
|
117
121
|
const toHour12 = (hour: number) => Number(hour) % 12 || 12;
|
|
118
122
|
const toTimeMode = (hour: number) => (Number(hour) >= 12 ? 'PM' : 'AM');
|
|
119
123
|
|
|
@@ -135,10 +139,14 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
135
139
|
|
|
136
140
|
// In 12-hour mode the hour and AM/PM wheels both map onto the 24-hour `hour`.
|
|
137
141
|
if (isOnlyHour && (name === 'hour' || name === 'timeMode')) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
+
let hour12 = toHour12(currentDate.hour);
|
|
143
|
+
if (name === 'hour') {
|
|
144
|
+
hour12 = Number(dataItem);
|
|
145
|
+
}
|
|
146
|
+
let mode = toTimeMode(currentDate.hour);
|
|
147
|
+
if (name === 'timeMode') {
|
|
148
|
+
mode = dataItem;
|
|
149
|
+
}
|
|
142
150
|
newDate.hour = (hour12 % 12) + (mode === 'PM' ? 12 : 0);
|
|
143
151
|
}
|
|
144
152
|
|
|
@@ -157,10 +165,9 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
157
165
|
newDate.min,
|
|
158
166
|
);
|
|
159
167
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
) {
|
|
168
|
+
const newValue = JSON.stringify(newDate.valueOf());
|
|
169
|
+
const currentValue = JSON.stringify(currentDate.valueOf());
|
|
170
|
+
if (newValue !== currentValue) {
|
|
164
171
|
if (changedDate < minDate) {
|
|
165
172
|
onChange?.(minDate);
|
|
166
173
|
} else if (changedDate > maxDate) {
|
|
@@ -171,15 +178,20 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
171
178
|
};
|
|
172
179
|
|
|
173
180
|
const paddingArray = ['', ''];
|
|
181
|
+
const columnGaps = Spacing.M * 2 + (numOfColumns - 1) * Spacing.M;
|
|
182
|
+
const columnWidth = (containerWidth - columnGaps) / numOfColumns;
|
|
183
|
+
|
|
184
|
+
const onLayout = (e: LayoutChangeEvent) => {
|
|
185
|
+
if (containerWidth !== e.nativeEvent.layout.width) {
|
|
186
|
+
setContainerWidth(e.nativeEvent.layout.width);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
174
189
|
|
|
175
190
|
return (
|
|
176
|
-
<View style={[style, styles.datePicker]}>
|
|
191
|
+
<View onLayout={onLayout} style={[style, styles.datePicker]}>
|
|
177
192
|
{data.map((dataItem, index) => {
|
|
178
193
|
return (
|
|
179
|
-
<View
|
|
180
|
-
key={`${dataItem.name}_${index}`}
|
|
181
|
-
style={[styles.column, index > 0 && styles.columnGap]}
|
|
182
|
-
>
|
|
194
|
+
<View key={`${dataItem.name}_${index}`}>
|
|
183
195
|
{arrayLabelTime[index] && (
|
|
184
196
|
<View style={styles.dateTimeContainer}>
|
|
185
197
|
<Text typography="action_s_bold">{arrayLabelTime[index]}</Text>
|
|
@@ -190,6 +202,7 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
190
202
|
onChange={onPickerChange}
|
|
191
203
|
data={[...paddingArray, ...dataItem.data, ...paddingArray]}
|
|
192
204
|
name={dataItem.name}
|
|
205
|
+
style={{ width: columnWidth }}
|
|
193
206
|
/>
|
|
194
207
|
</View>
|
|
195
208
|
);
|
package/package.json
CHANGED
package/styles.ts
CHANGED
|
@@ -25,9 +25,7 @@ export default StyleSheet.create({
|
|
|
25
25
|
datePicker: {
|
|
26
26
|
overflow: 'hidden',
|
|
27
27
|
flexDirection: 'row',
|
|
28
|
-
|
|
28
|
+
justifyContent: 'space-evenly',
|
|
29
29
|
},
|
|
30
|
-
column: { flex: 1 },
|
|
31
|
-
columnGap: { marginLeft: Spacing.M },
|
|
32
30
|
dateTimeContainer: { alignItems: 'center', marginBottom: Spacing.S },
|
|
33
31
|
});
|
package/types.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {ViewStyle} from 'react-native';
|
|
2
|
-
import type {SharedValue} from 'react-native-reanimated';
|
|
1
|
+
import { ViewStyle } from 'react-native';
|
|
2
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
3
3
|
|
|
4
4
|
export type DateTimePickerProps = {
|
|
5
5
|
/**
|
|
6
|
-
* Optional.
|
|
6
|
+
* Optional. Column tokens `DD`, `MM`, `YYYY`, `HH`, `mm` joined by separators.
|
|
7
|
+
* `HH` alone shows a 12-hour wheel plus an AM/PM wheel.
|
|
7
8
|
*/
|
|
8
9
|
format?: string;
|
|
9
10
|
|
|
@@ -32,7 +33,7 @@ export type DateTimePickerProps = {
|
|
|
32
33
|
*/
|
|
33
34
|
maxDate?: Date;
|
|
34
35
|
/**
|
|
35
|
-
* Optional.
|
|
36
|
+
* Optional. Labels shown above the columns, matched by column index.
|
|
36
37
|
*/
|
|
37
38
|
arrayLabelTime?: string[];
|
|
38
39
|
|
|
@@ -81,7 +82,7 @@ export type WheelPickerProps = {
|
|
|
81
82
|
/**
|
|
82
83
|
* Custom styles to apply to the WheelPicker component.
|
|
83
84
|
*/
|
|
84
|
-
style
|
|
85
|
+
style: ViewStyle;
|
|
85
86
|
|
|
86
87
|
/**
|
|
87
88
|
* A callback function that is triggered when the selected data changes.
|
|
@@ -99,10 +100,4 @@ export type WheelPickerItemProps = {
|
|
|
99
100
|
index: number;
|
|
100
101
|
scrollAnimatedValue: SharedValue<number>;
|
|
101
102
|
itemSize: number;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Rendered row height that scroll offsets are measured in; differs from `itemSize`
|
|
105
|
-
* when sizes are scaled.
|
|
106
|
-
*/
|
|
107
|
-
rowSize: number;
|
|
108
103
|
};
|
package/utils.ts
CHANGED
|
@@ -3,6 +3,7 @@ const itemSize = 42;
|
|
|
3
3
|
const paddingNum = (num: number) => {
|
|
4
4
|
return num > 9 ? String(num) : `0${num}`;
|
|
5
5
|
};
|
|
6
|
+
/** Zero-padded days of `month` (1-based) in `year`, trimmed to min/max. */
|
|
6
7
|
const getDaysInMonth = (
|
|
7
8
|
year: number,
|
|
8
9
|
month: number,
|
|
@@ -29,6 +30,7 @@ const getDaysInMonth = (
|
|
|
29
30
|
return days;
|
|
30
31
|
};
|
|
31
32
|
|
|
33
|
+
/** Zero-padded months (1–12) of `currentYear`, trimmed to min/max. */
|
|
32
34
|
const getMonths = (minDate: Date, maxDate: Date, currentYear: number) => {
|
|
33
35
|
let startMonth = 1;
|
|
34
36
|
let endMonth = 12;
|
|
@@ -48,6 +50,7 @@ const getMonths = (minDate: Date, maxDate: Date, currentYear: number) => {
|
|
|
48
50
|
return months;
|
|
49
51
|
};
|
|
50
52
|
|
|
53
|
+
/** Years from `minDate` to `maxDate`, inclusive. */
|
|
51
54
|
const getYears = (minDate: Date, maxDate: Date) => {
|
|
52
55
|
const startYear = minDate.getFullYear();
|
|
53
56
|
const endYear = maxDate.getFullYear();
|
|
@@ -59,6 +62,7 @@ const getYears = (minDate: Date, maxDate: Date) => {
|
|
|
59
62
|
return years;
|
|
60
63
|
};
|
|
61
64
|
|
|
65
|
+
/** Zero-padded hours: 01–12 in 12-hour mode, 00–23 otherwise. */
|
|
62
66
|
const getHours = (hourMode: 12 | 24) => {
|
|
63
67
|
const hours = [];
|
|
64
68
|
for (let i = 0; i < hourMode; i++) {
|
|
@@ -71,6 +75,7 @@ const getHours = (hourMode: 12 | 24) => {
|
|
|
71
75
|
return hours;
|
|
72
76
|
};
|
|
73
77
|
|
|
78
|
+
/** Zero-padded minutes from 00 stepping by `interval`. */
|
|
74
79
|
const getMinutes = (interval: number) => {
|
|
75
80
|
const minutes = [];
|
|
76
81
|
for (let min = 0; min < 60; min += interval) {
|
|
@@ -82,6 +87,7 @@ const getMinutes = (interval: number) => {
|
|
|
82
87
|
|
|
83
88
|
const timeMode = ['AM', 'PM'];
|
|
84
89
|
|
|
90
|
+
/** Runs `func` once `delay` ms pass without a new call; `cancel()` drops it. */
|
|
85
91
|
function debounce<T extends (...args: any[]) => any>(func: T, delay: number) {
|
|
86
92
|
let timer: ReturnType<typeof setTimeout>;
|
|
87
93
|
|