Skip to content

Feat/Add Haptic to Incubator WheelPicker #1297

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

Closed
wants to merge 14 commits into from
Closed
26 changes: 24 additions & 2 deletions src/incubator/WheelPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
// TODO: Support style customization
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';
import Animated, {
useSharedValue,
useAnimatedScrollHandler,
useAnimatedReaction,
runOnJS
} from 'react-native-reanimated';
import {Colors, Spacings} from 'style';
import View from '../../components/view';
import Fader, {FaderPosition} from '../../components/fader';
import {Constants} from 'helpers';
import Item, {ItemProps} from './Item';
import usePresenter from './usePresenter';
import Text, {TextProps} from '../../components/text';
import {HapticService, HapticType} from 'services';

const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
const HAPTIC_RANGE = 10;

export interface WheelPickerProps {
/**
Expand Down Expand Up @@ -106,14 +113,29 @@ const WheelPicker = React.memo(({
preferredNumVisibleRows: numberOfVisibleRows
});

const animatedPrevIndex = useSharedValue(currentIndex);
const animatedCurIndex = useSharedValue(currentIndex);
const prevIndex = useRef(currentIndex);
const [scrollOffset, setScrollOffset] = useState(currentIndex * itemHeight);

useEffect(() => {
controlComponent();
});

const keyExtractor = useCallback((item: ItemProps, index: number) => `${item}.${index}`, []);
useAnimatedReaction(() => {
return offset.value;
},
() => {
const shouldUpdateIndex =
offset.value % itemHeight > itemHeight - HAPTIC_RANGE || offset.value % itemHeight < HAPTIC_RANGE;
animatedCurIndex.value = shouldUpdateIndex ? Math.round(offset.value / itemHeight) : animatedCurIndex.value;
if (animatedCurIndex.value !== animatedPrevIndex.value) {
animatedPrevIndex.value = animatedCurIndex.value;
runOnJS(HapticService.triggerHaptic)(HapticType.selection, 'WheelPicker');
}
});

const keyExtractor = useCallback((item: ItemProps) => `${item.value}`, []);

/**
* The picker is a controlled component. This means we expect the
Expand Down