Skip to content

Commit

Permalink
feat(module:modal): support triggerOk/triggerCancel to trigger nzOnOk…
Browse files Browse the repository at this point in the history
…/nzOnCancel manually (NG-ZORRO#1201)
  • Loading branch information
wilsoncook authored Mar 23, 2018
1 parent b9cbcc3 commit 7a75e92
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions components/modal/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ The dialog created by the service method `NzModalService.xxx()` will return a `N
| close() | Close (hide) the dialog. <i>Note: When used for a dialog created as a service, this method will destroy the dialog directly (as with the destroy method)</i> |
| destroy() | Destroy the dialog. <i>Note: Used only for dialogs created by the service (non-service created dialogs, this method only hides the dialog)</i> |
| getContentComponent() | Gets the Component instance in the contents of the dialog for `nzContent`. <i> Note: When the dialog is not initialized (`ngOnInit` is not executed), this function will return `undefined`</i> |
| triggerOk() | Manually trigger nzOnOk |
| triggerCancel() | Manually trigger nzOnCancel |

#### ModalButtonOptions (used to customize the bottom button)

Expand Down
4 changes: 3 additions & 1 deletion components/modal/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ constructor(modal: NzModalService) {
| open() | 打开(显示)对话框。<i>若对话框已销毁,则调用此函数将失效</i> |
| close(result: any) | 关闭(隐藏)对话框。<i>注:当用于以服务方式创建的对话框,此方法将直接 销毁 对话框(同destroy方法)</i> |
| destroy(result: any) | 销毁对话框。<i>注:仅用于服务方式创建的对话框(非服务方式创建的对话框,此方法只会隐藏对话框)</i> |
| getContentComponent() | 获取对话框内容中`nzContent`的Component实例instance。<i>注:当对话框还未初始化完毕(`ngOnInit`未执行)时,此函数将返回`undefined`</i> |
| getContentComponent() | 获取对话框内容中`nzContent`的Component实例instance。<i>注:当对话框还未初始化完毕(`ngOnInit`未执行)时,此函数将返回`undefined`</i> |
| triggerOk() | 手动触发nzOnOk |
| triggerCancel() | 手动触发nzOnCancel |

#### ModalButtonOptions(用于自定义底部按钮)

Expand Down
6 changes: 6 additions & 0 deletions components/modal/nz-modal-ref.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export abstract class NzModalRef<T = any, R = any> { // 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)
Expand Down
8 changes: 8 additions & 0 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
this.close(result);
}

triggerOk(): void {
this.onClickOkCancel('ok');
}

triggerCancel(): void {
this.onClickOkCancel('cancel');
}

getInstance(): NzModalComponent {
return this;
}
Expand Down
17 changes: 17 additions & 0 deletions components/modal/nz-modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down

0 comments on commit 7a75e92

Please sign in to comment.