2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 1
|
2017-02-20 22:02:12 +08:00
|
|
|
title:
|
2016-08-11 11:41:06 +08:00
|
|
|
zh-CN: 异步关闭
|
|
|
|
en-US: Asynchronously close
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-06-12 17:11:32 +08:00
|
|
|
|
2016-08-11 11:41:06 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2015-10-27 19:57:18 +08:00
|
|
|
点击确定后异步关闭对话框,例如提交表单。
|
2015-06-12 17:11:32 +08:00
|
|
|
|
2016-08-11 11:41:06 +08:00
|
|
|
## en-US
|
|
|
|
|
2021-06-09 12:18:52 +08:00
|
|
|
Asynchronously close a modal dialog when the OK button is pressed. For example, you can use this pattern when you submit a form.
|
2016-08-11 11:41:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
|
|
|
import React, { useState } from 'react';
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Modal, Button } from 'antd';
|
2015-10-08 15:13:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [confirmLoading, setConfirmLoading] = useState(false);
|
|
|
|
const [modalText, setModalText] = useState('Content of the modal');
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-11-05 11:35:37 +08:00
|
|
|
const showModal = () => {
|
|
|
|
setVisible(true);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-11-05 11:35:37 +08:00
|
|
|
const handleOk = () => {
|
|
|
|
setModalText('The modal will be closed after two seconds');
|
|
|
|
setConfirmLoading(true);
|
2015-08-25 17:08:43 +08:00
|
|
|
setTimeout(() => {
|
2020-11-05 11:35:37 +08:00
|
|
|
setVisible(false);
|
|
|
|
setConfirmLoading(false);
|
2015-08-25 17:08:43 +08:00
|
|
|
}, 2000);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-11-05 11:35:37 +08:00
|
|
|
const handleCancel = () => {
|
2016-08-11 11:41:06 +08:00
|
|
|
console.log('Clicked cancel button');
|
2020-11-05 11:35:37 +08:00
|
|
|
setVisible(false);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-11-05 11:35:37 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button type="primary" onClick={showModal}>
|
|
|
|
Open Modal with async logic
|
|
|
|
</Button>
|
|
|
|
<Modal
|
|
|
|
title="Title"
|
|
|
|
visible={visible}
|
|
|
|
onOk={handleOk}
|
|
|
|
confirmLoading={confirmLoading}
|
|
|
|
onCancel={handleCancel}
|
|
|
|
>
|
|
|
|
<p>{modalText}</p>
|
|
|
|
</Modal>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2015-06-12 17:11:32 +08:00
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|