import * as React from 'react'; import { ScrollView, useWindowDimensions, View } from 'react-native'; import { useDelayExecTask } from '../../hook'; import { calculateIndex } from './TabPage.hooks'; import type { TabPageBodyProps } from './types'; export function TabPageBody(props: TabPageBodyProps) { const { style, children, propsRef, height: initHeight, width: initWidth, containerStyle, initIndex = 0, onLayout: propsOnLayout, onCurrentIndex, onScroll: propsScroll, onMomentumScrollEnd: propsOnMomentumScrollEnd, ...others } = props; const ref = React.useRef>({} as any); const { width: winWidth } = useWindowDimensions(); const w = initWidth ?? winWidth; let viewRef = React.useRef | undefined>( {} as any ); if (propsRef.current) { propsRef.current.scrollTo = (index: number, animated?: boolean) => { ref.current?.scrollTo({ x: index * w, animated: animated }); onCurrentIndex?.(index); }; } const { delayExecTask: _onCurrentIndex } = useDelayExecTask( 100, (index: number) => { onCurrentIndex?.(index); } ); 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} > {children.map((Body, i) => { return ( {Body} ); })} ); }