diff --git a/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.spec.ts b/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.spec.ts index a269833c..0488ce3a 100644 --- a/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.spec.ts +++ b/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.spec.ts @@ -136,6 +136,29 @@ describe('DropdownMenuDirective', () => { }) class SpaceTestComponent {} +@Component({ + template: ` + + + + + `, + imports: [DropdownComponent, DropdownMenuDirective, DropdownItemDirective, DropdownToggleDirective] +}) +class WidgetTestComponent { + handles = true; + + handle($event: KeyboardEvent): void { + if (this.handles) { + $event.preventDefault(); + } + } +} + describe('DropdownMenuDirective Space', () => { it('should prevent page scroll on Space only for targets without native Space handling', async () => { const fixture = TestBed.createComponent(SpaceTestComponent); @@ -186,4 +209,39 @@ describe('DropdownMenuDirective Space', () => { expect(document.activeElement).toBe(field); } }); + + it('should leave a key that a widget inside the menu already handled', async () => { + const fixture = TestBed.createComponent(WidgetTestComponent); + await fixture.whenStable(); + const widget: HTMLElement = fixture.nativeElement.querySelector('#widget'); + widget.focus(); + for (const [key, keyCode] of [ + ['ArrowDown', 40], + ['ArrowUp', 38], + ['Home', 36], + ['End', 35], + ['PageUp', 33], + ['PageDown', 34] + ] as const) { + widget.dispatchEvent(new KeyboardEvent('keydown', { key, code: key, keyCode, bubbles: true, cancelable: true })); + expect(document.activeElement).toBe(widget); + } + }); + + for (const [key, keyCode, target] of [ + ['ArrowDown', 40, '#first'], + ['ArrowUp', 38, '#last'], + ['Home', 36, '#first'], + ['End', 35, '#last'] + ] as const) { + it(`should move with ${key} from a widget inside the menu that leaves the key unhandled`, async () => { + const fixture = TestBed.createComponent(WidgetTestComponent); + fixture.componentInstance.handles = false; + await fixture.whenStable(); + const widget: HTMLElement = fixture.nativeElement.querySelector('#widget'); + widget.focus(); + widget.dispatchEvent(new KeyboardEvent('keydown', { key, code: key, keyCode, bubbles: true, cancelable: true })); + expect(document.activeElement).toBe(fixture.nativeElement.querySelector(target)); + }); + } }); diff --git a/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.ts b/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.ts index 9ffc7973..250562f4 100644 --- a/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.ts +++ b/projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.ts @@ -84,7 +84,7 @@ export class DropdownMenuDirective implements OnInit, AfterContentInit { readonly dataPopper = computed(() => (this.#dropdownService.popper() ? null : 'static')); onKeyDown($event: KeyboardEvent): void { - if (!this.visible() || isReplayedEvent($event) || isEditableTarget($event.target)) { + if (!this.visible() || $event.defaultPrevented || isReplayedEvent($event) || isEditableTarget($event.target)) { return; } if ($event.code === 'ArrowDown' || ($event.code === 'Space' && !this.#clicksOnSpace($event.target))) {