import * as React from 'react'; import { ScrollView, useWindowDimensions, View } from 'react-native'; import { ErrorCode, UIKitError } from '../../error'; import { useDelayExecTask } from '../../hook'; import { timeoutTask } from '../../utils'; import { gAnimatedDuration } from './TabPage.const'; import { calculateIndex } from './TabPage.hooks'; import type { TabPageBodyLISTContentProps, TabPageBodyLISTProps, } from './types'; export function TabPageBodyLIST( props: TabPageBodyLISTProps ) { const { style, childrenCount, RenderChildren, RenderChildrenProps, propsRef, height: initHeight, width: initWidth, containerStyle, initIndex = 0, onLayout: propsOnLayout, onCurrentIndex, onScroll: propsScroll, onMomentumScrollEnd: propsOnMomentumScrollEnd, enableCurrentIndex, scrollEnabled, ...others } = props; const ref = React.useRef>({} as any); const { width: winWidth } = useWindowDimensions(); const w = initWidth ?? winWidth; let viewRef = React.useRef | undefined>( {} as any ); const [currentIndex, setCurrentIndex] = React.useState(initIndex); if (propsRef.current) { propsRef.current.scrollTo = (index: number, animated?: boolean) => { ref.current?.scrollTo({ x: index * w, animated: animated }); timeoutTask(gAnimatedDuration, () => setCurrentIndex(index)); onCurrentIndex?.(index); }; } const { delayExecTask: _onCurrentIndex } = useDelayExecTask( 100, (index: number) => { timeoutTask(gAnimatedDuration, () => setCurrentIndex(index)); onCurrentIndex?.(index); } ); if (childrenCount !== RenderChildren.length) { throw new UIKitError({ code: ErrorCode.params, desc: 'TabPageBodyLIST: childrenCount !== RenderChildren.length, please check.', }); } // const TabPageBodyContentMemo = // React.memo(TabPageBodyContent); return ( { if (ref) { viewRef.current = ref; } }} > { if (propsOnLayout) { propsOnLayout(e); } if (initIndex > 0) { ref.current?.scrollTo({ x: initIndex * w, animated: false }); } }} onScroll={(e) => { propsScroll?.(e); }} onMomentumScrollEnd={(e) => { propsOnMomentumScrollEnd?.(e); const x = e.nativeEvent.contentOffset.x; _onCurrentIndex(calculateIndex({ width: w, contentOffsetX: x })); }} {...others} > {enableCurrentIndex === true ? ( ) : ( )} ); } export function TabPageBodyContent( props: TabPageBodyLISTContentProps ): React.ReactElement[] { const { RenderChildren, RenderChildrenProps, width, currentIndex } = props; // return ; return RenderChildren.map((RenderChild, i) => { return ( {currentIndex !== undefined ? ( ) : ( )} ); }); } export const TabPageBodyContentMemo = React.memo( TabPageBodyContent as any );