Skip to content

Commit 526a924

Browse files
coderz-wafc163
andauthored
fix: active wrong when submenus has key '' (#721)
* fix: active wrong when submenus has key '' * feat:optimise code * feat: add test * fix:grammar * Update tests/SubMenu.spec.tsx --------- Co-authored-by: afc163 <[email protected]>
1 parent 99a1709 commit 526a924

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/hooks/useKeyRecords.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ export default function useKeyRecords() {
7474

7575
const isSubPathKey = useCallback(
7676
(pathKeys: string[], eventKey: string) =>
77-
pathKeys.some(pathKey => {
78-
const pathKeyList = getKeyPath(pathKey, true);
79-
80-
return pathKeyList.includes(eventKey);
81-
}),
77+
pathKeys
78+
.filter(item => item !== undefined)
79+
.some(pathKey => {
80+
const pathKeyList = getKeyPath(pathKey, true);
81+
return pathKeyList.includes(eventKey);
82+
}),
8283
[getKeyPath],
8384
);
84-
8585
const getKeys = () => {
8686
const keys = [...key2pathRef.current.keys()];
8787

tests/SubMenu.spec.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@ describe('SubMenu', () => {
150150
expect(childText).toEqual('submenu');
151151
});
152152

153+
it(`The submenu item with key '' must not persistently remain active`, () => {
154+
const { container } = render(
155+
<Menu
156+
mode="horizontal"
157+
items={[
158+
{
159+
label: '菜单一',
160+
key: '1',
161+
},
162+
{
163+
label: '菜单二',
164+
key: '',
165+
children: [
166+
{
167+
label: 'Navigation One1',
168+
key: 'mail1',
169+
},
170+
],
171+
},
172+
]}
173+
/>,
174+
);
175+
expect(container.querySelector('.rc-menu-submenu-active')).toBeFalsy();
176+
});
177+
153178
describe('openSubMenuOnMouseEnter and closeSubMenuOnMouseLeave are true', () => {
154179
it('toggles when mouse enter and leave', () => {
155180
const { container } = render(
@@ -496,8 +521,14 @@ describe('SubMenu', () => {
496521

497522
fireEvent.mouseEnter(container.querySelector('.rc-menu-submenu-title'));
498523
runAllTimer();
499-
expect((container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style.zIndex).toEqual('100');
500-
expect((container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style.width).toEqual('150px');
524+
expect(
525+
(container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style
526+
.zIndex,
527+
).toEqual('100');
528+
expect(
529+
(container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style
530+
.width,
531+
).toEqual('150px');
501532
});
502533
});
503534
/* eslint-enable */

0 commit comments

Comments
 (0)