ant-design/components/modal/demo/async.md

63 lines
1.3 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 1
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-06-12 17:11:32 +08:00
2016-08-11 11:41:06 +08:00
## en-US
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
```tsx
2022-05-23 14:37:16 +08:00
import { Button, Modal } from 'antd';
import React, { useState } from 'react';
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
const showModal = () => {
setVisible(true);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const handleOk = () => {
setModalText('The modal will be closed after two seconds');
setConfirmLoading(true);
2015-08-25 17:08:43 +08:00
setTimeout(() => {
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
const handleCancel = () => {
2016-08-11 11:41:06 +08:00
console.log('Clicked cancel button');
setVisible(false);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +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
export default App;
2019-05-07 14:57:32 +08:00
```