Skip to content

Commit

Permalink
fix(angular/autocomplete): don't handle enter events with modifier ke…
Browse files Browse the repository at this point in the history
…ys (#1029)

Doesn't handle `ENTER` key presses and doesn't prevent their default action,
if they have a modifier, in order to avoid interfering with any OS-level shortcuts.
angular/components#14717
  • Loading branch information
jeripeierSBB authored Jan 17, 2022
1 parent 939eb69 commit 657cdb3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/angular/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export class SbbAutocompleteTrigger
event.preventDefault();
}

if (this.activeOption && keyCode === ENTER && this.panelOpen) {
if (this.activeOption && keyCode === ENTER && this.panelOpen && !hasModifierKey(event)) {
this.activeOption._selectViaInteraction();
this._resetActiveItem();
event.preventDefault();
Expand Down
22 changes: 22 additions & 0 deletions src/angular/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,28 @@ describe('SbbAutocomplete', () => {
expect(enterEvent.defaultPrevented).toBe(false, 'Default action should not be prevented.');
});

it('should not interfere with the ENTER key when pressing a modifier', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

expect(input.value).toBeFalsy('Expected input to start off blank.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to start off open.');

fixture.componentInstance.trigger._handleKeydown(downArrowEvent);
flush();
fixture.detectChanges();

Object.defineProperty(enterEvent, 'altKey', { get: () => true });
fixture.componentInstance.trigger._handleKeydown(enterEvent);
fixture.detectChanges();

expect(trigger.panelOpen).toBe(true, 'Expected panel to remain open.');
expect(input.value).toBeFalsy('Expected input to remain blank.');
expect(enterEvent.defaultPrevented).toBe(
false,
'Expected the default ENTER action not to have been prevented.'
);
}));

it('should fill the text field, not select an option, when SPACE is entered', () => {
typeInElement(input, 'New');
fixture.detectChanges();
Expand Down

0 comments on commit 657cdb3

Please sign in to comment.