index.js 1.75 KB
Newer Older
1
import React from 'react';
2
import { Card, Spin } from 'antd';
3
import classNames from 'classnames';
4 5 6

import styles from './index.less';

jim's avatar
jim committed
7
const renderTotal = total => {
8 9 10 11 12
  let totalDom;
  switch (typeof total) {
    case undefined:
      totalDom = null;
      break;
niko's avatar
niko committed
13 14
    case 'function':
      totalDom = <div className={styles.total}>{total()}</div>;
15 16 17 18 19 20 21
      break;
    default:
      totalDom = <div className={styles.total}>{total}</div>;
  }
  return totalDom;
};

22
const ChartCard = ({
niko's avatar
niko committed
23 24 25 26 27 28 29 30 31
  loading = false,
  contentHeight,
  title,
  avatar,
  action,
  total,
  footer,
  children,
  ...rest
32
}) => {
33
  const content = (
34
    <div className={styles.chartCard}>
35
      <div
jim's avatar
jim committed
36 37 38
        className={classNames(styles.chartTop, {
          [styles.chartTopMargin]: !children && !footer,
        })}
39
      >
niko's avatar
niko committed
40
        <div className={styles.avatar}>{avatar}</div>
41 42 43 44 45
        <div className={styles.metaWrap}>
          <div className={styles.meta}>
            <span className={styles.title}>{title}</span>
            <span className={styles.action}>{action}</span>
          </div>
46
          {renderTotal(total)}
47
        </div>
48
      </div>
niko's avatar
niko committed
49 50 51 52 53 54
      {children && (
        <div className={styles.content} style={{ height: contentHeight || 'auto' }}>
          <div className={contentHeight && styles.contentFixed}>{children}</div>
        </div>
      )}
      {footer && (
jim's avatar
jim committed
55 56 57 58 59
        <div
          className={classNames(styles.footer, {
            [styles.footerMargin]: !children,
          })}
        >
niko's avatar
niko committed
60 61 62
          {footer}
        </div>
      )}
63
    </div>
64 65 66
  );

  return (
niko's avatar
niko committed
67 68 69 70 71 72
    <Card bodyStyle={{ padding: '20px 24px 8px 24px' }} {...rest}>
      {
        <Spin spinning={loading} wrapperClassName={styles.spin}>
          {content}
        </Spin>
      }
73 74 75
    </Card>
  );
};
76 77

export default ChartCard;