Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/src/platform/patch/getDefaultOptions.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ export function PageWrapperHOC (WrappedComponent, pageConfig = {}) {
error('Using pageWrapper requires passing navigation and route')
return null
}
navigation.setWebViewShowNav = (showNav) => {
navigation.setPageConfig({ navigationStyle: showNav ? undefined : 'custom' })
}
const headerHeight = useInnerHeaderHeight(currentPageConfig)
navigation.layout = getLayoutData(headerHeight)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { forwardRef, useRef, useContext, useMemo, useState } from 'react'
import { forwardRef, useRef, useContext, useMemo, useState, useLayoutEffect } from 'react'
import { warn, isFunction } from '@mpxjs/utils'
import Portal from './mpx-portal/index'
import { usePreventRemove, PreventRemoveEvent } from '@react-navigation/native'
import { getCustomEvent } from './getInnerListeners'
import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
import { WebView } from 'react-native-webview'
import { WebView as NativeWebView, WebViewProps as NativeWebViewProps } from 'react-native-webview'
import useNodesRef, { HandlerRef } from './useNodesRef'
import { getCurrentPage, useNavigation } from './utils'
import { WebViewHttpErrorEvent, WebViewEvent, WebViewMessageEvent, WebViewNavigation, WebViewProgressEvent } from 'react-native-webview/lib/WebViewTypes'
Expand All @@ -25,11 +25,15 @@ type CommonCallbackEvent = {

interface WebViewProps {
src?: string
'show-navigation-bar'?: boolean
bindmessage?: (event: OnMessageCallbackEvent) => void
bindload?: (event: CommonCallbackEvent) => void
binderror?: (event: CommonCallbackEvent) => void
[x: string]: any
}
type WebViewInstance = NativeWebView<unknown>
// 固定组件的 props 类型,兼容不同版本的泛型默认值,JSX 中保持普通标签写法。
const WebView: new (props: NativeWebViewProps) => WebViewInstance = NativeWebView
type Listener = (type: string, callback: (e: Event) => void) => () => void

interface PayloadData {
Expand Down Expand Up @@ -77,8 +81,8 @@ const styles = StyleSheet.create({
}
})

const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element | null => {
const { src, bindmessage, bindload, binderror } = props
const _WebView = forwardRef<HandlerRef<WebViewInstance, WebViewProps>, WebViewProps>((props, ref): JSX.Element | null => {
const { src, 'show-navigation-bar': showNavigationBar, bindmessage, bindload, binderror } = props
const mpx = global.__mpx
const errorText: ErrorTextMap = {
'zh-CN': {
Expand All @@ -97,8 +101,9 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
}
const { pageId } = useContext(RouteContext) || {}
const [pageLoadErr, setPageLoadErr] = useState<boolean>(false)
const showNavRef = useRef<boolean>()
const currentPage = useMemo(() => getCurrentPage(pageId), [pageId])
const webViewRef = useRef<WebView>(null)
const webViewRef = useRef<WebViewInstance>(null)
const fristLoaded = useRef<boolean>(false)
const isLoadError = useRef<boolean>(false)
const isNavigateBack = useRef<boolean>(false)
Expand All @@ -123,7 +128,7 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
isNavigateBack.current = false
})

useNodesRef<WebView, WebViewProps>(props, ref, webViewRef, {
useNodesRef<WebViewInstance, WebViewProps>(props, ref, webViewRef, {
style: defaultWebViewStyle
})

Expand All @@ -147,6 +152,18 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
}
}

const setShowNav = (showNav: boolean | undefined) => {
if (showNav === undefined || showNavRef.current === showNav) return
const applyShowNav = navigation?.setWebViewShowNav
if (!applyShowNav) return
applyShowNav(showNav)
showNavRef.current = showNav
}

useLayoutEffect(() => {
setShowNav(showNavigationBar)
}, [showNavigationBar])

if (!src) {
return null
}
Expand Down Expand Up @@ -225,6 +242,11 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
const params = Array.isArray(args) ? args : [postData]
const type = data.type
switch (type) {
case 'hideNavigationBar':
case 'showNavigationBar':
setShowNav(type === 'showNavigationBar')
asyncCallback = Promise.resolve({ errMsg: `${type}:ok` })
break
case 'setTitle':
{ // case下不允许直接声明,包个块解决该问题
const title = postData._documentTitle?.trim()
Expand Down
Loading