2016-04-12 16:12:13 +08:00
|
|
|
|
---
|
|
|
|
|
order: 14
|
|
|
|
|
title: 与 Modal 配合使用
|
|
|
|
|
---
|
2016-04-12 15:18:57 +08:00
|
|
|
|
|
|
|
|
|
在 Modal 中使用 Form,当点击 Modal 的确定时,调用 `this.props.form.getFieldsValue` 获取表单内的值。
|
|
|
|
|
|
|
|
|
|
````jsx
|
|
|
|
|
import { Button, Form, Input, Modal } from 'antd';
|
|
|
|
|
const createForm = Form.create;
|
|
|
|
|
const FormItem = Form.Item;
|
|
|
|
|
|
|
|
|
|
let Demo = React.createClass({
|
|
|
|
|
getInitialState() {
|
|
|
|
|
return { visible: false };
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleSubmit() {
|
|
|
|
|
console.log(this.props.form.getFieldsValue());
|
|
|
|
|
this.hideModal();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
showModal() {
|
|
|
|
|
this.setState({ visible: true });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hideModal() {
|
|
|
|
|
this.setState({ visible: false });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { getFieldProps } = this.props.form;
|
|
|
|
|
|
|
|
|
|
const formItemLayout = {
|
|
|
|
|
labelCol: { span: 4 },
|
|
|
|
|
wrapperCol: { span: 20 },
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<Button type="primary" onClick={this.showModal}>点击有惊喜</Button>
|
|
|
|
|
<Modal title="登录" visible={this.state.visible} onOk={this.handleSubmit} onCancel={this.hideModal}>
|
|
|
|
|
<Form horizontal form={this.props.form}>
|
|
|
|
|
<FormItem
|
|
|
|
|
{...formItemLayout}
|
2016-06-02 18:09:20 +08:00
|
|
|
|
label="用户名">
|
2016-04-12 15:18:57 +08:00
|
|
|
|
<Input {...getFieldProps('username', {})} type="text" autoComplete="off" />
|
|
|
|
|
</FormItem>
|
|
|
|
|
<FormItem
|
|
|
|
|
{...formItemLayout}
|
2016-06-02 18:09:20 +08:00
|
|
|
|
label="密码">
|
2016-04-12 15:18:57 +08:00
|
|
|
|
<Input {...getFieldProps('password', {})} type="password" autoComplete="off" />
|
|
|
|
|
</FormItem>
|
|
|
|
|
</Form>
|
|
|
|
|
</Modal>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2016-05-11 09:32:33 +08:00
|
|
|
|
},
|
2016-04-12 15:18:57 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Demo = createForm()(Demo);
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
|
````
|