From 8cc016ee38762ce78dcbf66c4b82849a8e3c4b2b Mon Sep 17 00:00:00 2001 From: Wilson Zeng Date: Fri, 23 Mar 2018 16:28:25 +0800 Subject: [PATCH] feat(module:modal): support triggerOk/triggerCancel to trigger nzOnOk/nzOnCancel manually (#1201) --- components/modal/doc/index.en-US.md | 2 ++ components/modal/doc/index.zh-CN.md | 4 +++- components/modal/nz-modal-ref.class.ts | 6 ++++++ components/modal/nz-modal.component.ts | 8 ++++++++ components/modal/nz-modal.spec.ts | 17 +++++++++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/components/modal/doc/index.en-US.md b/components/modal/doc/index.en-US.md index 70b7f4019d..490625ec59 100644 --- a/components/modal/doc/index.en-US.md +++ b/components/modal/doc/index.en-US.md @@ -110,6 +110,8 @@ The dialog created by the service method `NzModalService.xxx()` will return a `N | close() | Close (hide) the dialog. Note: When used for a dialog created as a service, this method will destroy the dialog directly (as with the destroy method) | | destroy() | Destroy the dialog. Note: Used only for dialogs created by the service (non-service created dialogs, this method only hides the dialog) | | getContentComponent() | Gets the Component instance in the contents of the dialog for `nzContent`. Note: When the dialog is not initialized (`ngOnInit` is not executed), this function will return `undefined` | +| triggerOk() | Manually trigger nzOnOk | +| triggerCancel() | Manually trigger nzOnCancel | #### ModalButtonOptions (used to customize the bottom button) diff --git a/components/modal/doc/index.zh-CN.md b/components/modal/doc/index.zh-CN.md index a8068a701a..9f42b064b7 100644 --- a/components/modal/doc/index.zh-CN.md +++ b/components/modal/doc/index.zh-CN.md @@ -108,7 +108,9 @@ constructor(modal: NzModalService) { | open() | 打开(显示)对话框。若对话框已销毁,则调用此函数将失效 | | close(result: any) | 关闭(隐藏)对话框。注:当用于以服务方式创建的对话框,此方法将直接 销毁 对话框(同destroy方法) | | destroy(result: any) | 销毁对话框。注:仅用于服务方式创建的对话框(非服务方式创建的对话框,此方法只会隐藏对话框) | -| getContentComponent() | 获取对话框内容中`nzContent`的Component实例instance。注:当对话框还未初始化完毕(`ngOnInit`未执行)时,此函数将返回`undefined` | +| getContentComponent() | 获取对话框内容中`nzContent`的Component实例instance。注:当对话框还未初始化完毕(`ngOnInit`未执行)时,此函数将返回`undefined` | +| triggerOk() | 手动触发nzOnOk | +| triggerCancel() | 手动触发nzOnCancel | #### ModalButtonOptions(用于自定义底部按钮) diff --git a/components/modal/nz-modal-ref.class.ts b/components/modal/nz-modal-ref.class.ts index fd5f0815e7..5cf98e006b 100644 --- a/components/modal/nz-modal-ref.class.ts +++ b/components/modal/nz-modal-ref.class.ts @@ -15,6 +15,12 @@ export abstract class NzModalRef { // tslint:disable-line:no-a abstract close(result?: R): void; abstract destroy(result?: R): void; + /** + * Trigger the nzOnOk/nzOnCancel by manual + */ + abstract triggerOk(): void; + abstract triggerCancel(): void; + // /** // * Return the ComponentRef of nzContent when specify nzContent as a Component // * Note: this method may return undefined if the Component has not ready yet. (it only available after Modal's ngOnInit) diff --git a/components/modal/nz-modal.component.ts b/components/modal/nz-modal.component.ts index cb8b01ea38..f0053fddba 100644 --- a/components/modal/nz-modal.component.ts +++ b/components/modal/nz-modal.component.ts @@ -202,6 +202,14 @@ export class NzModalComponent extends NzModalRef impleme this.close(result); } + triggerOk(): void { + this.onClickOkCancel('ok'); + } + + triggerCancel(): void { + this.onClickOkCancel('cancel'); + } + getInstance(): NzModalComponent { return this; } diff --git a/components/modal/nz-modal.spec.ts b/components/modal/nz-modal.spec.ts index 0ede87ee16..89390ae918 100644 --- a/components/modal/nz-modal.spec.ts +++ b/components/modal/nz-modal.spec.ts @@ -425,6 +425,23 @@ describe('NzModal', () => { tick(600); expect(modalService.openModals.length).toBe(1); })); + + it('should trigger nzOnOk/nzOnCancel', () => { + const spyOk = jasmine.createSpy('ok spy'); + const spyCancel = jasmine.createSpy('cancel spy'); + const modalRef: NzModalRef = modalService.create({ + nzOnOk: spyOk, + nzOnCancel: spyCancel + }); + + fixture.detectChanges(); + + modalRef.triggerOk(); + expect(spyOk).toHaveBeenCalled(); + + modalRef.triggerCancel(); + expect(spyCancel).toHaveBeenCalled(); + }); }); });