import {TabView, TabViewProps, Route, TabBar} from 'react-native-tab-view'; import React, {forwardRef, useEffect, useImperativeHandle, useRef} from 'react'; import { GestureContainer, CollapsibleHeaderProps, GestureContainerRef, } from 'react-native-head-tab-view-flashlist-support'; import {DeviceEventEmitter} from 'react-native'; import { Events, IgnoreScrollEnableType, } from 'react-native-head-tab-view-flashlist-support/enum'; type ZTabViewProps = Partial> & Pick, 'onIndexChange' | 'navigationState' | 'renderScene'> & CollapsibleHeaderProps; type ForwardTabViewProps = ZTabViewProps & { forwardedRef: React.Ref>; Component: typeof TabView; }; export default function createHeaderTabsComponent( Component: typeof TabView, config?: {}, ): React.ForwardRefExoticComponent< React.PropsWithoutRef> & React.RefAttributes> > { return React.forwardRef((props: ZTabViewProps, ref) => { const {componentId} = props ?? {}; useImperativeHandle(ref, () => { return { scrollToTop: (params) => { DeviceEventEmitter.emit(Events.LIST_SCROLL_TO_TOP, { componentId, ...params, }); }, scrollToTabBar: (params) => { DeviceEventEmitter.emit(Events.LIST_SCROLL_DOWN_TO_TAB_BAR, { componentId, ...params, }); }, scrollToOffset: (params) => { DeviceEventEmitter.emit(Events.LIST_SCROLL_TO_OFFSET, { componentId, ...params, }); }, }; }); return ( ); }); } function CollapsibleHeaderTabView( props: ForwardTabViewProps, ): any { const mRef = useRef(); const initialPageRef = useRef(props.navigationState.index); const {animationEnabled} = props ?? {}; useEffect(() => { mRef.current && mRef.current.setCurrentIndex(props.navigationState.index); }, [props.navigationState.index]); const _renderTabBar = (tabbarProps: any) => { if (props.renderTabBar) { return props.renderTabBar(tabbarProps); } return ; }; const emitIgnoreScrollEnableEvent = (eventType: String) => { DeviceEventEmitter.emit(Events.IGNORE_SCROLL_ENABLE, {type: eventType}); }; const renderTabView = (e: {renderTabBarContainer: any}) => { const {Component} = props; return ( { return e.renderTabBarContainer( _renderTabBar({ ...tabbarProps, onTabPress: () => { tabbarProps?.onTabPress?.(); if (animationEnabled !== false) { emitIgnoreScrollEnableEvent( IgnoreScrollEnableType.ON_TAB_PRESSED, ); } }, }), ); }} onSwipeStart={() => { props?.onSwipeStart?.(); emitIgnoreScrollEnableEvent(IgnoreScrollEnableType.ON_SWIPE_START); }} onSwipeEnd={() => { props?.onSwipeEnd?.(); emitIgnoreScrollEnableEvent(IgnoreScrollEnableType.ON_SWIPE_END); }} /> ); }; return ( ); }