index.js 2.79 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
  static defaultProps = {
    onItemClick: () => {},
    onPopupVisibleChange: () => {},
    onTabChange: () => {},
    onClear: () => {},
    loading: false,
    locale: {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
19 20
      emptyText: 'No notifications',
      clear: 'Clear',
21
    },
afc163's avatar
afc163 committed
22
    emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
23
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
24

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

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

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

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