@quidone/react-native-wheel-picker
Version:
Picker is a UI component for selecting an item from a list of options.
96 lines (95 loc) • 2.74 kB
JavaScript
"use strict";
import React, { forwardRef, memo, useImperativeHandle, useMemo, useRef } from 'react';
import { Animated, StyleSheet } from 'react-native';
import { useInit } from '@rozhkov/react-useful-hooks';
import { withScrollStartEndEvent } from '../../utils/scrolling';
import { jsx as _jsx } from "react/jsx-runtime";
const ExtendedAnimatedScrollView = withScrollStartEndEvent(Animated.ScrollView);
const OFFSET_X = 0;
const getOffsetY = (index, itemHeight) => index * itemHeight;
const List = ({
initialIndex,
data,
keyExtractor,
renderItem,
itemHeight,
pickerHeight,
readOnly,
scrollOffset,
onTouchEnd,
onTouchStart,
onTouchCancel,
onScrollStart,
onScrollEnd,
contentContainerStyle: contentContainerStyleProp,
disableIntervalMomentum,
...restProps
}, forwardedRef) => {
const listRef = useRef(null);
useImperativeHandle(forwardedRef, () => ({
scrollToIndex: ({
index,
animated
}) => {
listRef.current?.scrollTo({
x: OFFSET_X,
y: getOffsetY(index, itemHeight),
animated
});
}
}), [itemHeight]);
const initialOffset = useInit(() => ({
x: OFFSET_X,
y: getOffsetY(initialIndex, itemHeight)
}));
const snapToOffsets = useMemo(() => data.map((_, i) => i * itemHeight), [data, itemHeight]);
const onScroll = useMemo(() => Animated.event([{
nativeEvent: {
contentOffset: {
y: scrollOffset
}
}
}], {
useNativeDriver: true
}), [scrollOffset]);
const contentContainerStyle = useMemo(() => {
return [{
paddingVertical: (pickerHeight - itemHeight) / 2
}, contentContainerStyleProp];
}, [pickerHeight, itemHeight, contentContainerStyleProp]);
return /*#__PURE__*/_jsx(ExtendedAnimatedScrollView, {
showsHorizontalScrollIndicator: false,
showsVerticalScrollIndicator: false,
scrollEventThrottle: 16,
scrollEnabled: !readOnly,
...restProps,
ref: listRef,
contentOffset: initialOffset,
onScroll: onScroll,
scrollOffset: scrollOffset,
snapToOffsets: snapToOffsets,
disableIntervalMomentum: disableIntervalMomentum,
style: styles.list,
contentContainerStyle: contentContainerStyle,
onTouchStart: onTouchStart,
onTouchEnd: onTouchEnd,
onTouchCancel: onTouchCancel,
nestedScrollEnabled: true,
removeClippedSubviews: false,
onScrollStart: onScrollStart,
onScrollEnd: onScrollEnd,
children: data.map((item, index) => renderItem({
key: keyExtractor(item, index),
item,
index
}))
});
};
const styles = StyleSheet.create({
list: {
width: '100%',
overflow: 'visible'
}
});
export default /*#__PURE__*/memo(/*#__PURE__*/forwardRef(List));
//# sourceMappingURL=List.js.map