@quidone/react-native-wheel-picker
Version:
Picker is a UI component for selecting an item from a list of options.
80 lines (79 loc) • 2.21 kB
JavaScript
;
import React, { memo, useMemo } from 'react';
import { Animated, TouchableWithoutFeedback } from 'react-native';
import { useScrollContentOffset } from '../contexts/ScrollContentOffsetContext';
import { usePickerItemHeight } from '../contexts/PickerItemHeightContext';
import { jsx as _jsx } from "react/jsx-runtime";
const PickerItemContainer = ({
listRef,
index,
item,
faces,
renderItem,
itemTextStyle,
enableScrollByTapOnItem,
readOnly
}) => {
const offset = useScrollContentOffset();
const height = usePickerItemHeight();
const {
opacity,
rotateX,
translateY
} = useMemo(() => {
const inputRange = faces.map(f => height * (index + f.index));
return {
opacity: offset.interpolate({
inputRange: inputRange,
outputRange: faces.map(x => x.opacity),
extrapolate: 'clamp'
}),
rotateX: offset.interpolate({
inputRange: inputRange,
outputRange: faces.map(x => `${x.deg}deg`),
extrapolate: 'extend'
}),
translateY: offset.interpolate({
inputRange: inputRange,
outputRange: faces.map(x => x.offsetY),
extrapolate: 'extend'
})
};
}, [faces, height, index, offset]);
const renderAnimatedView = () => {
return /*#__PURE__*/_jsx(Animated.View, {
style: [{
height,
opacity,
transform: [{
translateY
},
// first translateY, then rotateX for correct transformation.
{
rotateX
}, {
perspective: 1000
} // without this line this Animation will not render on Android https://reactnative.dev/docs/animations#bear-in-mind
]
}],
children: renderItem({
item,
index,
itemTextStyle
})
});
};
if (!enableScrollByTapOnItem || readOnly) {
return renderAnimatedView();
}
const scrollToItem = () => listRef.current?.scrollToIndex({
index,
animated: true
});
return /*#__PURE__*/_jsx(TouchableWithoutFeedback, {
onPress: scrollToItem,
children: renderAnimatedView()
});
};
export default /*#__PURE__*/memo(PickerItemContainer);
//# sourceMappingURL=PickerItemContainer.js.map