index.js 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
import React, { PureComponent } from 'react';
import { Popover, Icon, Tabs, Badge, Spin } from 'antd';
import classNames from 'classnames';
import List from './NoticeList';
import styles from './index.less';

const { TabPane } = Tabs;

export default class NoticeIcon extends PureComponent {
jim's avatar
jim committed
10 11
  static Tab = TabPane;

12 13 14 15 16 17 18 19 20 21
  static defaultProps = {
    onItemClick: () => {},
    onPopupVisibleChange: () => {},
    onTabChange: () => {},
    onClear: () => {},
    loading: false,
    locale: {
      emptyText: '暂无数据',
      clear: '清空',
    },
afc163's avatar
afc163 committed
22
    emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
23
  };
陈帅's avatar
陈帅 committed
24

25 26 27 28 29 30 31
  constructor(props) {
    super(props);
    this.state = {};
    if (props.children && props.children[0]) {
      this.state.tabType = props.children[0].props.title;
    }
  }
陈帅's avatar
陈帅 committed
32

33 34 35
  onItemClick = (item, tabProps) => {
    const { onItemClick } = this.props;
    onItemClick(item, tabProps);
jim's avatar
jim committed
36
  };
陈帅's avatar
陈帅 committed
37

jim's avatar
jim committed
38
  onTabChange = tabType => {
39
    this.setState({ tabType });
40 41
    const { onTabChange } = this.state;
    onTabChange(tabType);
jim's avatar
jim committed
42
  };
陈帅's avatar
陈帅 committed
43

44
  getNotificationBox() {
45
    const { children, loading, locale, onClear } = this.props;
46 47 48
    if (!children) {
      return null;
    }
jim's avatar
jim committed
49 50 51 52 53
    const panes = React.Children.map(children, child => {
      const title =
        child.props.list && child.props.list.length > 0
          ? `${child.props.title} (${child.props.list.length})`
          : child.props.title;
54 55 56
      return (
        <TabPane tab={title} key={child.props.title}>
          <List
afc163's avatar
afc163 committed
57
            {...child.props}
58 59
            data={child.props.list}
            onClick={item => this.onItemClick(item, child.props)}
60
            onClear={() => onClear(child.props.title)}
61 62 63 64 65 66 67 68 69 70 71 72 73 74
            title={child.props.title}
            locale={locale}
          />
        </TabPane>
      );
    });
    return (
      <Spin spinning={loading} delay={0}>
        <Tabs className={styles.tabs} onChange={this.onTabChange}>
          {panes}
        </Tabs>
      </Spin>
    );
  }
陈帅's avatar
陈帅 committed
75

76
  render() {
77
    const { className, count, popupAlign, onPopupVisibleChange, popupVisible } = this.props;
78 79 80 81 82 83 84 85 86 87 88 89
    const noticeButtonClass = classNames(className, styles.noticeButton);
    const notificationBox = this.getNotificationBox();
    const trigger = (
      <span className={noticeButtonClass}>
        <Badge count={count} className={styles.badge}>
          <Icon type="bell" className={styles.icon} />
        </Badge>
      </span>
    );
    if (!notificationBox) {
      return trigger;
    }
afc163's avatar
afc163 committed
90 91
    const popoverProps = {};
    if ('popupVisible' in this.props) {
92
      popoverProps.visible = popupVisible;
afc163's avatar
afc163 committed
93
    }
94 95 96 97 98 99 100 101
    return (
      <Popover
        placement="bottomRight"
        content={notificationBox}
        popupClassName={styles.popover}
        trigger="click"
        arrowPointAtCenter
        popupAlign={popupAlign}
afc163's avatar
afc163 committed
102 103
        onVisibleChange={onPopupVisibleChange}
        {...popoverProps}
104 105 106 107 108 109
      >
        {trigger}
      </Popover>
    );
  }
}