Skip to content

Commit f00a27f

Browse files
authored
Merge pull request #17134 from IgniteUI/ganastasov/combo-escape-close-master
fix(combo/simple-combo): prevent Escape from closing parent container on clear - master
2 parents c2c1b8b + ecf043f commit f00a27f

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,40 @@ describe('igxCombo', () => {
19421942
expect(document.activeElement).toEqual(combo.comboInput.nativeElement);
19431943
expect(combo.selection.length).toEqual(0);
19441944
}));
1945+
it('should stop Escape keydown event propagation when the dropdown is open', fakeAsync(() => {
1946+
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
1947+
spyOn(escapeEvent, 'stopPropagation');
1948+
1949+
combo.comboInput.nativeElement.focus();
1950+
fixture.detectChanges();
1951+
1952+
combo.toggle();
1953+
fixture.detectChanges();
1954+
expect(combo.collapsed).toBeFalsy();
1955+
1956+
combo.onEscape(escapeEvent);
1957+
tick();
1958+
fixture.detectChanges();
1959+
1960+
expect(escapeEvent.stopPropagation).toHaveBeenCalled();
1961+
}));
1962+
it('should stop Escape key propagation when the combo is collapsed and has a selection', fakeAsync(() => {
1963+
combo.comboInput.nativeElement.focus();
1964+
fixture.detectChanges();
1965+
1966+
combo.select([combo.data[0][combo.valueKey]]);
1967+
expect(combo.selection.length).toEqual(1);
1968+
fixture.detectChanges();
1969+
1970+
const keyEvent = new KeyboardEvent('keydown', { key: 'Escape' });
1971+
const stopPropSpy = spyOn(keyEvent, 'stopPropagation');
1972+
1973+
combo.onEscape(keyEvent);
1974+
tick();
1975+
fixture.detectChanges();
1976+
1977+
expect(stopPropSpy).toHaveBeenCalledTimes(1);
1978+
}));
19451979
it('should close the combo and preserve the focus when Escape key is pressed', fakeAsync(() => {
19461980
combo.comboInput.nativeElement.focus();
19471981
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
@@ -199,6 +199,7 @@ export class IgxComboComponent extends IgxComboBaseDirective implements AfterVie
199199

200200
@HostListener('keydown.Escape', ['$event'])
201201
public onEscape(event: Event) {
202+
event.stopPropagation();
202203
if (this.collapsed) {
203204
this.deselectAllItems(true, event);
204205
}

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
@@ -1220,6 +1220,38 @@ describe('IgxSimpleCombo', () => {
12201220
expect(combo.selection).not.toBeDefined();
12211221
}));
12221222

1223+
it('should stop Escape keydown event propagation when the dropdown is open', fakeAsync(() => {
1224+
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
1225+
spyOn(escapeEvent, 'stopPropagation');
1226+
1227+
combo.open();
1228+
fixture.detectChanges();
1229+
expect(combo.collapsed).toBeFalsy();
1230+
1231+
combo.handleKeyDown(escapeEvent);
1232+
tick();
1233+
fixture.detectChanges();
1234+
1235+
expect(escapeEvent.stopPropagation).toHaveBeenCalled();
1236+
}));
1237+
1238+
it('should stop Escape key propagation when the combo is collapsed and has a selection', fakeAsync(() => {
1239+
combo.comboInput.nativeElement.focus();
1240+
fixture.detectChanges();
1241+
1242+
combo.select(combo.data[2][combo.valueKey]);
1243+
fixture.detectChanges();
1244+
expect(combo.selection).toBeDefined();
1245+
1246+
const keyEvent = new KeyboardEvent('keydown', { key: 'Escape' });
1247+
const stopPropSpy = spyOn(keyEvent, 'stopPropagation');
1248+
1249+
combo.handleKeyDown(keyEvent);
1250+
fixture.detectChanges();
1251+
1252+
expect(stopPropSpy).toHaveBeenCalledTimes(1);
1253+
}));
1254+
12231255
it('should clear the selection on tab/blur if the search text does not match any value', () => {
12241256
// allowCustomValues does not matter
12251257
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
@@ -372,6 +372,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
372372
}
373373
}
374374
if (event.key === this.platformUtil.KEYMAP.ESCAPE) {
375+
event.stopPropagation();
375376
if (this.collapsed) {
376377
const oldSelection = this.selection;
377378
this.clearSelection(true);

0 commit comments

Comments
 (0)