@momo-kits/date-picker 0.80.9 → 0.81.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/WheelPicker.tsx +31 -12
- package/package.json +1 -1
package/WheelPicker.tsx
CHANGED
|
@@ -36,10 +36,29 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
36
36
|
return () => {
|
|
37
37
|
debouncedScrollEnd.cancel();
|
|
38
38
|
};
|
|
39
|
-
}, []);
|
|
39
|
+
}, [data.length]);
|
|
40
|
+
|
|
41
|
+
const findClosestIndex = (array: string[], value: string) => {
|
|
42
|
+
let closest = array.reduce((a, b) => {
|
|
43
|
+
let aDiff = Math.abs(Number(a) - Number(value));
|
|
44
|
+
let bDiff = Math.abs(Number(b) - Number(value));
|
|
45
|
+
|
|
46
|
+
if (aDiff == bDiff) {
|
|
47
|
+
return a < b ? a : b;
|
|
48
|
+
} else {
|
|
49
|
+
return bDiff < aDiff ? b : a;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return array.indexOf(closest);
|
|
54
|
+
};
|
|
40
55
|
|
|
41
56
|
const debouncedScrollEnd = debounce(() => {
|
|
42
|
-
let selectedIndex = data.
|
|
57
|
+
let selectedIndex = data.indexOf(selectedData);
|
|
58
|
+
|
|
59
|
+
if (selectedIndex === -1) {
|
|
60
|
+
selectedIndex = findClosestIndex(data, selectedData);
|
|
61
|
+
}
|
|
43
62
|
|
|
44
63
|
if (selectedIndex < 2) {
|
|
45
64
|
selectedIndex = 2;
|
|
@@ -47,14 +66,17 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
47
66
|
if (selectedIndex > data.length - 3) {
|
|
48
67
|
selectedIndex = data.length - 3;
|
|
49
68
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
});
|
|
55
|
-
}
|
|
69
|
+
flatListRef.current?.scrollToIndex({
|
|
70
|
+
index: selectedIndex - 2,
|
|
71
|
+
animated: true,
|
|
72
|
+
});
|
|
56
73
|
}, 100);
|
|
57
74
|
|
|
75
|
+
const onMomentumScrollEnd = () => {
|
|
76
|
+
let index = Math.round(active.current / itemSize);
|
|
77
|
+
onChange?.(name, data[index + 2]);
|
|
78
|
+
};
|
|
79
|
+
|
|
58
80
|
const ItemComponent: FC<any> = React.memo(props => {
|
|
59
81
|
const {item, opacity, scale} = props;
|
|
60
82
|
|
|
@@ -170,10 +192,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
170
192
|
useNativeDriver: true,
|
|
171
193
|
},
|
|
172
194
|
)}
|
|
173
|
-
onMomentumScrollEnd={
|
|
174
|
-
const index = Math.round(active.current / itemSize);
|
|
175
|
-
onChange?.(name, data[index + 2]);
|
|
176
|
-
}}
|
|
195
|
+
onMomentumScrollEnd={onMomentumScrollEnd}
|
|
177
196
|
ref={flatListRef}
|
|
178
197
|
keyExtractor={(item, index) => `Wheel picker item ${item}-${index}`}
|
|
179
198
|
showsVerticalScrollIndicator={false}
|