import React, { isValidElement, forwardRef, ForwardRefRenderFunction, useImperativeHandle, } from 'react'; import { View, StyleSheet, TouchableOpacity, StatusBar, Platform, } from 'react-native'; import { isBoolean, isFunction, isArray, isNil } from 'lodash'; import { renderTextLikeJSX, mergeProps } from '../../core/helpers'; import { STATUS_BAR_HEIGHT, NAVIGATION_HEIGHT } from '../../common/adapter'; import ShouldRender from '../ShouldRender'; import { IconMAReturn1, IconXMoreb1 } from '../../icons'; import createStyle from './styles/navbar.style'; import { useTheme } from '../Theme/Theme'; import { NavBarProps, NavBarRef, RightIcon } from './interface'; import { iosOrAndroidOrHarmony } from '../../utils'; import { useMaxLayoutWidth } from '../../core/hooks'; const defaultProps = { backArrow: true, translucent: false, }; const NavBar: ForwardRefRenderFunction = (p, ref) => { const { maxLayoutWidth, handleLayout } = useMaxLayoutWidth(); const props = mergeProps(defaultProps, p); const token = useTheme(); const styles = createStyle(token); const shouldEnableContentCenter = props.enableContentCenter ?? ((typeof props?.children === 'string' && props?.children?.length > 0) || (typeof props?.title === 'string' && props?.title?.length > 0)); // 处理状态栏高度 const getStatusBarHeight = () => { // 安卓状态栏高度为StatusBar.currentHeight // ios没有状态栏 return { height: iosOrAndroidOrHarmony( STATUS_BAR_HEIGHT, StatusBar.currentHeight, StatusBar.currentHeight ), }; }; // 处理导航栏高度 const getNavBarHeight = () => { // 安卓导航栏高度为56(react-navigation源码里有说明) // harmony和Android一致 // ios导航栏高度为44 const androidH = 56; const harmonyH = 56; const iosH = NAVIGATION_HEIGHT; return { height: iosOrAndroidOrHarmony(iosH, androidH, harmonyH), }; }; const onBack = () => { if (!isFunction(props.onBack)) { return; } props.onBack(); }; // 渲染状态栏 const renderStatusBar = ( ); // 自定义导航栏 const customNavBar = ( <> {/* 自定义导航栏 */} {props.customNavBar} ); // 是否有返回箭头 const hasBackArrow = isBoolean(props.backArrow) && props.backArrow; // 导航栏左边区域 const navBarLeftArea = ( 0 && { width: maxLayoutWidth }, props.leftStyle, ])} {...(!!shouldEnableContentCenter && { onLayout: handleLayout })} > {/* 返回区域的箭头 */} {/* 自定义返回区域 */} {props.backArrow} {/* 左侧内容,渲染在返回区域的右侧 */} {props.left} ); // 导航栏中间区域 const navBarCenterArea = ( {props?.children ? ( renderTextLikeJSX(props?.children, [styles.title, props.titleStyle], { numberOfLines: 1, }) ) : ( <> {renderTextLikeJSX(props?.title, [styles.title, props.titleStyle], { numberOfLines: 1, })} {renderTextLikeJSX( props?.description, [styles.description, props.descriptionStyle], { numberOfLines: 1, } )} )} ); // 导航栏右边区域 const rightList = isArray(props.right) && props.right.length > 2 ? [props.right[0], { icon: IconXMoreb1, onPress: () => {} }] : props.right; const navBarRightArea = ( <> {/* 右边区域 */} 0 && { width: maxLayoutWidth }, props.rightStyle, ])} {...(!!shouldEnableContentCenter && { onLayout: handleLayout })} > {renderTextLikeJSX(props?.right, [styles.rightTextStyle])} {rightList?.map?.((i: RightIcon, j: number) => { const IconComponent = i.icon; return ( { if (!isFunction(i.onPress)) { return; } i.onPress(); }} > ); })} ); // 渲染导航栏 const renderNavBar = ( {customNavBar} {/* 非自定义导航栏 */} {navBarLeftArea} {navBarCenterArea} {navBarRightArea} ); // 预留向外界暴露的对象 useImperativeHandle(ref, () => ({})); return ( {renderStatusBar} {renderNavBar} ); }; export default forwardRef(NavBar);