Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ describe('DropdownMenuDirective', () => {
})
class SpaceTestComponent {}

@Component({
template: `
<c-dropdown visible>
<button cDropdownToggle>Dropdown button</button>
<ul cDropdownMenu>
<li><button cDropdownItem id="first">First</button></li>
<li><div id="widget" tabindex="0" (keydown)="handle($event)"></div></li>
<li><button cDropdownItem id="last">Last</button></li>
</ul>
</c-dropdown>
`,
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);
Expand Down Expand Up @@ -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));
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
Loading