Skip to content

Commit

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

Fixes ng-bootstrap#3439
  • Loading branch information
maxokorokov committed Nov 7, 2019
1 parent 9a92667 commit c3253a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
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 @@ -39,7 +39,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 c3253a3

Please sign in to comment.