11import React , { memo , useEffect , useState , useRef } from 'react' ;
22import classnames from 'classnames' ;
33import Iconfont from '@/components/Iconfont' ;
4- import styles from './index.less' ;
5- import { Popover , Dropdown } from 'antd' ;
4+ import { Popover , Dropdown , MenuProps } from 'antd' ;
65import i18n from '@/i18n' ;
6+ import { isValid } from '@/utils/check' ;
7+ import _ from 'lodash' ;
8+ import styles from './index.less' ;
79
810export interface ITabItem {
911 prefixIcon ?: string | React . ReactNode ;
@@ -55,28 +57,28 @@ export default memo<IProps>((props) => {
5557 const tabsNavRef = useRef < HTMLDivElement > ( null ) ;
5658
5759 useEffect ( ( ) => {
58- if ( activeKey !== null && activeKey !== undefined ) {
59- setInternalActiveTab ( activeKey ) ;
60+ if ( isValid ( activeKey ) ) {
61+ setInternalActiveTab ( activeKey ! ) ;
6062 }
6163 } , [ activeKey ] ) ;
6264
6365 useEffect ( ( ) => {
6466 setInternalTabs ( items || [ ] ) ;
65- if ( items ?. length && ( internalActiveTab === undefined || internalActiveTab === null ) ) {
67+ if ( items ?. length && ! isValid ( internalActiveTab ) ) {
6668 setInternalActiveTab ( items [ 0 ] ?. key ) ;
6769 }
6870 } , [ items ] ) ;
6971
7072 useEffect ( ( ) => {
71- const fn = ( e ) => {
73+ const fn = _ . debounce ( ( e ) => {
7274 if ( e . deltaY ) {
7375 e . preventDefault ( ) ;
7476 // 鼠标滚轮事件,让tab可以横向滚动
7577 if ( tabsNavRef . current ) {
7678 tabsNavRef . current . scrollLeft -= e . deltaY ;
7779 }
7880 }
79- } ;
81+ } , 30 ) ;
8082 tabsNavRef . current ?. addEventListener ( 'wheel' , fn ) ;
8183 return ( ) => {
8284 tabsNavRef . current ?. removeEventListener ( 'wheel' , fn ) ;
@@ -88,14 +90,16 @@ export default memo<IProps>((props) => {
8890 if ( tabListBoxRef . current ) {
8991 const activeTab = tabListBoxRef . current . querySelector ( `.${ styles . activeTab } ` ) ;
9092 if ( activeTab ) {
91- activeTab . scrollIntoView ( { block : 'nearest' } ) ;
93+ setTimeout ( ( ) => {
94+ activeTab . scrollIntoView ( { behavior : 'smooth' , inline : 'start' } ) ;
95+ } , 50 ) ;
9296 }
9397 }
9498
9599 onChange ?.( internalActiveTab ) ;
96100 } , [ internalActiveTab ] ) ;
97101
98- function deleteTab ( data : ITabItem ) {
102+ const deleteTab = ( data : ITabItem ) => {
99103 const newInternalTabs = internalTabs ?. filter ( ( t ) => t . key !== data . key ) ;
100104 let activeKeyTemp = internalActiveTab ;
101105 // 删掉的是当前激活的tab,那么就切换到前一个,如果前一个没有就切换到后一个
@@ -110,7 +114,7 @@ export default memo<IProps>((props) => {
110114 changeTab ( activeKeyTemp ) ;
111115 setInternalTabs ( newInternalTabs ) ;
112116 onEdit ?.( 'remove' , [ data ] , newInternalTabs ) ;
113- }
117+ } ;
114118
115119 const deleteOtherTab = ( data : ITabItem ) => {
116120 const newInternalTabs = internalTabs ?. filter ( ( t ) => t . key === data . key ) ;
@@ -127,21 +131,21 @@ export default memo<IProps>((props) => {
127131 onEdit ?.( 'remove' , [ ...internalTabs ] ) ;
128132 } ;
129133
130- function changeTab ( key : string | number | null ) {
134+ const changeTab = ( key : string | number | null ) => {
131135 setInternalActiveTab ( key ) ;
132- }
136+ } ;
133137
134- function handleAdd ( ) {
138+ const handleAdd = ( ) => {
135139 onEdit ?.( 'add' ) ;
136- }
140+ } ;
137141
138- function onDoubleClick ( t : ITabItem ) {
142+ const onDoubleClick = ( t : ITabItem ) => {
139143 if ( t . editableName ) {
140144 setEditingTab ( t . key ) ;
141145 }
142- }
146+ } ;
143147
144- function renderTabItem ( t : ITabItem , index : number ) {
148+ const renderTabItem = ( t : ITabItem , index : number ) => {
145149 function inputOnChange ( value : string ) {
146150 internalTabs [ index ] . label = value ;
147151 setInternalTabs ( [ ...internalTabs ] ) ;
@@ -170,7 +174,7 @@ export default memo<IProps>((props) => {
170174 deleteTab ( t ) ;
171175 } ,
172176 } ,
173- {
177+ {
174178 label : i18n ( 'common.button.closeOthers' ) ,
175179 key : 'closeOther' ,
176180 onClick : ( ) => {
@@ -227,7 +231,18 @@ export default memo<IProps>((props) => {
227231 </ Popover >
228232 </ Dropdown >
229233 ) ;
230- }
234+ } ;
235+
236+ const moreTabsMenu : MenuProps [ 'items' ] = ( internalTabs || [ ] ) . map ( ( t ) => {
237+ return {
238+ label : t . label ,
239+ key : t . key ,
240+ // itemIcon: t.prefixIcon,
241+ // onClick: () => {
242+ // changeTab(t.key);
243+ // },
244+ } ;
245+ } ) ;
231246
232247 return (
233248 < div className = { classnames ( styles . tabBox , className ) } >
@@ -242,6 +257,21 @@ export default memo<IProps>((props) => {
242257 ) }
243258 { ! hideAdd && (
244259 < div className = { styles . rightBox } >
260+ < div className = { styles . moreTabs } >
261+ < Dropdown
262+ menu = { {
263+ items : moreTabsMenu ,
264+ selectable : true ,
265+ selectedKeys : [ `${ activeKey } ` ] ,
266+ onClick : ( v ) => changeTab ( Number ( v . key ) ) ,
267+ } }
268+ // trigger={['click']}
269+ >
270+ < a onClick = { ( e ) => e . preventDefault ( ) } >
271+ < Iconfont code = "" />
272+ </ a >
273+ </ Dropdown >
274+ </ div >
245275 < div className = { styles . addIcon } onClick = { handleAdd } >
246276 < Iconfont code = "" />
247277 </ div >
0 commit comments