cell.tsx 2.6 KB
Newer Older
宋宋 已提交
1 2
import React, { FunctionComponent, CSSProperties, ReactNode } from 'react'
import { useHistory } from 'react-router-dom'
O
oasis-cloud 已提交
3

宋宋 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
import bem from '@/utils/bem'
import { Icon } from '../icon/icon'

export interface CellProps {
  title: string
  subTitle: string
  desc: string
  descTextAlign: string
  isLink: boolean
  to: string
  replace: boolean
  url: string
  icon: string
17
  className: string
宋宋 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
  extra: ReactNode
  click: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
}
const defaultProps = {
  title: '',
  subTitle: '',
  desc: '',
  descTextAlign: 'right',
  isLink: false,
  to: '',
  replace: false,
  url: '',
  icon: '',
31
  className: '',
宋宋 已提交
32 33 34
  extra: '',
  click: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {},
} as CellProps
L
liuyijun 已提交
35 36 37
export const Cell: FunctionComponent<Partial<CellProps> & React.HTMLAttributes<HTMLDivElement>> = (
  props
) => {
宋宋 已提交
38 39 40 41 42 43 44
  const {
    children,
    click,
    isLink,
    to,
    url,
    replace,
45
    className,
宋宋 已提交
46 47 48 49 50 51
    descTextAlign,
    desc,
    icon,
    title,
    subTitle,
    extra,
L
liuyijun 已提交
52
    ...rest
宋宋 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  } = {
    ...defaultProps,
    ...props,
  }
  const b = bem('cell')
  let history = useHistory()
  const handleClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
    click(event)
    if (to && history) {
      history[replace ? 'replace' : 'push'](to)
    } else if (url) {
      replace ? location.replace(url) : (location.href = url)
    }
  }

68 69 70 71 72 73 74
  const styles =
    title || subTitle || icon
      ? {}
      : {
          textAlign: descTextAlign,
          flex: 1,
        }
宋宋 已提交
75 76
  return (
    <div
77
      className={`${b({ clickable: isLink || to ? true : false }, [className])} `}
宋宋 已提交
78
      onClick={(event) => handleClick(event)}
L
liuyijun 已提交
79
      {...rest}
宋宋 已提交
80 81 82 83 84 85 86 87
    >
      {children ? (
        children
      ) : (
        <>
          {title || subTitle || icon ? (
            <>
              <div className={`${b('title', { icon: icon ? true : false })}`}>
88
                {icon ? <Icon name={icon} className={`${b('icon')}`} /> : null}
宋宋 已提交
89 90 91 92 93 94 95 96 97 98 99 100
                {subTitle ? (
                  <>
                    <div className={b('maintitle')}>{title}</div>
                    <div className={b('subtitle')}>{subTitle}</div>
                  </>
                ) : (
                  <>{title}</>
                )}
              </div>
            </>
          ) : null}
          {desc ? (
101
            <div className={b('desc')} style={styles as React.CSSProperties}>
宋宋 已提交
102 103 104 105 106 107
              {desc}
            </div>
          ) : null}
        </>
      )}
      {extra ? extra : null}
108
      {!extra && (isLink || to) ? <Icon name="right" className={b('link')}></Icon> : null}
宋宋 已提交
109 110 111 112 113 114
    </div>
  )
}

Cell.defaultProps = defaultProps
Cell.displayName = 'NutCell'