index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { PureComponent } from 'react';
  2. import { Popover, Icon, Tabs, Badge, Spin } from 'antd';
  3. import classNames from 'classnames';
  4. import List from './NoticeList';
  5. import styles from './index.less';
  6. const { TabPane } = Tabs;
  7. export default class NoticeIcon extends PureComponent {
  8. static Tab = TabPane;
  9. static defaultProps = {
  10. onItemClick: () => {},
  11. onPopupVisibleChange: () => {},
  12. onTabChange: () => {},
  13. onClear: () => {},
  14. loading: false,
  15. locale: {
  16. emptyText: '暂无数据',
  17. clear: '清空',
  18. },
  19. emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
  20. };
  21. constructor(props) {
  22. super(props);
  23. this.state = {};
  24. if (props.children && props.children[0]) {
  25. this.state.tabType = props.children[0].props.title;
  26. }
  27. }
  28. onItemClick = (item, tabProps) => {
  29. const { onItemClick } = this.props;
  30. onItemClick(item, tabProps);
  31. };
  32. onTabChange = tabType => {
  33. this.setState({ tabType });
  34. const { onTabChange } = this.state;
  35. onTabChange(tabType);
  36. };
  37. getNotificationBox() {
  38. const { children, loading, locale, onClear } = this.props;
  39. if (!children) {
  40. return null;
  41. }
  42. const panes = React.Children.map(children, child => {
  43. const title =
  44. child.props.list && child.props.list.length > 0
  45. ? `${child.props.title} (${child.props.list.length})`
  46. : child.props.title;
  47. return (
  48. <TabPane tab={title} key={child.props.title}>
  49. <List
  50. {...child.props}
  51. data={child.props.list}
  52. onClick={item => this.onItemClick(item, child.props)}
  53. onClear={() => onClear(child.props.title)}
  54. title={child.props.title}
  55. locale={locale}
  56. />
  57. </TabPane>
  58. );
  59. });
  60. return (
  61. <Spin spinning={loading} delay={0}>
  62. <Tabs className={styles.tabs} onChange={this.onTabChange}>
  63. {panes}
  64. </Tabs>
  65. </Spin>
  66. );
  67. }
  68. render() {
  69. const { className, count, popupAlign, onPopupVisibleChange, popupVisible } = this.props;
  70. const noticeButtonClass = classNames(className, styles.noticeButton);
  71. const notificationBox = this.getNotificationBox();
  72. const trigger = (
  73. <span className={noticeButtonClass}>
  74. <Badge count={count} className={styles.badge}>
  75. <Icon type="bell" className={styles.icon} />
  76. </Badge>
  77. </span>
  78. );
  79. if (!notificationBox) {
  80. return trigger;
  81. }
  82. const popoverProps = {};
  83. if ('popupVisible' in this.props) {
  84. popoverProps.visible = popupVisible;
  85. }
  86. return (
  87. <Popover
  88. placement="bottomRight"
  89. content={notificationBox}
  90. popupClassName={styles.popover}
  91. trigger="click"
  92. arrowPointAtCenter
  93. popupAlign={popupAlign}
  94. onVisibleChange={onPopupVisibleChange}
  95. {...popoverProps}
  96. >
  97. {trigger}
  98. </Popover>
  99. );
  100. }
  101. }