import React, { FunctionComponent } from 'react'; import Icon from '../icon/index'; import { TouchableOpacity, View, Text, StyleSheet } from 'react-native'; import { IComponent, ComponentDefaults } from '../utils/typings'; import statusBarHeight from '../utils/statusBarHeight'; import pt from '../utils/pt'; export interface NavBarProps extends IComponent { leftShow: boolean; title: string; titIcon: string; leftText: string; desc: string; className: string; fixed: boolean; safeAreaInsetTop: boolean; border: boolean; placeholder: boolean; zIndex: number | string; style: any; onClickTitle: (e: any) => void; onClickIcon: (e: any) => void; onClickBack: (e: any) => void; onClickRight: (e: any) => void; children?: React.ReactNode; } const defaultProps = { ...ComponentDefaults, title: '', desc: '', leftShow: true, titIcon: '', className: '', leftText: '', fixed: false, safeAreaInsetTop: false, border: false, placeholder: false, zIndex: 10, style: {}, onClickTitle: () => {}, onClickIcon: () => {}, onClickBack: () => {}, onClickRight: () => {}, } as NavBarProps; export const NavBar: FunctionComponent> = (props) => { const { desc, title, titIcon, leftShow, className, style, leftText, fixed, safeAreaInsetTop, border, placeholder, zIndex, onClickTitle, onClickIcon, onClickBack, onClickRight, iconClassPrefix, iconFontClassName, } = { ...defaultProps, ...props, }; const children = Array.isArray(props.children) ? props.children : [props.children]; const slot = children.reduce((slot: any, item: React.ReactElement) => { const data = slot; if (item && item.props) { data[item.props.slot] = item; } return data; }, {}); const customStyles = () => { return { ...style, zIndex, }; }; const renderLeft = () => { return ( onClickBack(e)}> {leftShow && ( )} {leftText ? ( {leftText} ) : null} {slot.left} ); }; const renderContent = () => { return ( {title ? ( onClickTitle(e)}> {title} ) : null} {titIcon ? ( onClickIcon(e)} style={mStyles.marL10} > ) : null} {slot.content} ); }; const renderRight = () => { return ( onClickRight(e)} > {desc ? ( {desc} ) : null} {slot.right} ); }; const renderWrapper = () => { return ( {renderLeft()} {renderContent()} {renderRight()} ); }; return ( <> {safeAreaInsetTop ? ( {renderWrapper()} ) : ( renderWrapper() )} ); }; const mStyles = StyleSheet.create({ wrapper: { height: pt(100), backgroundColor: '#fff', flexDirection: 'row', alignItems: 'center', justifyContent: 'center', borderBottomColor: '#f2f2f2', }, content: { justifyContent: 'center', alignItems: 'center', flexDirection: 'row', height: '100%', }, mRright: { height: '100%', justifyContent: 'center', alignItems: 'center', flexDirection: 'row', position: 'absolute', right: 0, top: 0, }, mLeft: { height: '100%', justifyContent: 'center', alignItems: 'center', flexDirection: 'row', position: 'absolute', left: 10, top: 0, }, marL10: { marginLeft: pt(10), }, }); NavBar.defaultProps = defaultProps; NavBar.displayName = 'NutNavBar';