@klyftig/mobile-ui 1.3.1 → 1.3.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/package.json +1 -1
- package/src/components/WheelPicker.tsx +67 -39
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import React, { useEffect, useMemo, useRef } from 'react';
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
FlatList,
|
|
4
4
|
View,
|
|
5
5
|
Text,
|
|
6
6
|
StyleSheet,
|
|
7
7
|
NativeSyntheticEvent,
|
|
8
8
|
NativeScrollEvent,
|
|
9
|
+
ListRenderItem,
|
|
9
10
|
} from 'react-native';
|
|
10
11
|
import { useTheme } from '../hooks/useTheme';
|
|
11
12
|
|
|
@@ -34,7 +35,7 @@ export function WheelPicker({
|
|
|
34
35
|
flex,
|
|
35
36
|
}: WheelPickerProps) {
|
|
36
37
|
const { colors } = useTheme();
|
|
37
|
-
const
|
|
38
|
+
const listRef = useRef<FlatList<number>>(null);
|
|
38
39
|
const containerHeight = itemHeight * visibleCount;
|
|
39
40
|
const padding = (containerHeight - itemHeight) / 2;
|
|
40
41
|
const lastIndexRef = useRef<number>(-1);
|
|
@@ -45,22 +46,58 @@ export function WheelPicker({
|
|
|
45
46
|
}, [values, selected]);
|
|
46
47
|
|
|
47
48
|
useEffect(() => {
|
|
48
|
-
if (selectedIndex
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
49
|
+
if (selectedIndex === lastIndexRef.current) return;
|
|
50
|
+
const prev = lastIndexRef.current;
|
|
51
|
+
lastIndexRef.current = selectedIndex;
|
|
52
|
+
// Skip the very first effect — initialScrollIndex already positioned us.
|
|
53
|
+
if (prev === -1) return;
|
|
54
|
+
listRef.current?.scrollToOffset({ offset: selectedIndex * itemHeight, animated: false });
|
|
54
55
|
}, [selectedIndex, itemHeight]);
|
|
55
56
|
|
|
56
|
-
const handleMomentumEnd = (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
const handleMomentumEnd = useCallback(
|
|
58
|
+
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
59
|
+
const offsetY = e.nativeEvent.contentOffset.y;
|
|
60
|
+
const index = Math.round(offsetY / itemHeight);
|
|
61
|
+
const clamped = Math.max(0, Math.min(values.length - 1, index));
|
|
62
|
+
if (values[clamped] !== selected) {
|
|
63
|
+
onChange(values[clamped]);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
[itemHeight, values, selected, onChange],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const getItemLayout = useCallback(
|
|
70
|
+
(_: ArrayLike<number> | null | undefined, index: number) => ({
|
|
71
|
+
length: itemHeight,
|
|
72
|
+
offset: itemHeight * index,
|
|
73
|
+
index,
|
|
74
|
+
}),
|
|
75
|
+
[itemHeight],
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const keyExtractor = useCallback((item: number) => String(item), []);
|
|
79
|
+
|
|
80
|
+
const dimColor = colors.text.tertiary;
|
|
81
|
+
const activeColor = colors.text.primary;
|
|
82
|
+
|
|
83
|
+
const renderItem: ListRenderItem<number> = useCallback(
|
|
84
|
+
({ item, index }) => {
|
|
85
|
+
const isSelected = index === selectedIndex;
|
|
86
|
+
return (
|
|
87
|
+
<View style={[styles.item, { height: itemHeight }]}>
|
|
88
|
+
<Text
|
|
89
|
+
style={[
|
|
90
|
+
styles.itemText,
|
|
91
|
+
{ color: isSelected ? activeColor : dimColor },
|
|
92
|
+
]}
|
|
93
|
+
>
|
|
94
|
+
{formatLabel ? formatLabel(item) : String(item)}
|
|
95
|
+
</Text>
|
|
96
|
+
</View>
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
[selectedIndex, itemHeight, activeColor, dimColor, formatLabel],
|
|
100
|
+
);
|
|
64
101
|
|
|
65
102
|
return (
|
|
66
103
|
<View style={[styles.wrapper, { height: containerHeight, width, flex }]}>
|
|
@@ -71,34 +108,25 @@ export function WheelPicker({
|
|
|
71
108
|
{ top: padding, height: itemHeight, backgroundColor: 'rgba(0,0,0,0.06)' },
|
|
72
109
|
]}
|
|
73
110
|
/>
|
|
74
|
-
<
|
|
75
|
-
ref={
|
|
111
|
+
<FlatList
|
|
112
|
+
ref={listRef}
|
|
113
|
+
data={values}
|
|
114
|
+
keyExtractor={keyExtractor}
|
|
115
|
+
renderItem={renderItem}
|
|
116
|
+
getItemLayout={getItemLayout}
|
|
117
|
+
initialScrollIndex={selectedIndex}
|
|
118
|
+
extraData={selectedIndex}
|
|
76
119
|
showsVerticalScrollIndicator={false}
|
|
77
120
|
snapToInterval={itemHeight}
|
|
78
121
|
decelerationRate="fast"
|
|
79
122
|
contentContainerStyle={{ paddingTop: padding, paddingBottom: padding }}
|
|
80
123
|
onMomentumScrollEnd={handleMomentumEnd}
|
|
81
124
|
nestedScrollEnabled
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
{
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<View key={item} style={[styles.item, { height: itemHeight }]}>
|
|
88
|
-
<Text
|
|
89
|
-
style={[
|
|
90
|
-
styles.itemText,
|
|
91
|
-
{
|
|
92
|
-
color: isSelected ? colors.text.primary : colors.text.tertiary,
|
|
93
|
-
},
|
|
94
|
-
]}
|
|
95
|
-
>
|
|
96
|
-
{formatLabel ? formatLabel(item) : String(item)}
|
|
97
|
-
</Text>
|
|
98
|
-
</View>
|
|
99
|
-
);
|
|
100
|
-
})}
|
|
101
|
-
</ScrollView>
|
|
125
|
+
initialNumToRender={visibleCount + 4}
|
|
126
|
+
windowSize={3}
|
|
127
|
+
maxToRenderPerBatch={visibleCount + 4}
|
|
128
|
+
removeClippedSubviews
|
|
129
|
+
/>
|
|
102
130
|
</View>
|
|
103
131
|
);
|
|
104
132
|
}
|