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 e207ed4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ComponentRef,
ElementRef,
EventEmitter,
HostListener,
Inject,
Injector,
Input,
Expand All @@ -24,7 +23,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 +110,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 +142,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 +199,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
4 changes: 2 additions & 2 deletions components/modal/nz-modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ describe('modal testing (legacy)', () => {

it('should be closed when clicking ESC', fakeAsync(() => {
// click 'ESC' key
dispatchKeyboardEvent(modalElement, 'keydown', ESCAPE);
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
expectModalDestroyed(tempModalId, false);

modalInstance.nzKeyboard = true;
dispatchKeyboardEvent(modalElement, 'keydown', ESCAPE);
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
flush();
fixture.detectChanges();
expectModalDestroyed(tempModalId, true);
Expand Down

0 comments on commit e207ed4

Please sign in to comment.