Skip to content

Commit

Permalink
fix(modal): don't close modal on ESC if file dialog is open (#3455)
Browse files Browse the repository at this point in the history
Keyboard events in modal and autoclose handling use 'keydown' instead of 'keyup'

Fixes #3439
  • Loading branch information
maxokorokov committed Nov 8, 2019
1 parent c08e3b4 commit 5977dcb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/modal/modal-window.spec.ts
Expand Up @@ -107,7 +107,7 @@ describe('ngb-modal-dialog', () => {
done();
});

fixture.nativeElement.dispatchEvent(createKeyEvent(Key.Escape));
fixture.nativeElement.dispatchEvent(createKeyEvent(Key.Escape, {type: 'keydown'}));
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal-window.ts
Expand Up @@ -55,7 +55,7 @@ export class NgbModalWindow implements OnInit,
constructor(
@Inject(DOCUMENT) private _document: any, private _elRef: ElementRef<HTMLElement>, private _zone: NgZone) {
_zone.runOutsideAngular(() => {
fromEvent<KeyboardEvent>(this._elRef.nativeElement, 'keyup')
fromEvent<KeyboardEvent>(this._elRef.nativeElement, 'keydown')
.pipe(
takeUntil(this.dismissEvent),
// tslint:disable-next-line:deprecation
Expand Down
8 changes: 4 additions & 4 deletions src/modal/modal.spec.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/util/autoclose.ts
Expand Up @@ -43,7 +43,7 @@ export function ngbAutoClose(
}
};

const escapes$ = fromEvent<KeyboardEvent>(document, 'keyup')
const escapes$ = fromEvent<KeyboardEvent>(document, 'keydown')
.pipe(
takeUntil(closed$),
// tslint:disable-next-line:deprecation
Expand Down

0 comments on commit 5977dcb

Please sign in to comment.