Skip to content

Commit

Permalink
fix: move listener on document
Browse files Browse the repository at this point in the history
  • Loading branch information
wenqi73 committed Nov 27, 2018
1 parent 866cba0 commit 6b4eec1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
ViewContainerRef
} from '@angular/core';

import { Observable, Subject } from 'rxjs';
import { fromEvent, Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NzMeasureScrollbarService } from '../core/services/nz-measure-scrollbar.service';
Expand Down Expand Up @@ -111,12 +111,6 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
@ViewChild('bodyContainer', { read: ViewContainerRef }) bodyContainer: ViewContainerRef;

@Input() @InputBoolean() nzKeyboard: boolean = true;
@HostListener('keydown', [ '$event' ])
public onKeyDown(event: KeyboardEvent): void {
if (event.keyCode === ESCAPE && this.nzKeyboard) {
this.onClickOkCancel('cancel');
}
}

get hidden(): boolean {
return !this.nzVisible && !this.animationState;
Expand Down Expand Up @@ -149,6 +143,7 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme

ngOnInit(): void {
this.i18n.localeChange.pipe(takeUntil(this.unsubscribe$)).subscribe(() => this.locale = this.i18n.getLocaleData('Modal'));
fromEvent(this.document.body, 'keydown').pipe(takeUntil(this.unsubscribe$)).subscribe((e: KeyboardEvent) => this.keydownListener(e));

if (this.isComponent(this.nzContent)) {
this.createDynamicComponent(this.nzContent as Type<T>); // Create component along without View
Expand Down Expand Up @@ -205,6 +200,12 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
});
}

keydownListener(event: KeyboardEvent): void {
if (event.keyCode === ESCAPE && this.nzKeyboard) {
this.onClickOkCancel('cancel');
}
}

open(): void {
this.changeVisibleFromInside(true);
}
Expand Down

0 comments on commit 6b4eec1

Please sign in to comment.