diff --git a/CHANGELOG.md b/CHANGELOG.md index fefb80c..59e2e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fix + +- Prevent several instances of a sub menu when opening with the mouse + ## [17.0.0] - 2023-11-10 ### BREAKING CHANGES diff --git a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts index 4f1a1e9..42f69e0 100644 --- a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts +++ b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts @@ -903,4 +903,49 @@ describe('Component: ContextMenuContentComponent', () => { ).not.toHaveBeenCalled(); }); }); + + describe('#openSubMenu', () => { + beforeEach(() => { + configureTestingModule(); + component.contextMenuContentItems = new QueryList(); + }); + + it('should show subMenu', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + + spyOn(subMenu, 'show'); + component.openSubMenu(subMenu, event); + + expect(subMenu.show).toHaveBeenCalledWith({ + anchoredTo: 'position', + x: 0, + y: 0, + value: undefined, + }); + }); + + it('should not show subMenu if already opened', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + spyOnProperty(subMenu, 'isOpen', 'get').and.returnValue(true); + spyOn(subMenu, 'show'); + component.openSubMenu(subMenu, event); + + expect(subMenu.show).not.toHaveBeenCalled(); + }); + + it('should not throw if subMenu does not exist', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + + spyOn(subMenu, 'show'); + expect(() => { + component.openSubMenu(undefined, event); + }).not.toThrow(); + }); + }); }); diff --git a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts index 12fc982..e4db2e7 100644 --- a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts +++ b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts @@ -242,7 +242,7 @@ export class ContextMenuContentComponent subMenu: ContextMenuComponent | undefined, event: MouseEvent | KeyboardEvent ): void { - if (!subMenu) { + if (!subMenu || subMenu.isOpen) { return; }