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
|
|
|
|
|
|
|
|
Asynchronously close a modal dialog when a user clicked OK button, for example,
|
|
|
|
you can use this pattern when you submit a form.
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Modal, Button } from 'antd';
|
2015-10-08 15:13:04 +08:00
|
|
|
|
2017-02-20 22:02:12 +08:00
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
2017-05-16 10:43:12 +08:00
|
|
|
ModalText: 'Content of the modal',
|
2017-02-20 22:02:12 +08:00
|
|
|
visible: false,
|
2017-09-20 22:22:53 +08:00
|
|
|
confirmLoading: false,
|
2017-02-20 22:02:12 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-02-20 22:02:12 +08:00
|
|
|
showModal = () => {
|
2015-06-12 23:31:44 +08:00
|
|
|
this.setState({
|
2016-05-11 09:32:33 +08:00
|
|
|
visible: true,
|
2015-06-12 23:31:44 +08:00
|
|
|
});
|
2017-02-20 22:02:12 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-02-20 22:02:12 +08:00
|
|
|
handleOk = () => {
|
2015-06-12 17:11:32 +08:00
|
|
|
this.setState({
|
2017-05-16 10:43:12 +08:00
|
|
|
ModalText: 'The modal will be closed after two seconds',
|
2016-05-11 09:32:33 +08:00
|
|
|
confirmLoading: true,
|
2015-06-12 17:11:32 +08:00
|
|
|
});
|
2015-08-25 17:08:43 +08:00
|
|
|
setTimeout(() => {
|
2015-06-12 23:31:44 +08:00
|
|
|
this.setState({
|
2015-10-27 19:57:18 +08:00
|
|
|
visible: false,
|
2016-05-11 09:32:33 +08:00
|
|
|
confirmLoading: false,
|
2015-06-12 23:31:44 +08:00
|
|
|
});
|
2015-08-25 17:08:43 +08:00
|
|
|
}, 2000);
|
2017-02-20 22:02:12 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-02-20 22:02:12 +08:00
|
|
|
handleCancel = () => {
|
2016-08-11 11:41:06 +08:00
|
|
|
console.log('Clicked cancel button');
|
2015-09-08 12:38:21 +08:00
|
|
|
this.setState({
|
2016-05-11 09:32:33 +08:00
|
|
|
visible: false,
|
2015-09-08 12:38:21 +08:00
|
|
|
});
|
2017-02-20 22:02:12 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2015-06-12 17:11:32 +08:00
|
|
|
render() {
|
2017-05-16 10:43:12 +08:00
|
|
|
const { visible, confirmLoading, ModalText } = this.state;
|
2016-01-07 16:29:12 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2017-05-16 10:43:12 +08:00
|
|
|
<Button type="primary" onClick={this.showModal}>Open</Button>
|
|
|
|
<Modal title="Title"
|
|
|
|
visible={visible}
|
2016-01-08 14:41:05 +08:00
|
|
|
onOk={this.handleOk}
|
2017-05-16 10:43:12 +08:00
|
|
|
confirmLoading={confirmLoading}
|
2016-06-06 13:54:10 +08:00
|
|
|
onCancel={this.handleCancel}
|
|
|
|
>
|
2017-05-16 10:43:12 +08:00
|
|
|
<p>{ModalText}</p>
|
2016-01-07 16:29:12 +08:00
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
2017-02-20 22:02:12 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 17:11:32 +08:00
|
|
|
|
2017-02-20 22:02:12 +08:00
|
|
|
ReactDOM.render(<App />, mountNode);
|
2015-06-12 17:11:32 +08:00
|
|
|
````
|