@momo-kits/date-picker 0.110.1-beta.2 → 0.110.1-beta.4
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 +36 -53
- package/index.tsx +2 -4
- package/package.json +1 -1
package/WheelPicker.tsx
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import React, {FC, memo, useContext, useEffect,
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import React, {FC, memo, useContext, useEffect, useRef} from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Animated,
|
|
4
|
+
FlatList,
|
|
5
|
+
ListRenderItemInfo,
|
|
6
|
+
Platform,
|
|
7
|
+
View,
|
|
8
|
+
} from 'react-native';
|
|
4
9
|
import styles from './styles';
|
|
5
10
|
import {ApplicationContext, scaleSize} from '@momo-kits/foundation';
|
|
6
11
|
import {WheelPickerProps} from './types';
|
|
@@ -14,21 +19,23 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
14
19
|
onChange,
|
|
15
20
|
selectedData,
|
|
16
21
|
}) => {
|
|
22
|
+
const isAndroid = Platform.OS === 'android';
|
|
17
23
|
const {theme} = useContext(ApplicationContext);
|
|
18
24
|
const flatListRef = useRef<any>(null);
|
|
19
25
|
const scrollAnimatedValue = useRef(new Animated.Value(0)).current;
|
|
20
|
-
const active = useRef(0);
|
|
21
26
|
const scrollListener = useRef('0');
|
|
27
|
+
const active = useRef(0);
|
|
22
28
|
const itemSize = 42;
|
|
29
|
+
const initItemNum = 15;
|
|
23
30
|
|
|
24
31
|
useEffect(() => {
|
|
25
|
-
scrollListener.current &&
|
|
32
|
+
scrollListener.current && scrollAnimatedValue.removeAllListeners();
|
|
26
33
|
scrollListener.current = scrollAnimatedValue.addListener(
|
|
27
34
|
({value}) => (active.current = value)
|
|
28
35
|
);
|
|
29
36
|
|
|
30
37
|
return () => {
|
|
31
|
-
|
|
38
|
+
scrollAnimatedValue.removeAllListeners();
|
|
32
39
|
};
|
|
33
40
|
}, [scrollAnimatedValue]);
|
|
34
41
|
|
|
@@ -67,6 +74,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
67
74
|
if (selectedIndex > data.length - 3) {
|
|
68
75
|
selectedIndex = data.length - 3;
|
|
69
76
|
}
|
|
77
|
+
|
|
70
78
|
flatListRef.current?.scrollToIndex({
|
|
71
79
|
index: selectedIndex - 2,
|
|
72
80
|
animated: true,
|
|
@@ -74,55 +82,31 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
74
82
|
}, 100);
|
|
75
83
|
|
|
76
84
|
const onMomentumScrollEnd = () => {
|
|
77
|
-
let index = Math.round(active.current / itemSize);
|
|
85
|
+
let index = Math.round((active.current + itemSize / 2) / itemSize) - 1;
|
|
78
86
|
onChange?.(name, data[index + 2]);
|
|
79
87
|
};
|
|
80
88
|
|
|
81
89
|
const ItemComponent: FC<any> = React.memo(props => {
|
|
82
90
|
const {item, index} = props;
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return c;
|
|
96
|
-
}
|
|
97
|
-
}),
|
|
98
|
-
],
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const scaleAnimated = (a: number, b: number) => {
|
|
103
|
-
return {
|
|
104
|
-
inputRange: [...data.map((_: any, i: number) => i * itemSize)],
|
|
105
|
-
outputRange: [
|
|
106
|
-
...data.map((_: any, i: number) => {
|
|
107
|
-
const center = i + 2;
|
|
108
|
-
if (center === index) {
|
|
109
|
-
return a;
|
|
110
|
-
} else {
|
|
111
|
-
return b;
|
|
112
|
-
}
|
|
113
|
-
}),
|
|
114
|
-
],
|
|
115
|
-
};
|
|
116
|
-
};
|
|
91
|
+
const position = Animated.divide(scrollAnimatedValue, itemSize);
|
|
92
|
+
const distance = Animated.subtract(index, position);
|
|
93
|
+
const opacity = distance.interpolate({
|
|
94
|
+
inputRange: [0, 1, 2, 3, 4],
|
|
95
|
+
outputRange: [0.4, 0.8, 1, 0.8, 0.4],
|
|
96
|
+
extrapolate: 'clamp',
|
|
97
|
+
});
|
|
98
|
+
const scale = distance.interpolate({
|
|
99
|
+
inputRange: [0, 1, 2, 3, 4],
|
|
100
|
+
outputRange: [0.87, 0.87, 1, 0.87, 0.87],
|
|
101
|
+
extrapolate: 'clamp',
|
|
102
|
+
});
|
|
117
103
|
|
|
118
104
|
return (
|
|
119
105
|
<Animated.View
|
|
120
106
|
style={[
|
|
121
107
|
styles.wheelItem,
|
|
122
108
|
{
|
|
123
|
-
opacity
|
|
124
|
-
useMemo(() => opacityAnimated(1, 0.8, 0.4), [])
|
|
125
|
-
),
|
|
109
|
+
opacity,
|
|
126
110
|
},
|
|
127
111
|
]}>
|
|
128
112
|
<Animated.Text
|
|
@@ -130,9 +114,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
130
114
|
fontFamily: `${theme.font}-Semibold`,
|
|
131
115
|
transform: [
|
|
132
116
|
{
|
|
133
|
-
scale
|
|
134
|
-
useMemo(() => scaleAnimated(1, 0.87), [])
|
|
135
|
-
),
|
|
117
|
+
scale,
|
|
136
118
|
},
|
|
137
119
|
],
|
|
138
120
|
fontSize: scaleSize(16),
|
|
@@ -171,7 +153,6 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
171
153
|
|
|
172
154
|
return (
|
|
173
155
|
<View
|
|
174
|
-
key={`Wheel picker ${name}`}
|
|
175
156
|
style={[
|
|
176
157
|
style,
|
|
177
158
|
styles.wheelPicker,
|
|
@@ -181,11 +162,13 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
181
162
|
},
|
|
182
163
|
]}>
|
|
183
164
|
<AnimatedFlatList
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
165
|
+
removeClippedSubviews
|
|
166
|
+
windowSize={initItemNum}
|
|
167
|
+
initialNumToRender={initItemNum}
|
|
168
|
+
maxToRenderPerBatch={initItemNum}
|
|
188
169
|
snapToInterval={itemSize}
|
|
170
|
+
snapToOffsets={data.map((_, index) => index * itemSize)}
|
|
171
|
+
keyExtractor={(item, index) => `Wheel picker item ${item}-${index}`}
|
|
189
172
|
decelerationRate={'fast'}
|
|
190
173
|
getItemLayout={getItemLayout}
|
|
191
174
|
onScrollToIndexFailed={onScrollToIndexFailed}
|
|
@@ -196,8 +179,8 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
196
179
|
}
|
|
197
180
|
)}
|
|
198
181
|
onMomentumScrollEnd={onMomentumScrollEnd}
|
|
182
|
+
onScrollEndDrag={onMomentumScrollEnd}
|
|
199
183
|
ref={flatListRef}
|
|
200
|
-
keyExtractor={(item, index) => `Wheel picker item ${item}-${index}`}
|
|
201
184
|
showsVerticalScrollIndicator={false}
|
|
202
185
|
data={data}
|
|
203
186
|
renderItem={renderItem}
|
package/index.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, {FC, useEffect, useState} from 'react';
|
|
2
|
-
import {Dimensions, LayoutChangeEvent,
|
|
2
|
+
import {Dimensions, LayoutChangeEvent, View} from 'react-native';
|
|
3
3
|
import {scaleSize, Spacing, Text} from '@momo-kits/foundation';
|
|
4
4
|
import WheelPicker from './WheelPicker';
|
|
5
5
|
import {
|
|
@@ -43,7 +43,6 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
43
43
|
|
|
44
44
|
useEffect(() => {
|
|
45
45
|
setupData();
|
|
46
|
-
return () => {};
|
|
47
46
|
}, [selectedValue, currentDate]);
|
|
48
47
|
|
|
49
48
|
useEffect(() => {
|
|
@@ -143,14 +142,13 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
143
142
|
]}>
|
|
144
143
|
{data.map((dataItem, index) => {
|
|
145
144
|
return (
|
|
146
|
-
<View>
|
|
145
|
+
<View key={`${dataItem.name}_${index}`}>
|
|
147
146
|
{arrayLabelTime[index] && (
|
|
148
147
|
<View style={{alignItems: 'center', marginBottom: Spacing.S}}>
|
|
149
148
|
<Text typography="action_s_bold">{arrayLabelTime[index]}</Text>
|
|
150
149
|
</View>
|
|
151
150
|
)}
|
|
152
151
|
<WheelPicker
|
|
153
|
-
key={`${dataItem.name}_${index}`}
|
|
154
152
|
selectedData={String(currentDate[dataItem.name])}
|
|
155
153
|
onChange={onPickerChange}
|
|
156
154
|
data={[...paddingArray, ...dataItem.data, ...paddingArray]}
|