2016-04-12 16:12:13 +08:00
---
2016-10-27 11:42:28 +08:00
order: 4
2016-08-02 09:34:04 +08:00
title:
2016-10-27 11:42:28 +08:00
zh-CN: 弹出层中的新建表单
en-US: Form in Modal to Create
2016-04-12 16:12:13 +08:00
---
2016-04-12 15:18:57 +08:00
2016-07-31 09:53:51 +08:00
## zh-CN
2016-10-27 11:42:28 +08:00
当用户访问一个展示了某个列表的页面,想新建一项但又不想跳转页面时,可以用 Modal 弹出一个表单,用户填写必要信息后创建新的项。
2016-04-12 15:18:57 +08:00
2016-07-31 09:53:51 +08:00
## en-US
2018-04-03 22:00:31 +08:00
When user visit a page with a list of items, and want to create a new item. The page can popup a form in Modal, then let user fill in the form to create an item.
2016-07-31 09:53:51 +08:00
2019-05-07 14:57:32 +08:00
```jsx
import { Button, Modal, Form, Input, Radio } from 'antd';
2018-06-27 15:55:04 +08:00
2018-12-17 10:02:08 +08:00
const CollectionCreateForm = Form.create({ name: 'form_in_modal' })(
2018-11-28 15:00:03 +08:00
// eslint-disable-next-line
2018-03-15 14:40:47 +08:00
class extends React.Component {
render() {
2019-05-07 14:57:32 +08:00
const { visible, onCancel, onCreate, form } = this.props;
2018-03-15 14:40:47 +08:00
const { getFieldDecorator } = form;
return (
< Modal
visible={visible}
title="Create a new collection"
okText="Create"
onCancel={onCancel}
onOk={onCreate}
>
< Form layout = "vertical" >
2018-12-22 16:48:30 +08:00
< Form.Item label = "Title" >
2018-03-15 14:40:47 +08:00
{getFieldDecorator('title', {
rules: [{ required: true, message: 'Please input the title of collection!' }],
2019-05-07 14:57:32 +08:00
})(< Input / > )}
2018-12-22 16:48:30 +08:00
< / Form.Item >
< Form.Item label = "Description" >
2018-03-15 14:40:47 +08:00
{getFieldDecorator('description')(< Input type = "textarea" / > )}
2018-12-22 16:48:30 +08:00
< / Form.Item >
2019-02-19 23:24:07 +08:00
< Form.Item className = "collection-create-form_last-form-item" >
2018-03-15 14:40:47 +08:00
{getFieldDecorator('modifier', {
initialValue: 'public',
})(
< Radio.Group >
< Radio value = "public" > Public< / Radio >
< Radio value = "private" > Private< / Radio >
2019-05-07 14:57:32 +08:00
< / Radio.Group > ,
2018-03-15 14:40:47 +08:00
)}
2018-12-22 16:48:30 +08:00
< / Form.Item >
2018-03-15 14:40:47 +08:00
< / Form >
< / Modal >
);
}
2019-05-07 14:57:32 +08:00
},
2016-10-27 11:42:28 +08:00
);
2017-02-20 11:50:57 +08:00
class CollectionsPage extends React.Component {
state = {
visible: false,
};
2018-06-27 15:55:04 +08:00
2017-02-20 11:50:57 +08:00
showModal = () => {
2016-04-12 15:18:57 +08:00
this.setState({ visible: true });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2017-02-20 11:50:57 +08:00
handleCancel = () => {
2016-04-12 15:18:57 +08:00
this.setState({ visible: false });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2017-02-20 11:50:57 +08:00
handleCreate = () => {
2018-03-15 14:40:47 +08:00
const form = this.formRef.props.form;
2016-10-27 11:42:28 +08:00
form.validateFields((err, values) => {
if (err) {
return;
}
2016-04-12 15:18:57 +08:00
2016-10-27 11:42:28 +08:00
console.log('Received values of form: ', values);
form.resetFields();
this.setState({ visible: false });
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
saveFormRef = formRef => {
2018-03-15 14:40:47 +08:00
this.formRef = formRef;
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-04-12 15:18:57 +08:00
render() {
return (
< div >
2019-05-07 14:57:32 +08:00
< Button type = "primary" onClick = {this.showModal} >
New Collection
< / Button >
2016-10-27 11:42:28 +08:00
< CollectionCreateForm
2018-03-15 14:40:47 +08:00
wrappedComponentRef={this.saveFormRef}
2016-10-27 11:42:28 +08:00
visible={this.state.visible}
onCancel={this.handleCancel}
onCreate={this.handleCreate}
/>
2016-04-12 15:18:57 +08:00
< / div >
);
2017-02-20 11:50:57 +08:00
}
}
2016-04-12 15:18:57 +08:00
2016-10-27 11:42:28 +08:00
ReactDOM.render(< CollectionsPage / > , mountNode);
2019-05-07 14:57:32 +08:00
```
2016-04-12 15:18:57 +08:00
2019-05-07 14:57:32 +08:00
```css
2019-02-19 23:24:07 +08:00
.collection-create-form_last-form-item {
margin-bottom: 0;
}
2019-05-07 14:57:32 +08:00
```