index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 defaultProps = {
  9. onItemClick: () => {},
  10. onPopupVisibleChange: () => {},
  11. onTabChange: () => {},
  12. onClear: () => {},
  13. loading: false,
  14. locale: {
  15. emptyText: '暂无数据',
  16. clear: '清空',
  17. },
  18. emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
  19. };
  20. static Tab = TabPane;
  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. this.props.onTabChange(tabType);
  35. }
  36. getNotificationBox() {
  37. const { children, loading, locale } = this.props;
  38. if (!children) {
  39. return null;
  40. }
  41. const panes = React.Children.map(children, (child) => {
  42. const title = child.props.list && child.props.list.length > 0
  43. ? `${child.props.title} (${child.props.list.length})` : child.props.title;
  44. return (
  45. <TabPane tab={title} key={child.props.title}>
  46. <List
  47. {...child.props}
  48. data={child.props.list}
  49. onClick={item => this.onItemClick(item, child.props)}
  50. onClear={() => this.props.onClear(child.props.title)}
  51. title={child.props.title}
  52. locale={locale}
  53. />
  54. </TabPane>
  55. );
  56. });
  57. return (
  58. <Spin spinning={loading} delay={0}>
  59. <Tabs className={styles.tabs} onChange={this.onTabChange}>
  60. {panes}
  61. </Tabs>
  62. </Spin>
  63. );
  64. }
  65. render() {
  66. const { className, count, popupAlign, onPopupVisibleChange } = this.props;
  67. const noticeButtonClass = classNames(className, styles.noticeButton);
  68. const notificationBox = this.getNotificationBox();
  69. const trigger = (
  70. <span className={noticeButtonClass}>
  71. <Badge count={count} className={styles.badge}>
  72. <Icon type="bell" className={styles.icon} />
  73. </Badge>
  74. </span>
  75. );
  76. if (!notificationBox) {
  77. return trigger;
  78. }
  79. const popoverProps = {};
  80. if ('popupVisible' in this.props) {
  81. popoverProps.visible = this.props.popupVisible;
  82. }
  83. return (
  84. <Popover
  85. placement="bottomRight"
  86. content={notificationBox}
  87. popupClassName={styles.popover}
  88. trigger="click"
  89. arrowPointAtCenter
  90. popupAlign={popupAlign}
  91. onVisibleChange={onPopupVisibleChange}
  92. {...popoverProps}
  93. >
  94. {trigger}
  95. </Popover>
  96. );
  97. }
  98. }