diff --git a/packages/core/src/platform/patch/getDefaultOptions.ios.js b/packages/core/src/platform/patch/getDefaultOptions.ios.js index 5d703cef37..9c996851d3 100644 --- a/packages/core/src/platform/patch/getDefaultOptions.ios.js +++ b/packages/core/src/platform/patch/getDefaultOptions.ios.js @@ -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) diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-web-view.tsx b/packages/webpack-plugin/lib/runtime/components/react/mpx-web-view.tsx index ff86251681..c77a4e5513 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-web-view.tsx +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-web-view.tsx @@ -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' @@ -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 +// 固定组件的 props 类型,兼容不同版本的泛型默认值,JSX 中保持普通标签写法。 +const WebView: new (props: NativeWebViewProps) => WebViewInstance = NativeWebView type Listener = (type: string, callback: (e: Event) => void) => () => void interface PayloadData { @@ -77,8 +81,8 @@ const styles = StyleSheet.create({ } }) -const _WebView = forwardRef, WebViewProps>((props, ref): JSX.Element | null => { - const { src, bindmessage, bindload, binderror } = props +const _WebView = forwardRef, 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': { @@ -97,8 +101,9 @@ const _WebView = forwardRef, WebViewProps>((pr } const { pageId } = useContext(RouteContext) || {} const [pageLoadErr, setPageLoadErr] = useState(false) + const showNavRef = useRef() const currentPage = useMemo(() => getCurrentPage(pageId), [pageId]) - const webViewRef = useRef(null) + const webViewRef = useRef(null) const fristLoaded = useRef(false) const isLoadError = useRef(false) const isNavigateBack = useRef(false) @@ -123,7 +128,7 @@ const _WebView = forwardRef, WebViewProps>((pr isNavigateBack.current = false }) - useNodesRef(props, ref, webViewRef, { + useNodesRef(props, ref, webViewRef, { style: defaultWebViewStyle }) @@ -147,6 +152,18 @@ const _WebView = forwardRef, 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 } @@ -225,6 +242,11 @@ const _WebView = forwardRef, 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()