index.js 1.84 KB
Newer Older
1
import React from 'react';
niko's avatar
niko committed
2 3 4
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Icon, Tooltip } from 'antd';
5 6 7

import styles from './index.less';

niko's avatar
niko committed
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
const Item = ({ title, flag, children, ...rest }, { mini }) => {
  const map = {
    xs: 0,
    sm: 0,
    md: 0,
    lg: 0,
    xlg: 0,
    xl: 0,
    xxl: 0,
  };

  if (mini && mini.forEach) {
    mini.forEach((size) => {
      map[size] = 1;
    });
  }

  const clsObj = {};
  Object.keys(map).forEach((k) => {
    clsObj[styles[k]] = map[k];
  });

  const clsString = classNames(styles.trendItem, {
    [styles.mini]: (typeof mini === 'boolean' && mini),
    ...clsObj,
  });

  const miniContent = (
    <Tooltip title={children}>
      <span className={styles.title}>{title}</span>
      { flag && <span className={styles[flag]}><Icon type={`caret-${flag}`} /></span>}
    </Tooltip>
  );

  return (
    <div {...rest} className={clsString}>
      <div className={styles.content}>
        <span className={styles.title}>{title}</span>
        { flag && <span className={styles[flag]}><Icon type={`caret-${flag}`} /></span>}
        <span className={styles.value}>{children}</span>
      </div>
      <div className={styles.miniContent}>
        {miniContent}
      </div>
    </div>
  );
};

Item.contextTypes = {
  mini: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.bool,
  ]),
};

class Trend extends React.Component {
  getChildContext() {
    return {
      mini: this.props.mini,
    };
  }

  render() {
    const { colorType, children, mini, ...rest } = this.props;
    return (
      <div className={colorType ? (styles[`trend${colorType}`] || styles.trend) : styles.trend} {...rest}>
        {children}
      </div>
    );
  }
}

Trend.childContextTypes = {
  mini: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.bool,
  ]),
};
86 87 88 89

Trend.Item = Item;

export default Trend;