|
| 1 | +import React, { useMemo, useRef, useState } from "react"; |
| 2 | +import { findNodeHandle, LogBox } from "react-native"; |
| 3 | +import Animated, { add } from "react-native-reanimated"; |
| 4 | +import DraggableFlatList, { DraggableFlatListProps } from "../index"; |
| 5 | +import { useNestableScrollContainerContext } from "../context/nestableScrollContainerContext"; |
| 6 | +import { useNestedAutoScroll } from "../hooks/useNestedAutoScroll"; |
| 7 | + |
| 8 | +export function NestableDraggableFlatList<T>(props: DraggableFlatListProps<T>) { |
| 9 | + const hasSuppressedWarnings = useRef(false); |
| 10 | + |
| 11 | + if (!hasSuppressedWarnings.current) { |
| 12 | + LogBox.ignoreLogs([ |
| 13 | + "VirtualizedLists should never be nested inside plain ScrollViews with the same orientation because it can break windowing", |
| 14 | + ]); // Ignore log notification by message |
| 15 | + //@ts-ignore |
| 16 | + console.reportErrorsAsExceptions = false; |
| 17 | + hasSuppressedWarnings.current = true; |
| 18 | + } |
| 19 | + |
| 20 | + const { |
| 21 | + containerRef, |
| 22 | + outerScrollOffset, |
| 23 | + setOuterScrollEnabled, |
| 24 | + } = useNestableScrollContainerContext(); |
| 25 | + |
| 26 | + const listVerticalOffset = useMemo(() => new Animated.Value<number>(0), []); |
| 27 | + const viewRef = useRef<Animated.View>(null); |
| 28 | + const [animVals, setAnimVals] = useState({}); |
| 29 | + |
| 30 | + useNestedAutoScroll(animVals); |
| 31 | + |
| 32 | + const onListContainerLayout = async () => { |
| 33 | + const viewNode = viewRef.current; |
| 34 | + const nodeHandle = findNodeHandle(containerRef.current); |
| 35 | + |
| 36 | + const onSuccess = (_x: number, y: number) => { |
| 37 | + listVerticalOffset.setValue(y); |
| 38 | + }; |
| 39 | + const onFail = () => { |
| 40 | + console.log("## nested draggable list measure fail"); |
| 41 | + }; |
| 42 | + //@ts-ignore |
| 43 | + viewNode.measureLayout(nodeHandle, onSuccess, onFail); |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <Animated.View ref={viewRef} onLayout={onListContainerLayout}> |
| 48 | + <DraggableFlatList |
| 49 | + activationDistance={20} |
| 50 | + autoscrollSpeed={50} |
| 51 | + scrollEnabled={false} |
| 52 | + {...props} |
| 53 | + outerScrollOffset={outerScrollOffset} |
| 54 | + onDragBegin={(...args) => { |
| 55 | + setOuterScrollEnabled(false); |
| 56 | + props.onDragBegin?.(...args); |
| 57 | + }} |
| 58 | + onDragEnd={(...args) => { |
| 59 | + props.onDragEnd?.(...args); |
| 60 | + setOuterScrollEnabled(true); |
| 61 | + }} |
| 62 | + onAnimValInit={(animVals) => { |
| 63 | + setAnimVals({ |
| 64 | + ...animVals, |
| 65 | + hoverAnim: add(animVals.hoverAnim, listVerticalOffset), |
| 66 | + }); |
| 67 | + props.onAnimValInit?.(animVals); |
| 68 | + }} |
| 69 | + /> |
| 70 | + </Animated.View> |
| 71 | + ); |
| 72 | +} |
0 commit comments