import React, { useState } from 'react'; import { LayoutChangeEvent, View, StyleProp, ViewStyle } from 'react-native'; import Animated, { useAnimatedRef, useAnimatedScrollHandler, useSharedValue } from 'react-native-reanimated'; import Item from './Item'; interface SortableListProps { items: Array; itemHeight: number; onOrderChange: (item: any) => void; renderItem: (item: any) => JSX.Element; dragableAreaSize?: number; style?: StyleProp; contentContainerStyle?: StyleProp; } const SortableList = ({ items, itemHeight, onOrderChange, renderItem, dragableAreaSize, style = {}, contentContainerStyle = {} }: SortableListProps) => { const [containerHeight, setContainerHeight] = useState(); const positions = useSharedValue({ ...items.map((item: any, index: number) => index) }); const scrollY = useSharedValue(0); const scrolViewRef = useAnimatedRef(); const contentHeight = items.length * itemHeight; const onScroll = useAnimatedScrollHandler({ onScroll: ({ contentOffset: { y } }) => scrollY.value = y }); const onLayout = (event: LayoutChangeEvent) => { setContainerHeight(event.nativeEvent.layout.height); }; const sortList = (positions: any) => { const sortedList = new Array(items.length).fill(null); for (let i = 0; i < items.length; ++i) { const sortedIndex = positions[i]; sortedList[sortedIndex] = items[i]; } onOrderChange(sortedList); }; return ( {items.map((item: any, key: number) => { return ( ) })} ); }; export default SortableList;