index.js 1.69 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';

7 8 9 10 11 12
const renderTotal = (total) => {
  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
niko's avatar
niko committed
36
        className={classNames(styles.chartTop, { [styles.chartTopMargin]: !children && !footer })}
37
      >
niko's avatar
niko committed
38
        <div className={styles.avatar}>{avatar}</div>
39 40 41 42 43
        <div className={styles.metaWrap}>
          <div className={styles.meta}>
            <span className={styles.title}>{title}</span>
            <span className={styles.action}>{action}</span>
          </div>
44
          {renderTotal(total)}
45
        </div>
46
      </div>
niko's avatar
niko committed
47 48 49 50 51 52 53 54 55 56
      {children && (
        <div className={styles.content} style={{ height: contentHeight || 'auto' }}>
          <div className={contentHeight && styles.contentFixed}>{children}</div>
        </div>
      )}
      {footer && (
        <div className={classNames(styles.footer, { [styles.footerMargin]: !children })}>
          {footer}
        </div>
      )}
57
    </div>
58 59 60
  );

  return (
niko's avatar
niko committed
61 62 63 64 65 66
    <Card bodyStyle={{ padding: '20px 24px 8px 24px' }} {...rest}>
      {
        <Spin spinning={loading} wrapperClassName={styles.spin}>
          {content}
        </Spin>
      }
67 68 69
    </Card>
  );
};
70 71

export default ChartCard;