Skip to content

Commit b203aeb

Browse files
authored
Merge pull request #185 from insekkei/feature/tabs
fix(component): 修复多层 menu 父菜单切换后子菜单定位失败的问题
2 parents f88a539 + 81ddd05 commit b203aeb

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/menu/HeadMenu.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const HeadMenu: FC<HeadMenuProps> = (props) => {
2828
return activeMenu.props.children;
2929
}, [children, value.expandType, value.active]);
3030

31+
const currentChildsValues = childs?.length > 0 ? childs.map((item) => item.props.value) : [];
32+
3133
return (
3234
<MenuContext.Provider value={value}>
3335
<div
@@ -41,7 +43,10 @@ const HeadMenu: FC<HeadMenuProps> = (props) => {
4143
</div>
4244
{childs?.length > 0 && (
4345
<ul className={`${classPrefix}-head-menu__submenu ${classPrefix}-submenu`}>
44-
<Tabs value={value.active} onChange={value.onChange}>
46+
<Tabs
47+
value={currentChildsValues.includes(value.active) ? value.active : currentChildsValues[0]}
48+
onChange={value.onChange}
49+
>
4550
{childs.map(({ props }) => (
4651
<TabPanel value={props.value} key={props.value} label={props.children}></TabPanel>
4752
))}

src/tabs/TabNav.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { useCallback, useEffect, useRef, useState } from 'react';
22
import classNames from 'classnames';
33
import { AddIcon, ChevronLeftIcon, ChevronRightIcon } from 'tdesign-icons-react';
44
import { TdTabsProps, TdTabPanelProps, TabValue } from './type';
@@ -36,17 +36,23 @@ const TabNav: React.FC<TabNavProps> = (props) => {
3636

3737
// :todo 兼容老版本 TabBar 的实现
3838
const navContainerRef = useRef<HTMLDivElement>(null);
39-
const getIndex = (value = activeValue) => {
40-
let index = 0;
41-
itemList.forEach((v, i) => {
42-
if (v.value === value) {
43-
index = i;
44-
}
45-
});
46-
return index;
47-
};
39+
const getIndex = useCallback(
40+
(value) => {
41+
let index = 0;
42+
itemList.forEach((v, i) => {
43+
if (v.value === value) {
44+
index = i;
45+
}
46+
});
47+
return index;
48+
},
49+
[itemList],
50+
);
4851

49-
const [activeIndex, setActiveIndex] = useState(getIndex());
52+
const [activeIndex, setActiveIndex] = useState(getIndex(activeValue));
53+
useEffect(() => {
54+
setActiveIndex(getIndex(activeValue));
55+
}, [activeValue, getIndex]);
5056

5157
// 判断滚动条是否需要展示
5258
const [isScrollVisible, setIsScrollVisible] = useState(false);

src/tabs/TabNavItem.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const TabNavItem: React.FC<TabNavItemProps> = (props) => {
3838

3939
// 样式变量和常量定义
4040
const { tdTabsClassGenerator, tdClassGenerator, tdSizeClassGenerator } = useTabClass();
41-
4241
const rippleRef = useRef();
4342
useRipple(rippleRef);
4443

src/tabs/Tabs.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import classNames from 'classnames';
33
import { TabValue, TdTabsProps } from './type';
44
import forwardRefWithStatics from '../_util/forwardRefWithStatics';
@@ -12,7 +12,7 @@ export interface TabsProps extends TdTabsProps {
1212

1313
const Tabs = forwardRefWithStatics(
1414
(props: TabsProps, ref) => {
15-
const { children, placement, onRemove } = props;
15+
const { children, placement, onRemove, value: tabValue } = props;
1616
let { defaultValue } = props;
1717

1818
// 样式工具引入
@@ -32,6 +32,10 @@ const Tabs = forwardRefWithStatics(
3232

3333
const [value, setValue] = useState<TabValue>(defaultValue);
3434

35+
useEffect(() => {
36+
tabValue !== undefined && setValue(tabValue);
37+
}, [tabValue]);
38+
3539
const renderTabNav = () => (
3640
<TabNav {...props} activeValue={value} onRemove={onRemove} itemList={itemList} tabClick={setValue} />
3741
);

0 commit comments

Comments
 (0)