Skip to content

Commit

Permalink
fix scroll speed issues & always trigger onchange when first initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 4, 2022
1 parent ec09898 commit cfc14cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
40 changes: 13 additions & 27 deletions src/WheelPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// src/WheelPicker.tsx
import React, {
useCallback,
useRef,
useMemo,
useEffect,
useState,
} from 'react';
import { View, FlatList, Platform, Dimensions, ViewStyle } from 'react-native';
import React, { useCallback, useRef, useMemo, useEffect } from 'react';
import { View, FlatList, Dimensions, ViewStyle } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedScrollHandler,
Expand Down Expand Up @@ -39,7 +33,7 @@ function WheelPicker(props: IWheelPickerProps) {
offset.value = e.contentOffset.y;
});

const [currentIndex, setCurrentIndex] = useState(0);
// const [currentIndex, setCurrentIndex] = useState(0);

const {
items: propItems,
Expand All @@ -51,13 +45,14 @@ function WheelPicker(props: IWheelPickerProps) {
debug = false,
} = props;

const { items, defaultIndex, height } = usePresenter({
initialValue: value,
items: propItems,
valueAttribute,
labelAttribute,
numberOfVisibleRows,
});
const { items, defaultIndex, height, currentIndex, onValueChange } =
usePresenter({
initialValue: value,
items: propItems,
valueAttribute,
labelAttribute,
numberOfVisibleRows,
});

if (debug)
console.log('WheelPicker', {
Expand Down Expand Up @@ -124,14 +119,6 @@ function WheelPicker(props: IWheelPickerProps) {
[height]
);

const onValueChange = useCallback(
(event) =>
setCurrentIndex(
Math.floor(event.nativeEvent.contentOffset.y / itemHeight)
),
[]
);

useEffect(() => {
setTimeout(() => {
scrollToOffset(defaultIndex, true);
Expand Down Expand Up @@ -160,7 +147,6 @@ function WheelPicker(props: IWheelPickerProps) {
ref={scrollView}
height={height}
data={items}
// onLayout={getLayout}
showsVerticalScrollIndicator={false}
scrollEventThrottle={100}
initialNumToRender={8}
Expand All @@ -169,10 +155,10 @@ function WheelPicker(props: IWheelPickerProps) {
renderItem={renderItem}
snapToOffsets={snapToOffsets}
keyExtractor={(_, index) => `Item_${index}`}
// initialScrollIndex={10}
contentContainerStyle={contentContainerStyle}
snapToInterval={itemHeight}
decelerationRate={Platform.OS === 'ios' ? 'normal' : 0.98}
decelerationRate={'fast'}
// decelerationRate={Platform.OS === 'ios' ? 'normal' : 0.98}
onMomentumScrollEnd={onValueChange}
/>
{/* <LinearGradient
Expand Down
23 changes: 18 additions & 5 deletions src/usePresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// src/usePresenter.ts
import { useCallback, useState } from 'react';
import findIndex from 'lodash/findIndex';
import isUndefined from 'lodash/isUndefined';
import { itemHeight } from './constant';
Expand All @@ -18,24 +19,36 @@ export default function usePresenter({
labelAttribute,
numberOfVisibleRows,
}: IProps) {
// const value = isUndefined(selectedValue) ? initialValue : selectedValue;

const items = propItems.map((item: any) => {
return {
value: item[valueAttribute],
label: item[labelAttribute],
};
});

const defaultIndex = isUndefined(initialValue)
? 0
: findIndex(items, { value: initialValue });

const [currentIndex, setCurrentIndex] = useState(defaultIndex);

const onValueChange = useCallback(
(event) =>
setCurrentIndex(
Math.floor(event.nativeEvent.contentOffset.y / itemHeight)
),
[]
);

const getRowItemByIndex = (index: number) => {
return items[index];
};

return {
items,
defaultIndex: isUndefined(initialValue)
? 0
: findIndex(items, { value: initialValue }),
onValueChange,
currentIndex,
defaultIndex,
getRowItemByIndex,
height: numberOfVisibleRows * itemHeight,
};
Expand Down

0 comments on commit cfc14cd

Please sign in to comment.