index.js 3.5 KB
Newer Older
1
import React, { PureComponent, Fragment } from 'react';
wingsico's avatar
wingsico committed
2
import ReactDOM from 'react-dom';
3
import { Icon, Tabs, Badge, Spin } from 'antd';
4
import classNames from 'classnames';
5
import HeaderDropdown from '../HeaderDropdown';
6 7 8 9 10 11
import List from './NoticeList';
import styles from './index.less';

const { TabPane } = Tabs;

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

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

28 29 30 31
  state = {
    visible: false,
  };

32 33
  onItemClick = (item, tabProps) => {
    const { onItemClick } = this.props;
wingsico's avatar
wingsico committed
34
    const { clickClose } = item;
35
    onItemClick(item, tabProps);
wingsico's avatar
wingsico committed
36 37 38
    if (clickClose) {
      this.popover.click();
    }
jim's avatar
jim committed
39
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40

41
  onClear = name => {
wingsico's avatar
wingsico committed
42
    const { onClear, clearClose } = this.props;
43
    onClear(name);
wingsico's avatar
wingsico committed
44 45 46
    if (clearClose) {
      this.popover.click();
    }
47
  };
wingsico's avatar
wingsico committed
48

jim's avatar
jim committed
49
  onTabChange = tabType => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50
    const { onTabChange } = this.props;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
51
    onTabChange(tabType);
jim's avatar
jim committed
52
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
53

54
  getNotificationBox() {
wingsico's avatar
wingsico committed
55
    const { children, loading, locale } = this.props;
56 57 58
    if (!children) {
      return null;
    }
jim's avatar
jim committed
59
    const panes = React.Children.map(children, child => {
60 61 62 63
      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;
64
      return (
65
        <TabPane tab={tabTitle} key={name}>
66
          <List
afc163's avatar
afc163 committed
67
            {...child.props}
68
            data={list}
69
            onClick={item => this.onItemClick(item, child.props)}
70 71
            onClear={() => this.onClear(name)}
            title={title}
72 73 74 75 76 77
            locale={locale}
          />
        </TabPane>
      );
    });
    return (
78 79 80 81 82 83 84
      <Fragment>
        <Spin spinning={loading} delay={0}>
          <Tabs className={styles.tabs} onChange={this.onTabChange}>
            {panes}
          </Tabs>
        </Spin>
      </Fragment>
85 86
    );
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
87

88 89 90 91 92 93
  handleVisibleChange = visible => {
    const { onPopupVisibleChange } = this.props;
    this.setState({ visible });
    onPopupVisibleChange(visible);
  };

94
  render() {
95 96
    const { className, count, popupVisible, bell } = this.props;
    const { visible } = this.state;
97 98
    const noticeButtonClass = classNames(className, styles.noticeButton);
    const notificationBox = this.getNotificationBox();
99
    const NoticeBellIcon = bell || <Icon type="bell" className={styles.icon} />;
100
    const trigger = (
101
      <span className={classNames(noticeButtonClass, { opened: visible })}>
jim's avatar
jim committed
102
        <Badge count={count} style={{ boxShadow: 'none' }} className={styles.badge}>
103
          {NoticeBellIcon}
104 105 106 107 108 109
        </Badge>
      </span>
    );
    if (!notificationBox) {
      return trigger;
    }
afc163's avatar
afc163 committed
110 111
    const popoverProps = {};
    if ('popupVisible' in this.props) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
112
      popoverProps.visible = popupVisible;
afc163's avatar
afc163 committed
113
    }
114
    return (
115
      <HeaderDropdown
116
        placement="bottomRight"
117 118 119 120 121
        overlay={notificationBox}
        overlayClassName={styles.popover}
        trigger={['click']}
        visible={visible}
        onVisibleChange={this.handleVisibleChange}
afc163's avatar
afc163 committed
122
        {...popoverProps}
123
        ref={node => (this.popover = ReactDOM.findDOMNode(node))} // eslint-disable-line
124 125
      >
        {trigger}
126
      </HeaderDropdown>
127 128 129
    );
  }
}