Skip to content

Commit bda1bd9

Browse files
authored
fix(OverflowMenu): ensure onClose is only called once (#20486)
* fix(OverflowMenu): ensure onClose is only called once * test: add test * chore: remove test story
1 parent c76188a commit bda1bd9

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

packages/react/src/components/OverflowMenu/OverflowMenu-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ describe('OverflowMenu', () => {
117117
expect(onClose).toHaveBeenCalled();
118118
});
119119

120+
it('should call onClose only once when menu is closed', async () => {
121+
const onClose = jest.fn();
122+
render(
123+
<OverflowMenu
124+
aria-label="Overflow menu"
125+
className="extra-class"
126+
onClose={onClose}>
127+
<OverflowMenuItem className="test-child" itemText="one" />
128+
<OverflowMenuItem className="test-child" itemText="two" />
129+
</OverflowMenu>
130+
);
131+
132+
await userEvent.click(screen.getByRole('button'));
133+
await userEvent.click(screen.getByText('one'));
134+
await waitFor(() => {
135+
expect(onClose).toHaveBeenCalledTimes(1);
136+
});
137+
});
138+
120139
it('should call onFocus', async () => {
121140
const onFocus = jest.fn();
122141
render(

packages/react/src/components/OverflowMenu/OverflowMenu.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,13 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
342342
}
343343
}, []);
344344

345-
const closeMenu = useCallback(
346-
(onCloseMenu?: () => void) => {
347-
setOpen(false);
348-
// Optional callback to be executed after the state as been set to close
349-
if (onCloseMenu) {
350-
onCloseMenu();
351-
}
352-
onClose();
353-
},
354-
[onClose]
355-
);
345+
const closeMenu = useCallback((onCloseMenu?: () => void) => {
346+
setOpen(false);
347+
// Optional callback to be executed after the state as been set to close
348+
if (onCloseMenu) {
349+
onCloseMenu();
350+
}
351+
}, []);
356352

357353
const closeMenuAndFocus = useCallback(() => {
358354
const wasClicked = click;

0 commit comments

Comments
 (0)