Skip to content

Commit fb9a81b

Browse files
authored
Fix/ WheelPicker onChange in Android (#1301)
* Trigger onChange from scrollToIndex for Android * call scrollToOffset from setTimeout
1 parent bc310c2 commit fb9a81b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/incubator/WheelPicker/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const WheelPicker = React.memo(({
9999
preferredNumVisibleRows: numberOfVisibleRows
100100
});
101101

102+
const prevIndex = useRef(currentIndex);
102103
const [scrollOffset, setScrollOffset] = useState(currentIndex * itemHeight);
103104

104105
useEffect(() => {
@@ -122,8 +123,14 @@ const WheelPicker = React.memo(({
122123
};
123124

124125
const scrollToIndex = (index: number, animated: boolean) => {
126+
// this is done to handle onMomentumScrollEnd not being called in Android:
127+
// https://github.com/facebook/react-native/issues/26661
128+
if (Constants.isAndroid && prevIndex.current !== index) {
129+
prevIndex.current = index;
130+
onChange?.(items?.[index]?.value, index);
131+
}
125132
//@ts-ignore for some reason scrollToOffset isn't recognized
126-
scrollView.current?.scrollToOffset({offset: index * itemHeight, animated});
133+
setTimeout(() => scrollView.current?.scrollToOffset({offset: index * itemHeight, animated}), 100);
127134
};
128135

129136
const selectItem = useCallback(index => {
@@ -181,7 +188,8 @@ const WheelPicker = React.memo(({
181188

182189
const getItemLayout = useCallback((_data, index: number) => {
183190
return {length: itemHeight, offset: itemHeight * index, index};
184-
}, [itemHeight]);
191+
},
192+
[itemHeight]);
185193

186194
const contentContainerStyle = useMemo(() => {
187195
return {paddingVertical: height / 2 - itemHeight / 2};

0 commit comments

Comments
 (0)