import Taro from '@tarojs/taro'; import React from 'react'; import SafeView from '../SafeView'; import { observer } from 'mobx-react'; import PropTypes from 'prop-types'; import { PageContext } from '../../../../utils/context'; import { Image, ImageBackground, Text, View } from '../../../..'; import { HeaderBarProps } from './HeaderBar.PropType'; import StatusBar from '../StatusBar'; import Icon from '../../../icon'; import { px } from '../../../../utils'; import config from '../../../../config'; // @ts-ignore import BackLight from './images/back_light.png'; // @ts-ignore import BackDark from './images/back_dark.png'; import { observable } from 'mobx'; const DarkBackIcon = ( ); const LightBackIcon = ( ); @observer class HeaderBar extends React.Component { static contextType = PageContext; static propTypes = { mode: PropTypes.oneOf(['dark', 'light']), title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), // titleTrans: PropTypes.oneOf(['left', 'center']), showBack: PropTypes.bool, translucent: PropTypes.bool, imageBackground: PropTypes.bool, leftContent: PropTypes.element, rightContent: PropTypes.element, bottomContentHeight: PropTypes.number, loginIcon: PropTypes.bool, onBack: PropTypes.func, }; componentDidMount(): void { // 判断是否可以返回,否则首页按钮 this.isCanBack = this.canBack(); this.canBackHome = !!config.get('homePath'); } @observable isCanBack = true; @observable canBackHome = false; onBackHome?: () => void; handleBack = () => { if (!this.canBack() && config.get('homePath')) { return Taro.reLaunch({ url: config.get('homePath') }); } if (this.props.onBack) { return this.props.onBack(); } else { Taro.navigateBack(); } }; canBack = () => { const pages = Taro.getCurrentPages(); return !!pages[pages.length - 2]; }; render() { const { mode = 'dark', backgroundColor, imageBackground, title, titleAlign, showBack = true, translucent, leftContent, rightContent, fixed, bottomContentHeight, capsulePadding, onLayout, } = this.props; const titleEle = title || this.props.children; const bgColor = backgroundColor || (mode === 'dark' ? '' : '#fff'); const Container: any = imageBackground ? ImageBackground : View; const headerHeight = px(88 + (bottomContentHeight || 0)); const isCanBack = this.isCanBack; const canBackHome = this.canBackHome; return ( {({}) => { const isWeapp = Taro.getEnv() !== Taro.ENV_TYPE.RN && Taro.getEnv() !== Taro.ENV_TYPE.WEB; return ( {isWeapp && capsulePadding === 'top' && } {showBack && ( {isCanBack ? mode === 'dark' ? DarkBackIcon : LightBackIcon : canBackHome && ( )} )} {leftContent === null ? ( showBack ? null : ( ) ) : ( leftContent )} {typeof titleEle === 'string' ? ( {titleEle} ) : ( titleEle )} {rightContent} {this.props.bottomContent} {/* fixed 模式占位 */} {fixed && ( {isWeapp && capsulePadding === 'top' && } )} ); }} ); } } export default HeaderBar;