From ff41a7b84c308c5a4ea1ea59655ea744b317d0c5 Mon Sep 17 00:00:00 2001 From: WB01676250 Date: Tue, 25 Feb 2025 11:21:47 +0800 Subject: [PATCH 1/5] feat: menuRef --- src/Menu.tsx | 1 + src/interface.ts | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Menu.tsx b/src/Menu.tsx index edad0c5b..466cb48d 100644 --- a/src/Menu.tsx +++ b/src/Menu.tsx @@ -426,6 +426,7 @@ const Menu = React.forwardRef((props, ref) => { elementToFocus?.focus?.(options); } }, + activeItem: { activeKey, uuid }, }; }); diff --git a/src/interface.ts b/src/interface.ts index f47873d1..97ed4508 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -134,9 +134,15 @@ export type MenuRef = { */ focus: (options?: FocusOptions) => void; list: HTMLUListElement; + activeItem: { + activeKey: string; + uuid: string; + }; }; // ======================== Component ======================== export type ComponentType = 'submenu' | 'item' | 'group' | 'divider'; -export type Components = Partial>>; +export type Components = Partial< + Record> +>; From 0b0b6fb39b2036e073d7d76cbf3144d532d29345 Mon Sep 17 00:00:00 2001 From: WB01676250 Date: Tue, 25 Feb 2025 11:27:56 +0800 Subject: [PATCH 2/5] feat: menuRef --- src/interface.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface.ts b/src/interface.ts index 97ed4508..5c198ac0 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -136,7 +136,7 @@ export type MenuRef = { list: HTMLUListElement; activeItem: { activeKey: string; - uuid: string; + uuid1: string; }; }; From ba55c5b870c8be2b1b2968f8173366420c2cf442 Mon Sep 17 00:00:00 2001 From: WB01676250 Date: Tue, 25 Feb 2025 11:34:23 +0800 Subject: [PATCH 3/5] feat: menuRef --- src/interface.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface.ts b/src/interface.ts index 5c198ac0..97ed4508 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -136,7 +136,7 @@ export type MenuRef = { list: HTMLUListElement; activeItem: { activeKey: string; - uuid1: string; + uuid: string; }; }; From fc70584bb5a799e33e04f1014870965990f11749 Mon Sep 17 00:00:00 2001 From: WB01676250 Date: Wed, 26 Feb 2025 09:55:51 +0800 Subject: [PATCH 4/5] feat:menuRef --- src/Menu.tsx | 5 +++++ src/interface.ts | 1 + tests/Menu.spec.tsx | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Menu.tsx b/src/Menu.tsx index 466cb48d..86c54465 100644 --- a/src/Menu.tsx +++ b/src/Menu.tsx @@ -427,6 +427,11 @@ const Menu = React.forwardRef((props, ref) => { } }, activeItem: { activeKey, uuid }, + findItem: ({ key: itemKey }) => { + const keys = getKeys(); + const { key2element } = refreshElements(keys, uuid); + return key2element.get(itemKey) || null; + }, }; }); diff --git a/src/interface.ts b/src/interface.ts index 97ed4508..9a82659c 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -138,6 +138,7 @@ export type MenuRef = { activeKey: string; uuid: string; }; + findItem: (params: { key: string }) => HTMLElement | null; }; // ======================== Component ======================== diff --git a/tests/Menu.spec.tsx b/tests/Menu.spec.tsx index fc7326ef..76318b9a 100644 --- a/tests/Menu.spec.tsx +++ b/tests/Menu.spec.tsx @@ -333,7 +333,7 @@ describe('Menu', () => { // don't use selectedKeys as string // it is a compatible feature for https://github.com/ant-design/ant-design/issues/29429 const { container } = render( - + 1 2 , @@ -807,5 +807,23 @@ describe('Menu', () => { rerender(); expect(container.querySelectorAll('.rc-menu-submenu-arrow').length).toBe(1); }); + + it('should find item by key', () => { + const menuRef = React.createRef(); + const { container } = render( + + Light + Cat + , + ); + + const lightItem = menuRef.current?.findItem({ key: 'light' }); + const catItem = menuRef.current?.findItem({ key: 'cat' }); + const nonExistentItem = menuRef.current?.findItem({ key: 'dog' }); + + expect(lightItem).toBe(container.querySelectorAll('li')[0]); + expect(catItem).toBe(container.querySelectorAll('li')[1]); + expect(nonExistentItem).toBe(null); + }); }); /* eslint-enable */ From 969833ee186d485ee32eed9a0124f73c19d96d30 Mon Sep 17 00:00:00 2001 From: WB01676250 Date: Wed, 26 Feb 2025 09:59:04 +0800 Subject: [PATCH 5/5] feat:menuRef --- src/Menu.tsx | 1 - src/interface.ts | 4 ---- 2 files changed, 5 deletions(-) diff --git a/src/Menu.tsx b/src/Menu.tsx index 86c54465..4f53db97 100644 --- a/src/Menu.tsx +++ b/src/Menu.tsx @@ -426,7 +426,6 @@ const Menu = React.forwardRef((props, ref) => { elementToFocus?.focus?.(options); } }, - activeItem: { activeKey, uuid }, findItem: ({ key: itemKey }) => { const keys = getKeys(); const { key2element } = refreshElements(keys, uuid); diff --git a/src/interface.ts b/src/interface.ts index 9a82659c..f44b9eb6 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -134,10 +134,6 @@ export type MenuRef = { */ focus: (options?: FocusOptions) => void; list: HTMLUListElement; - activeItem: { - activeKey: string; - uuid: string; - }; findItem: (params: { key: string }) => HTMLElement | null; };