Skip to content
Open
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
43 changes: 17 additions & 26 deletions src/WheelPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
import {
Animated,
FlatList,
FlatListProps,
StyleProp,
TextStyle,
NativeSyntheticEvent,
NativeScrollEvent,
Animated,
ViewStyle,
View,
ViewProps,
FlatListProps,
FlatList,
ViewStyle,
} from 'react-native';
import styles from './WheelPicker.styles';
import WheelPickerItem from './WheelPickerItem';
Expand Down Expand Up @@ -42,16 +40,16 @@ const WheelPicker: React.FC<Props> = ({
itemTextStyle = {},
itemHeight = 40,
scaleFunction = (x: number) => 1.0 ** x,
rotationFunction = (x: number) => 1 - Math.pow(1 / 2, x),
opacityFunction = (x: number) => Math.pow(1 / 3, x),
rotationFunction = (x: number) => 1 - (1 / 2) ** x,
opacityFunction = (x: number) => (1 / 3) ** x,
visibleRest = 2,
decelerationRate = 'fast',
containerProps = {},
flatListProps = {},
}) => {
const flatListRef = useRef<FlatList>(null);
const [scrollY] = useState(new Animated.Value(0));

const calculatedSelectingIndex = React.useRef<number>(selectedIndex);
const containerHeight = (1 + visibleRest * 2) * itemHeight;
const paddedOptions = useMemo(() => {
const array: (string | null)[] = [...options];
Expand All @@ -72,23 +70,16 @@ const WheelPicker: React.FC<Props> = ({
[visibleRest, scrollY, itemHeight],
);

const handleMomentumScrollEnd = (
event: NativeSyntheticEvent<NativeScrollEvent>,
) => {
// Due to list bounciness when scrolling to the start or the end of the list
// the offset might be negative or over the last item.
// We therefore clamp the offset to the supported range.
const offsetY = Math.min(
itemHeight * (options.length - 1),
Math.max(event.nativeEvent.contentOffset.y, 0),
currentScrollIndex.addListener(({ value }) => {
calculatedSelectingIndex.current = Math.min(
Math.max(0, Math.round(value) - visibleRest),
options.length - 1,
);
});

let index = Math.floor(Math.floor(offsetY) / itemHeight);
const last = Math.floor(offsetY % itemHeight);
if (last > itemHeight / 2) index++;

if (index !== selectedIndex) {
onChange(index);
const handleMomentumScrollEnd = () => {
if (calculatedSelectingIndex.current !== selectedIndex) {
onChange(calculatedSelectingIndex.current);
}
};

Expand Down Expand Up @@ -141,13 +132,13 @@ const WheelPicker: React.FC<Props> = ({
snapToOffsets={offsets}
decelerationRate={decelerationRate}
initialScrollIndex={selectedIndex}
getItemLayout={(data, index) => ({
getItemLayout={(_, index) => ({
length: itemHeight,
offset: itemHeight * index,
index,
})}
data={paddedOptions}
keyExtractor={(item, index) => index.toString()}
keyExtractor={(_, index) => index.toString()}
renderItem={({ item: option, index }) => (
<WheelPickerItem
key={`option-${index}`}
Expand Down