Skip to content

Commit a68d01e

Browse files
authored
Merge pull request #17135 from IgniteUI/ganastasov/combo-escape-close-21.1.x
fix(combo/simple-combo): prevent Escape from closing parent container on clear - 21.1.x
2 parents f4d0c39 + e816e68 commit a68d01e

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

projects/igniteui-angular/combo/src/combo/combo.component.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,40 @@ describe('igxCombo', () => {
18661866
expect(document.activeElement).toEqual(combo.comboInput.nativeElement);
18671867
expect(combo.selection.length).toEqual(0);
18681868
}));
1869+
it('should stop Escape keydown event propagation when the dropdown is open', fakeAsync(() => {
1870+
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
1871+
spyOn(escapeEvent, 'stopPropagation');
1872+
1873+
combo.comboInput.nativeElement.focus();
1874+
fixture.detectChanges();
1875+
1876+
combo.toggle();
1877+
fixture.detectChanges();
1878+
expect(combo.collapsed).toBeFalsy();
1879+
1880+
combo.onEscape(escapeEvent);
1881+
tick();
1882+
fixture.detectChanges();
1883+
1884+
expect(escapeEvent.stopPropagation).toHaveBeenCalled();
1885+
}));
1886+
it('should stop Escape key propagation when the combo is collapsed and has a selection', fakeAsync(() => {
1887+
combo.comboInput.nativeElement.focus();
1888+
fixture.detectChanges();
1889+
1890+
combo.select([combo.data[0][combo.valueKey]]);
1891+
expect(combo.selection.length).toEqual(1);
1892+
fixture.detectChanges();
1893+
1894+
const keyEvent = new KeyboardEvent('keydown', { key: 'Escape' });
1895+
const stopPropSpy = spyOn(keyEvent, 'stopPropagation');
1896+
1897+
combo.onEscape(keyEvent);
1898+
tick();
1899+
fixture.detectChanges();
1900+
1901+
expect(stopPropSpy).toHaveBeenCalledTimes(1);
1902+
}));
18691903
it('should close the combo and preserve the focus when Escape key is pressed', fakeAsync(() => {
18701904
combo.comboInput.nativeElement.focus();
18711905
fixture.detectChanges();

projects/igniteui-angular/combo/src/combo/combo.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export class IgxComboComponent extends IgxComboBaseDirective implements AfterVie
187187

188188
@HostListener('keydown.Escape', ['$event'])
189189
public onEscape(event: Event) {
190+
event.stopPropagation();
190191
if (this.collapsed) {
191192
this.deselectAllItems(true, event);
192193
}

projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,38 @@ describe('IgxSimpleCombo', () => {
11341134
expect(combo.selection).not.toBeDefined();
11351135
}));
11361136

1137+
it('should stop Escape keydown event propagation when the dropdown is open', fakeAsync(() => {
1138+
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
1139+
spyOn(escapeEvent, 'stopPropagation');
1140+
1141+
combo.open();
1142+
fixture.detectChanges();
1143+
expect(combo.collapsed).toBeFalsy();
1144+
1145+
combo.handleKeyDown(escapeEvent);
1146+
tick();
1147+
fixture.detectChanges();
1148+
1149+
expect(escapeEvent.stopPropagation).toHaveBeenCalled();
1150+
}));
1151+
1152+
it('should stop Escape key propagation when the combo is collapsed and has a selection', fakeAsync(() => {
1153+
combo.comboInput.nativeElement.focus();
1154+
fixture.detectChanges();
1155+
1156+
combo.select(combo.data[2][combo.valueKey]);
1157+
fixture.detectChanges();
1158+
expect(combo.selection).toBeDefined();
1159+
1160+
const keyEvent = new KeyboardEvent('keydown', { key: 'Escape' });
1161+
const stopPropSpy = spyOn(keyEvent, 'stopPropagation');
1162+
1163+
combo.handleKeyDown(keyEvent);
1164+
fixture.detectChanges();
1165+
1166+
expect(stopPropSpy).toHaveBeenCalledTimes(1);
1167+
}));
1168+
11371169
it('should clear the selection on tab/blur if the search text does not match any value', () => {
11381170
// allowCustomValues does not matter
11391171
combo.select(combo.data[2][combo.valueKey]);

projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
350350
}
351351
}
352352
if (event.key === this.platformUtil.KEYMAP.ESCAPE) {
353+
event.stopPropagation();
353354
if (this.collapsed) {
354355
const oldSelection = this.selection;
355356
this.clearSelection(true);

0 commit comments

Comments
 (0)