From f3ff21eca61665ace49b93a13e74234ed02496e1 Mon Sep 17 00:00:00 2001 From: liuchao233 <36943472+liuchao233@users.noreply.github.com> Date: Mon, 7 Dec 2020 13:44:06 +0800 Subject: [PATCH] feat: Modal.method support afterClose (#28053) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Modal.method support afterClose * Update index.en-US.md * Update index.zh-CN.md * docs(dropdown): improve demo (#28032) * Modal.method support afterClose * add afterClose test case * resolve conflix * Update index.en-US.md Co-authored-by: 偏右 Co-authored-by: Tom Xu --- components/modal/Modal.tsx | 1 + components/modal/__tests__/confirm.test.js | 20 ++++++++++++++++++++ components/modal/confirm.tsx | 7 ++++++- components/modal/index.en-US.md | 1 + components/modal/index.zh-CN.md | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/components/modal/Modal.tsx b/components/modal/Modal.tsx index 5d43d01feb..713ed053ac 100644 --- a/components/modal/Modal.tsx +++ b/components/modal/Modal.tsx @@ -95,6 +95,7 @@ export interface ModalFuncProps { // TODO: find out exact types onOk?: (...args: any[]) => any; onCancel?: (...args: any[]) => any; + afterClose?: () => void; okButtonProps?: ButtonProps; cancelButtonProps?: ButtonProps; centered?: boolean; diff --git a/components/modal/__tests__/confirm.test.js b/components/modal/__tests__/confirm.test.js index 41bdbc1233..aa61183fb2 100644 --- a/components/modal/__tests__/confirm.test.js +++ b/components/modal/__tests__/confirm.test.js @@ -501,4 +501,24 @@ describe('Modal.confirm triggers callbacks correctly', () => { }); jest.useRealTimers(); }); + + it('trigger afterClose once when click on cancel button', () => { + const afterClose = jest.fn(); + open({ + afterClose, + }); + // first Modal + $$('.ant-btn')[0].click(); + expect(afterClose.mock.calls.length).toBe(0); + }); + + it('trigger afterClose once when click on ok button', () => { + const afterClose = jest.fn(); + open({ + afterClose, + }); + // second Modal + $$('.ant-btn-primary')[0].click(); + expect(afterClose.mock.calls.length).toBe(0); + }); }); diff --git a/components/modal/confirm.tsx b/components/modal/confirm.tsx index 3acb1955a0..77771204ec 100644 --- a/components/modal/confirm.tsx +++ b/components/modal/confirm.tsx @@ -81,7 +81,12 @@ export default function confirm(config: ModalFuncProps) { currentConfig = { ...currentConfig, visible: false, - afterClose: destroy.bind(this, ...args), + afterClose: () => { + if (typeof config.afterClose === 'function') { + config.afterClose(); + } + destroy.apply(this, args); + }, }; render(currentConfig); } diff --git a/components/modal/index.en-US.md b/components/modal/index.en-US.md index a594166acf..bc643acd80 100644 --- a/components/modal/index.en-US.md +++ b/components/modal/index.en-US.md @@ -65,6 +65,7 @@ The items listed above are all functions, expecting a settings object as paramet | Property | Description | Type | Default | Version | | --- | --- | --- | --- | --- | +| afterClose | Specify a function that will be called when modal is closed completely | function | - | 4.9.0 | | autoFocusButton | Specify which button to autofocus | null \| `ok` \| `cancel` | `ok` | | | bodyStyle | Body style for modal body element. Such as height, padding etc | CSSProperties | | 4.8.0 | | cancelButtonProps | The cancel button props | [ButtonProps](/components/button/#API) | - | | diff --git a/components/modal/index.zh-CN.md b/components/modal/index.zh-CN.md index 9885f1e0a2..9ed6e83335 100644 --- a/components/modal/index.zh-CN.md +++ b/components/modal/index.zh-CN.md @@ -68,6 +68,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/3StSdUlSH/Modal.svg | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | +| afterClose | Modal 完全关闭后的回调 | function | - | 4.9.0 | | autoFocusButton | 指定自动获得焦点的按钮 | null \| `ok` \| `cancel` | `ok` | | | bodyStyle | Modal body 样式 | CSSProperties | | 4.8.0 | | cancelButtonProps | cancel 按钮 props | [ButtonProps](/components/button/#API) | - | |