动态菜单和动态路由的 antd pro

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 =
  43. child.props.list && child.props.list.length > 0
  44. ? `${child.props.title} (${child.props.list.length})`
  45. : child.props.title;
  46. return (
  47. <TabPane tab={title} key={child.props.title}>
  48. <List
  49. {...child.props}
  50. data={child.props.list}
  51. onClick={item => this.onItemClick(item, child.props)}
  52. onClear={() => this.props.onClear(child.props.title)}
  53. title={child.props.title}
  54. locale={locale}
  55. />
  56. </TabPane>
  57. );
  58. });
  59. return (
  60. <Spin spinning={loading} delay={0}>
  61. <Tabs className={styles.tabs} onChange={this.onTabChange}>
  62. {panes}
  63. </Tabs>
  64. </Spin>
  65. );
  66. }
  67. render() {
  68. const { className, count, popupAlign, onPopupVisibleChange } = this.props;
  69. const noticeButtonClass = classNames(className, styles.noticeButton);
  70. const notificationBox = this.getNotificationBox();
  71. const trigger = (
  72. <span className={noticeButtonClass}>
  73. <Badge count={count} className={styles.badge}>
  74. <Icon type="bell" className={styles.icon} />
  75. </Badge>
  76. </span>
  77. );
  78. if (!notificationBox) {
  79. return trigger;
  80. }
  81. const popoverProps = {};
  82. if ('popupVisible' in this.props) {
  83. popoverProps.visible = this.props.popupVisible;
  84. }
  85. return (
  86. <Popover
  87. placement="bottomRight"
  88. content={notificationBox}
  89. popupClassName={styles.popover}
  90. trigger="click"
  91. arrowPointAtCenter
  92. popupAlign={popupAlign}
  93. onVisibleChange={onPopupVisibleChange}
  94. {...popoverProps}
  95. >
  96. {trigger}
  97. </Popover>
  98. );
  99. }
  100. }