index.js 3.16 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

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

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 55 56 57 58
    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;
59
      return (
60
        <TabPane tab={title} key={child.props.name}>
61
          <List
afc163's avatar
afc163 committed
62
            {...child.props}
63 64
            data={child.props.list}
            onClick={item => this.onItemClick(item, child.props)}
wingsico's avatar
wingsico committed
65
            onClear={() => this.onClear(child.props.name)}
66 67 68 69 70 71 72 73 74 75 76 77 78 79
            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
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}
wingsico's avatar
wingsico committed
110
        ref={node => { this.popover = ReactDOM.findDOMNode(node)}} // eslint-disable-line
111 112 113 114 115 116
      >
        {trigger}
      </Popover>
    );
  }
}