From 5977dcb0699b791045ec3b2d06ed3310d5d5c4ec Mon Sep 17 00:00:00 2001 From: Max Okorokov Date: Fri, 8 Nov 2019 11:56:50 +0100 Subject: [PATCH] fix(modal): don't close modal on ESC if file dialog is open (#3455) Keyboard events in modal and autoclose handling use 'keydown' instead of 'keyup' Fixes #3439 --- src/modal/modal-window.spec.ts | 2 +- src/modal/modal-window.ts | 2 +- src/modal/modal.spec.ts | 8 ++++---- src/util/autoclose.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modal/modal-window.spec.ts b/src/modal/modal-window.spec.ts index e859257170..52446829b7 100644 --- a/src/modal/modal-window.spec.ts +++ b/src/modal/modal-window.spec.ts @@ -107,7 +107,7 @@ describe('ngb-modal-dialog', () => { done(); }); - fixture.nativeElement.dispatchEvent(createKeyEvent(Key.Escape)); + fixture.nativeElement.dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'})); }); }); diff --git a/src/modal/modal-window.ts b/src/modal/modal-window.ts index d1106e940c..59a57f5187 100644 --- a/src/modal/modal-window.ts +++ b/src/modal/modal-window.ts @@ -55,7 +55,7 @@ export class NgbModalWindow implements OnInit, constructor( @Inject(DOCUMENT) private _document: any, private _elRef: ElementRef, private _zone: NgZone) { _zone.runOutsideAngular(() => { - fromEvent(this._elRef.nativeElement, 'keyup') + fromEvent(this._elRef.nativeElement, 'keydown') .pipe( takeUntil(this.dismissEvent), // tslint:disable-next-line:deprecation diff --git a/src/modal/modal.spec.ts b/src/modal/modal.spec.ts index 6d863ad46b..88787907f5 100644 --- a/src/modal/modal.spec.ts +++ b/src/modal/modal.spec.ts @@ -353,13 +353,13 @@ describe('ngb-modal', () => { expect(fixture.nativeElement).toHaveModal(['foo', 'bar']); expect(document.activeElement).toBe(ngbModalWindow2); - ngbModalWindow2.dispatchEvent(createKeyEvent(Key.Escape)); + ngbModalWindow2.dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'})); tick(16); // RAF in escape handling fixture.detectChanges(); expect(fixture.nativeElement).toHaveModal(['foo']); expect(document.activeElement).toBe(ngbModalWindow1); - ngbModalWindow1.dispatchEvent(createKeyEvent(Key.Escape)); + ngbModalWindow1.dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'})); tick(16); // RAF in escape handling fixture.detectChanges(); expect(fixture.nativeElement).not.toHaveModal(); @@ -580,7 +580,7 @@ describe('ngb-modal', () => { fixture.detectChanges(); expect(fixture.nativeElement).toHaveModal('foo'); - document.querySelector('ngb-modal-window').dispatchEvent(createKeyEvent(Key.Escape)); + document.querySelector('ngb-modal-window').dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'})); tick(16); // RAF in escape handling fixture.detectChanges(); expect(fixture.nativeElement).not.toHaveModal(); @@ -591,7 +591,7 @@ describe('ngb-modal', () => { fixture.detectChanges(); expect(fixture.nativeElement).toHaveModal('foo'); - document.querySelector('ngb-modal-window').dispatchEvent(createKeyEvent(Key.Escape)); + document.querySelector('ngb-modal-window').dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'})); tick(16); // RAF in escape handling fixture.detectChanges(); expect(fixture.nativeElement).toHaveModal(); diff --git a/src/util/autoclose.ts b/src/util/autoclose.ts index cccc1b60f9..01c1eef54d 100644 --- a/src/util/autoclose.ts +++ b/src/util/autoclose.ts @@ -43,7 +43,7 @@ export function ngbAutoClose( } }; - const escapes$ = fromEvent(document, 'keyup') + const escapes$ = fromEvent(document, 'keydown') .pipe( takeUntil(closed$), // tslint:disable-next-line:deprecation