index.js 2.9 KB
Newer Older
1
import React, { PureComponent } from 'react';
2
import { formatMessage } from 'umi/locale';
3 4 5 6 7 8 9 10
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
11 12
  static Tab = TabPane;

13 14 15 16 17 18 19
  static defaultProps = {
    onItemClick: () => {},
    onPopupVisibleChange: () => {},
    onTabChange: () => {},
    onClear: () => {},
    loading: false,
    locale: {
20 21
      emptyText: formatMessage({ id: 'component.noticeIcon.empty' }),
      clear: formatMessage({ id: 'component.noticeIcon.clear' }),
22
    },
afc163's avatar
afc163 committed
23
    emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
24
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25

26 27 28
  onItemClick = (item, tabProps) => {
    const { onItemClick } = this.props;
    onItemClick(item, tabProps);
jim's avatar
jim committed
29
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30

jim's avatar
jim committed
31
  onTabChange = tabType => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32
    const { onTabChange } = this.props;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33
    onTabChange(tabType);
jim's avatar
jim committed
34
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35

36
  getNotificationBox() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
    const { children, loading, locale, onClear } = this.props;
38 39 40
    if (!children) {
      return null;
    }
jim's avatar
jim committed
41 42 43 44 45
    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;
46
      return (
47
        <TabPane tab={title} key={child.props.name}>
48
          <List
afc163's avatar
afc163 committed
49
            {...child.props}
50 51
            data={child.props.list}
            onClick={item => this.onItemClick(item, child.props)}
52
            onClear={() => onClear(child.props.name)}
53 54 55 56 57 58 59 60 61 62 63 64 65 66
            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
67

68
  render() {
69
    const { className, count, popupAlign, popupVisible, onPopupVisibleChange, bell } = this.props;
70 71
    const noticeButtonClass = classNames(className, styles.noticeButton);
    const notificationBox = this.getNotificationBox();
72
    const NoticeBellIcon = bell || <Icon type="bell" className={styles.icon} />;
73 74
    const trigger = (
      <span className={noticeButtonClass}>
jim's avatar
jim committed
75
        <Badge count={count} style={{ boxShadow: 'none' }} className={styles.badge}>
76
          {NoticeBellIcon}
77 78 79 80 81 82
        </Badge>
      </span>
    );
    if (!notificationBox) {
      return trigger;
    }
afc163's avatar
afc163 committed
83 84
    const popoverProps = {};
    if ('popupVisible' in this.props) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
85
      popoverProps.visible = popupVisible;
afc163's avatar
afc163 committed
86
    }
87 88 89 90 91 92 93 94
    return (
      <Popover
        placement="bottomRight"
        content={notificationBox}
        popupClassName={styles.popover}
        trigger="click"
        arrowPointAtCenter
        popupAlign={popupAlign}
afc163's avatar
afc163 committed
95 96
        onVisibleChange={onPopupVisibleChange}
        {...popoverProps}
97 98 99 100 101 102
      >
        {trigger}
      </Popover>
    );
  }
}