navbar.taro.tsx 3.2 KB
Newer Older
X
xiaoyatong 已提交
1 2
import React, { FunctionComponent } from 'react'
import classNames from 'classnames'
O
oasis-cloud 已提交
3
import Icon from '@/packages/icon/index.taro'
X
xiaoyatong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
import bem from '@/utils/bem'

export interface NavBarProps {
  leftShow: boolean
  title: string
  titIcon: string
  leftText: string
  desc: string
  className: string
  fixed: boolean
  safeAreaInsetTop: boolean
  border: boolean
  placeholder: boolean
  zIndex: number | string
  style: React.CSSProperties
  onClickTitle: (e: React.MouseEvent<HTMLDivElement>) => void
  onClickIcon: (e: React.MouseEvent<HTMLDivElement>) => void
  onClickBack: (e: React.MouseEvent<HTMLElement>) => void
  onClickRight: (e: React.MouseEvent<HTMLDivElement>) => void
  children?: React.ReactNode
}
const defaultProps = {
  title: '',
  desc: '',
  leftShow: true,
  titIcon: '',
  className: '',
  leftText: '',
  fixed: false,
  safeAreaInsetTop: false,
  border: false,
  placeholder: false,
  zIndex: 10,
  style: {},
} as NavBarProps
export const NavBar: FunctionComponent<Partial<NavBarProps>> = (props) => {
  const {
    desc,
    title,
    titIcon,
    leftShow,
    className,
    style,
    leftText,
    fixed,
    safeAreaInsetTop,
    border,
    placeholder,
    zIndex,
    onClickTitle,
    onClickIcon,
    onClickBack,
    onClickRight,
  } = {
    ...defaultProps,
    ...props,
  }
  const b = bem('navbar')

  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 styles = () => {
    return {
      ...style,
      zIndex,
    }
  }

  const renderLeft = () => {
    return (
      <div className={`${b('left')}`} onClick={(e) => onClickBack(e)}>
        {leftShow && <Icon name="left" color="#979797" />}
        {leftText && <div className={`${b('text')}`}>{leftText}</div>}
        {slot.left}
      </div>
    )
  }

  const renderContent = () => {
    return (
      <div className={`${b('title')}`}>
        {title && (
          <div className="title" onClick={(e) => onClickTitle(e)}>
            {title}
          </div>
        )}
        {titIcon && (
          <div onClick={(e) => onClickIcon(e)}>
            <Icon name={titIcon} />
          </div>
        )}
        {slot.content}
      </div>
    )
  }

  const renderRight = () => {
    return (
      <div className={`${b('right')}`} onClick={(e) => onClickRight(e)}>
        {desc && <div className={`${b('text')}`}>{desc}</div>}
        {slot.right}
      </div>
    )
  }

  const renderWrapper = () => {
    return (
      <div className={cls} style={styles()}>
        {renderLeft()}
        {renderContent()}
        {renderRight()}
      </div>
    )
  }

  const classes = classNames({
    [`nut-navbar--border`]: border,
    [`nut-navbar--fixed`]: fixed,
    [`nut-navbar--safe-area-inset-top`]: safeAreaInsetTop,
  })

  const cls = classNames(b(''), classes, className)

  return (
    <>
      {fixed && placeholder ? (
        <div className={`${b('')}--placeholder`}>{renderWrapper()}</div>
      ) : (
        renderWrapper()
      )}
    </>
  )
}

NavBar.defaultProps = defaultProps
NavBar.displayName = 'NutNavBar'