import * as React from 'react' ; import { camelCase } from 'lodash' ; import { getTabVisible , getTabBarPages } from './utils/index' ; import { navigationRef } from './rootNavigation' ; interface PageProps { navigation : any ; currentPath : string ; pageConfig : any ; children : React . ReactNode ; } const globalAny = global ; export class PageProvider extends React . Component < any > { constructor ( props ) { super ( props ) ; if ( navigationRef && navigationRef ?. current ) { navigationRef . current . setOptions = this . props . navigation . setOptions ; } this . handleConfig ( ) ; } componentDidMount ( ) { const { navigation } = this . props ; this . unSubscribleFocus = this . props . navigation . addListener ( 'focus' , ( ) => { if ( navigationRef && navigationRef ?. current ) { navigationRef . current . setOptions = navigation . setOptions ; } if ( this . isTabBarPage ( ) ) { const tabBarVisible = getTabVisible ( ) ; navigation . setOptions ( { tabBarVisible : tabBarVisible } ) ; } } ) ; } componentWillUnmount ( ) { if ( this . unSubscribleFocus ) { this . unSubscribleFocus ( ) ; } } handleConfig ( ) { const { navigation , pageConfig } = this . props ; if ( navigation && navigation . setOptions ) { const config = globalAny . __taroAppConfig ?. appConfig || { } ; const winOptions = config . window || { } ; const winRnOptions = config . rn || { } ; const winScreenOptions = this . isTabBarPage ( ) ? { } : ( winRnOptions ?. screenOptions || { } ) ; const { title = '' , headerTintColor = '' , headerStyle = { } , headerShown = true } = winScreenOptions ; const winRnTitle = this . isTabBarPage ( ) ? winRnOptions ?. options ?. title || '' : title ; const headerTitle = pageConfig . navigationBarTitleText || winRnTitle || winOptions ?. navigationBarTitleText || '' ; const color = pageConfig . navigationBarTextStyle || headerTintColor || winOptions ?. navigationBarTextStyle || 'white' ; const bgColor = pageConfig . navigationBarBackgroundColor || headerStyle ?. backgroundColor || winOptions ?. navigationBarBackgroundColor || '#000000' ; let showHeader = headerShown ; if ( winOptions . navigationStyle ) { showHeader = winOptions . navigationStyle !== 'custom' ; } if ( pageConfig . navigationStyle ) { showHeader = pageConfig . navigationStyle !== 'custom' ; } const rnConfig = pageConfig ?. rn || { } ; const screenOptions = rnConfig . screenOptions || { } ; const screenHeaderStyle = screenOptions ?. headerStyle || { } ; screenOptions . headerStyle = Object . assign ( { } , { backgroundColor : bgColor , shadowOffset : { width : 0 , height : 0 } , borderWidth : 0 , elevation : 0 , shadowOpacity : 1 , borderBottomWidth : 0 } , screenHeaderStyle ) ; const navBarParams = Object . assign ( winScreenOptions , { title : headerTitle , headerShown : showHeader , headerTintColor : color } , screenOptions ) ; if ( pageConfig ) { if ( this . isTabBarPage ( ) ) { navigation . setParams ( { navigateConfig : navBarParams } ) ; } else { navigation . setOptions ( navBarParams ) ; } } } } isTabBarPage ( ) { const { currentPath = '' } = this . props ; const tabPages = getTabBarPages ( ) ; return ! ! ( ( tabPages . length > 0 && tabPages . indexOf ( camelCase ( currentPath ) ) !== - 1 ) ) ; } private unSubscribleFocus ; render ( ) { return this . props . children ; } }