Skip to content

WheelPicker - fix warning about getNode #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/incubator/WheelPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TODO: Support style customization
import {isFunction} from 'lodash';
import React, {useCallback, useRef, useMemo, useEffect, useState} from 'react';
import {TextStyle, ViewStyle, FlatList, NativeSyntheticEvent, NativeScrollEvent, StyleSheet} from 'react-native';
import Animated, {useSharedValue, useAnimatedScrollHandler} from 'react-native-reanimated';
Expand Down Expand Up @@ -131,15 +132,26 @@ const WheelPicker = React.memo(({
scrollToIndex(currentIndex, false);
}, []);

const scrollToOffset = (index: number, animated: boolean) => {
// TODO: we should remove this split (the getNode section) in V6 and remove support for reanimated 1
//@ts-expect-error for some reason scrollToOffset isn't recognized
if (isFunction(scrollView.current?.scrollToOffset)) {
//@ts-expect-error
scrollView.current?.scrollToOffset({offset: index * itemHeight, animated});
} else {
//@ts-expect-error
scrollView.current?.getNode()?.scrollToOffset({offset: index * itemHeight, animated});
}
};

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

const selectItem = useCallback(index => {
Expand Down