index.js 3.19 KB
Newer Older
1
import React, { PureComponent } from 'react';
wingsico's avatar
wingsico committed
2
import ReactDOM from 'react-dom';
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
  static defaultProps = {
    onItemClick: () => {},
    onPopupVisibleChange: () => {},
    onTabChange: () => {},
    onClear: () => {},
    loading: false,
wingsico's avatar
wingsico committed
19
    clearClose: false,
20
    locale: {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21 22
      emptyText: 'No notifications',
      clear: 'Clear',
23
    },
afc163's avatar
afc163 committed
24
    emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
25
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
26

27 28
  onItemClick = (item, tabProps) => {
    const { onItemClick } = this.props;
wingsico's avatar
wingsico committed
29
    const { clickClose } = item;
30
    onItemClick(item, tabProps);
wingsico's avatar
wingsico committed
31 32 33
    if (clickClose) {
      this.popover.click();
    }
jim's avatar
jim committed
34
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35

36
  onClear = name => {
wingsico's avatar
wingsico committed
37
    const { onClear, clearClose } = this.props;
38
    onClear(name);
wingsico's avatar
wingsico committed
39 40 41
    if (clearClose) {
      this.popover.click();
    }
42
  };
wingsico's avatar
wingsico committed
43

jim's avatar
jim committed
44
  onTabChange = tabType => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45
    const { onTabChange } = this.props;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46
    onTabChange(tabType);
jim's avatar
jim committed
47
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48

49
  getNotificationBox() {
wingsico's avatar
wingsico committed
50
    const { children, loading, locale } = this.props;
51 52 53
    if (!children) {
      return null;
    }
jim's avatar
jim committed
54
    const panes = React.Children.map(children, child => {
55 56 57 58
      const { list, title, name, count } = child.props;
      const len = list && list.length ? list.length : 0;
      const msgCount = count || count === 0 ? count : len;
      const tabTitle = msgCount > 0 ? `${title} (${msgCount})` : title;
59
      return (
60
        <TabPane tab={tabTitle} key={name}>
61
          <List
afc163's avatar
afc163 committed
62
            {...child.props}
63
            data={list}
64
            onClick={item => this.onItemClick(item, child.props)}
65 66
            onClear={() => this.onClear(name)}
            title={title}
67 68 69 70 71 72 73 74 75 76 77 78 79
            locale={locale}
          />
        </TabPane>
      );
    });
    return (
      <Spin spinning={loading} delay={0}>
        <Tabs className={styles.tabs} onChange={this.onTabChange}>
          {panes}
        </Tabs>
      </Spin>
    );
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80

81
  render() {
82
    const { className, count, popupAlign, popupVisible, onPopupVisibleChange, bell } = this.props;
83 84
    const noticeButtonClass = classNames(className, styles.noticeButton);
    const notificationBox = this.getNotificationBox();
85
    const NoticeBellIcon = bell || <Icon type="bell" className={styles.icon} />;
86 87
    const trigger = (
      <span className={noticeButtonClass}>
jim's avatar
jim committed
88
        <Badge count={count} style={{ boxShadow: 'none' }} className={styles.badge}>
89
          {NoticeBellIcon}
90 91 92 93 94 95
        </Badge>
      </span>
    );
    if (!notificationBox) {
      return trigger;
    }
afc163's avatar
afc163 committed
96 97
    const popoverProps = {};
    if ('popupVisible' in this.props) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98
      popoverProps.visible = popupVisible;
afc163's avatar
afc163 committed
99
    }
100 101 102 103 104 105 106 107
    return (
      <Popover
        placement="bottomRight"
        content={notificationBox}
        popupClassName={styles.popover}
        trigger="click"
        arrowPointAtCenter
        popupAlign={popupAlign}
afc163's avatar
afc163 committed
108 109
        onVisibleChange={onPopupVisibleChange}
        {...popoverProps}
110
        ref={node => (this.popover = ReactDOM.findDOMNode(node))} // eslint-disable-line
111 112 113 114 115 116
      >
        {trigger}
      </Popover>
    );
  }
}