Skip to content

Commit 2294b3d

Browse files
authored
fix: the interaction bug of MenuBar in horizontal mode (#647)
* fix: the interaction bug of MenuBar in horizontal mode * test: update unit test code
1 parent 4b7d8de commit 2294b3d

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/workbench/menuBar/__tests__/menubar.test.tsx

+30-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('Test MenuBar Component', () => {
124124
expect(mockFn).toBeCalled();
125125
});
126126

127-
test('Should support to execute the handleClickMenuBar method in HorizontalView', async () => {
127+
test('Should support to hide the menu by clicking the menu item at the root', async () => {
128128
const { getByText } = render(
129129
<MenuBar
130130
data={menuData}
@@ -153,6 +153,35 @@ describe('Test MenuBar Component', () => {
153153
document.elementsFromPoint = originalFunc;
154154
});
155155

156+
test('Should support to hide menu by clicking the menu item of the submenu', async () => {
157+
const { getByText } = render(
158+
<MenuBar
159+
data={menuData}
160+
onClick={TEST_FN}
161+
mode={MenuBarMode.horizontal}
162+
/>
163+
);
164+
const elem = getByText(TEST_ID);
165+
const liElem = elem.closest('li');
166+
const elemArr = liElem ? [liElem] : [];
167+
const spanElem = getByText(TEST_DATA);
168+
const ulElem = spanElem.closest('ul');
169+
const originalFunc = document.elementsFromPoint;
170+
document.elementsFromPoint = jest.fn(() => elemArr);
171+
172+
fireEvent.click(elem);
173+
await waitFor(() => {
174+
expect(ulElem?.style.opacity).toBe('1');
175+
});
176+
177+
fireEvent.click(spanElem);
178+
await waitFor(() => {
179+
expect(ulElem?.style.opacity).toBe('0');
180+
});
181+
182+
document.elementsFromPoint = originalFunc;
183+
});
184+
156185
test('Should support to execute the clearAutoDisplay method in HorizontalView', async () => {
157186
const { getByText } = render(
158187
<MenuBar

src/workbench/menuBar/horizontalView.tsx

+20-10
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,32 @@ export function HorizontalView(props: IHorizontalViewProps) {
4545

4646
const handleClickMenuBar = (e: MouseEvent) => {
4747
const isRootLiElem = checkIsRootLiElem(e);
48-
if (!isRootLiElem) return;
49-
if (autoDisplayMenu) {
50-
e.preventDefault();
51-
e.stopPropagation();
52-
menuRef.current?.dispose?.();
48+
const target = e.target as HTMLElement;
49+
50+
if (isRootLiElem) {
51+
if (autoDisplayMenu) {
52+
menuRef.current?.dispose?.();
53+
}
54+
// Delay the execution of setAutoDisplayMenu to ensure that the menu can be displayed.
55+
setTimeout(() => setAutoDisplayMenu(!autoDisplayMenu));
56+
} else {
57+
const liElem = target.closest('li');
58+
const isNormalLiElem =
59+
liElem &&
60+
menuBarElem.contains(liElem) &&
61+
!liElem?.dataset.submenu;
62+
63+
if (!liElem || isNormalLiElem) {
64+
setAutoDisplayMenu(false);
65+
}
5366
}
54-
// Delay the execution of setAutoDisplayMenu to ensure that the menu can be displayed.
55-
setTimeout(() => setAutoDisplayMenu(!autoDisplayMenu));
5667
};
5768

5869
const clearAutoDisplay = (e: MouseEvent) => {
5970
if (!autoDisplayMenu) return;
60-
const isRootLiElem = checkIsRootLiElem(e);
71+
6172
const target = e.target as HTMLElement;
62-
const liElem = target.closest('li');
63-
if (!isRootLiElem && !liElem?.dataset.submenu) {
73+
if (!menuBarElem.contains(target)) {
6474
setAutoDisplayMenu(false);
6575
}
6676
};

0 commit comments

Comments
 (0)