11import React , { useState , useEffect , useRef , ReactNode } from 'react' ;
2- import { TabView as TabViewOriginal , Route } from 'react-native-tab-view' ;
3- import { View , Dimensions , StyleProp , ViewStyle } from 'react-native' ;
4- import type { SceneRendererProps } from 'react-native-tab-view' ;
5-
6- const initialLayout = { width : Dimensions . get ( 'window' ) . width } ;
2+ import {
3+ TabView as TabViewOriginal ,
4+ Route ,
5+ TabViewProps as TabViewPropsOriginal ,
6+ } from 'react-native-tab-view' ;
7+ import { View , useWindowDimensions } from 'react-native' ;
78
89interface ScenesMap {
910 [ key : string ] : ReactNode ;
1011}
1112
1213export type TabViewProps = {
1314 enableOptimizations ?: boolean ;
14- onIndexChange ?: ( index : number ) => void ;
15- renderTabBar ?: ( props : SceneRendererProps ) => React . ReactNode ;
16- tabBarPosition ?: 'top' | 'bottom' ;
17- initialLayout ?: {
18- width ?: number ;
19- height ?: number ;
20- } ;
21- lazy ?: boolean ;
22- lazyPreloadDistance ?: number ;
23- removeClippedSubviews ?: boolean ;
24- sceneContainerStyle ?: StyleProp < ViewStyle > ;
25- style ?: StyleProp < ViewStyle > ;
26- children ?: ReactNode ;
27- } ;
15+ children : ReactNode ;
16+ } & Omit <
17+ Partial < TabViewPropsOriginal < Route > > ,
18+ 'navigationState' | 'renderScene' | 'onIndexChange'
19+ > ;
2820
2921export const TabView = ( props : TabViewProps ) => {
3022 const [ index , setIndex ] = useState ( 0 ) ;
3123 const [ routes , setRoutes ] = useState < Route [ ] > ( [ ] ) ;
3224 const scenes = useRef < ScenesMap > ( { } ) ;
3325
26+ const layout = useWindowDimensions ( ) ;
27+
3428 useEffect ( ( ) => {
3529 if ( props . children ) {
3630 let newRoutes : Route [ ] = [ ] ;
@@ -55,7 +49,7 @@ export const TabView = (props: TabViewProps) => {
5549 }
5650 } , [ props . children ] ) ;
5751
58- const renderScene = ( { route } : { route : any } ) => {
52+ const renderScene = ( { route } : { route : Route } ) => {
5953 if (
6054 props . enableOptimizations &&
6155 Math . abs ( index - routes . findIndex ( ( item ) => item . key === route . key ) ) > 2
@@ -69,15 +63,19 @@ export const TabView = (props: TabViewProps) => {
6963 return null ;
7064 } ;
7165
72- return (
73- < TabViewOriginal
74- { ...props }
75- navigationState = { { index, routes } }
76- renderScene = { renderScene }
77- onIndexChange = { setIndex }
78- initialLayout = { initialLayout }
79- />
80- ) ;
66+ if ( routes . length ) {
67+ return (
68+ // @ts -ignore
69+ < TabViewOriginal
70+ { ...props }
71+ navigationState = { { index, routes } }
72+ renderScene = { renderScene }
73+ onIndexChange = { setIndex }
74+ initialLayout = { props . initialLayout || { width : layout . width } }
75+ />
76+ ) ;
77+ }
78+ return null ;
8179} ;
8280
8381export type TabProps = {
@@ -91,5 +89,5 @@ export type TabProps = {
9189} ;
9290
9391export const Tab = ( props : TabProps ) => {
94- return < React . Fragment > { props . children } </ React . Fragment > ;
92+ return < > { props . children } </ > ;
9593} ;
0 commit comments