index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-11-16 09:12:37
  4. * @LastEditTime: 2022-02-14 11:39:52
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: /KC-MiddlePlatform/src/pages/index/components/topBar/index.tsx
  8. */
  9. import React, { useEffect, useState } from 'react';
  10. import styles from './style.less';
  11. import { Tooltip } from 'antd';
  12. import { LogoutOutlined, SettingOutlined } from '@ant-design/icons';
  13. import logo from '../../../public/images/kc-logo.png';
  14. import platFormMenuIcon from '../../../public/images/platformMenu.png';
  15. import tabCloseIcon from '../../../public/images/tabCloseIcon.png';
  16. import { history } from 'umi';
  17. interface TopBarType {
  18. onTabChange?: (data: TopBar.Tab[]) => void; //当tab切换时回调
  19. sysList: TopBar.Tab[]; //可选系统列表
  20. openedTabs: TopBar.Tab[]; //已打开的列表
  21. currentTab?: TopBar.Tab | undefined; //当前tab
  22. userPannelTabClick?: (tag: 'SETTING' | 'LOGOUT' | 'SETUSERINFO') => void;
  23. onCloseTab?: (data: TopBar.Tab) => void;
  24. onTabClick?: (data: TopBar.Tab) => void;
  25. userData?: { name: string; [key: string]: any };
  26. }
  27. const TopBar: React.FC<TopBarType> = (props) => {
  28. const {
  29. onTabChange,
  30. sysList = [],
  31. openedTabs = [],
  32. currentTab,
  33. userPannelTabClick,
  34. onCloseTab,
  35. onTabClick,
  36. userData,
  37. } = props;
  38. const [systemTabs, setSystemTabs] = useState<TopBar.Tab[]>([]); //已打开的tab
  39. const [currentSelectedTab, setCurrentSelectedTab] = useState<TopBar.Tab>();
  40. const [ifOpenPannel, setIfOpenPannel] = useState(false);
  41. const [arrowRotate, setArrowRotate] = useState(false);
  42. // const [opendedTabs,setOpendedTabs] = useState<SystemListItem[]>([]);
  43. const _systemTabClickHandle = (item: TopBar.Tab) => {
  44. //导航栏tab点击
  45. // console.log('tab click');
  46. setCurrentSelectedTab(item);
  47. onTabClick && onTabClick(item);
  48. };
  49. const _systemListClickHandle = (data: TopBar.Tab) => {
  50. //导航栏系统菜单列表点击回调
  51. const findIndex = systemTabs.findIndex((t) => t.id == data.id);
  52. if (findIndex != -1) return;
  53. _systemTabClickHandle(data); //触发一次tab点击
  54. setSystemTabs([...systemTabs, data]);
  55. onTabChange && onTabChange([...systemTabs, data]);
  56. };
  57. const _userPannelTabClick = (tag: 'SETTING' | 'LOGOUT') => {
  58. //用户菜单tab点击回调
  59. userPannelTabClick && userPannelTabClick(tag);
  60. };
  61. const closeTabHandle = (item: TopBar.Tab, e: React.MouseEvent) => {
  62. e.stopPropagation();
  63. let _systemTabs = [...systemTabs];
  64. let delIndex = -1;
  65. const filtered = _systemTabs.filter((t, index) => {
  66. if (t.id == item.id) {
  67. delIndex = index;
  68. }
  69. return t.id != item.id;
  70. });
  71. setSystemTabs([...filtered]);
  72. if (delIndex != 0) {
  73. _systemTabClickHandle(_systemTabs[delIndex - 1]); //自动切换为前一个tab
  74. }
  75. onTabChange && onTabChange(filtered);
  76. onCloseTab && onCloseTab(item);
  77. };
  78. const UserPannel = () => {
  79. return (
  80. <div className={styles.userPannel}>
  81. <div className={styles.userPannelTab} onClick={() => _userPannelTabClick('SETTING')}>
  82. <SettingOutlined />
  83. 设置
  84. </div>
  85. <div className={styles.userPannelTab} onClick={() => _userPannelTabClick('LOGOUT')}>
  86. <LogoutOutlined />
  87. 退出
  88. </div>
  89. </div>
  90. );
  91. };
  92. useEffect(() => {
  93. if (openedTabs.length == 1) {
  94. //当有且仅当只有一个的时候,默认激活
  95. setCurrentSelectedTab(openedTabs[0]);
  96. history.push(openedTabs[0].url);
  97. }
  98. if (openedTabs.length == 0) {
  99. //当所有tab都关闭时
  100. setCurrentSelectedTab(undefined);
  101. history.push('/index');
  102. }
  103. }, [openedTabs]);
  104. useEffect(() => {
  105. // console.log('Topbar props',props);
  106. setSystemTabs(openedTabs);
  107. currentTab && setCurrentSelectedTab(currentTab);
  108. }, [props]);
  109. return (
  110. <div className={styles.topBar}>
  111. <div className={styles.logoWrap}>
  112. <img className={styles.logo} src={logo} />
  113. </div>
  114. <div
  115. className={ifOpenPannel ? `${styles.platformMenu} ${styles.on}` : `${styles.platformMenu}`}
  116. onClick={() => setIfOpenPannel(!ifOpenPannel)}
  117. >
  118. <img src={platFormMenuIcon} />
  119. <div className={styles.systemPannel}>
  120. {sysList.map((item) => (
  121. <div
  122. key={item.id}
  123. className={styles.systemList}
  124. onClick={() => _systemListClickHandle(item)}
  125. >
  126. <img src={item.icon} alt="" />
  127. <div className={styles.systemListName}>{item.name}</div>
  128. </div>
  129. ))}
  130. </div>
  131. </div>
  132. {/**
  133. * 已打开的tab
  134. */}
  135. <div className={styles.tabWrap}>
  136. {systemTabs.map((item) => (
  137. <div
  138. key={item.id}
  139. className={
  140. currentSelectedTab?.id == item.id ? `${styles.tab} ${styles.on}` : `${styles.tab}`
  141. }
  142. onClick={() => _systemTabClickHandle(item)}
  143. >
  144. <div className={styles.tabText}>{item.name} </div>
  145. <div className={styles.closeIconWrap}>
  146. <img src={tabCloseIcon} onClick={(e) => closeTabHandle(item, e)} alt="close" />
  147. </div>
  148. </div>
  149. ))}
  150. </div>
  151. <div className={styles.userRelaInfoWrap}>
  152. <div className={styles.notification}>
  153. <img
  154. className={styles.notificationIcon}
  155. src={require('../../../public/images/notificationIcon.png')}
  156. />
  157. </div>
  158. <Tooltip
  159. title={<UserPannel />}
  160. color="#fff"
  161. onVisibleChange={(visible) => setArrowRotate(visible)}
  162. >
  163. <div className={styles.user}>
  164. <img className={styles.avator} src={require('../../../public/images/Mask.png')} />
  165. <span className={styles.name}>{userData?.name}</span>
  166. <img
  167. className={arrowRotate ? `${styles.arrow} ${styles.on}` : `${styles.arrow}`}
  168. src={require('../../../public/images/arrow_white.png')}
  169. />
  170. </div>
  171. </Tooltip>
  172. </div>
  173. </div>
  174. );
  175. };
  176. export default TopBar;