form-in-modal.md 2.8 KB
Newer Older
A
afc163 已提交
1
---
2
order: 4
B
Benjy Cui 已提交
3
title:
4 5
  zh-CN: 弹出层中的新建表单
  en-US: Form in Modal to Create
A
afc163 已提交
6
---
B
Benjy Cui 已提交
7

G
Gray Choi 已提交
8 9
## zh-CN

10
当用户访问一个展示了某个列表的页面,想新建一项但又不想跳转页面时,可以用 Modal 弹出一个表单,用户填写必要信息后创建新的项。
B
Benjy Cui 已提交
11

G
Gray Choi 已提交
12 13
## en-US

A
André 已提交
14
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.
G
Gray Choi 已提交
15

A
afc163 已提交
16 17
```jsx
import { Button, Modal, Form, Input, Radio } from 'antd';
A
afc163 已提交
18

Z
zombiej 已提交
19
const CollectionCreateForm = Form.create({ name: 'form_in_modal' })(
A
afc163 已提交
20
  // eslint-disable-next-line
21 22
  class extends React.Component {
    render() {
A
afc163 已提交
23
      const { visible, onCancel, onCreate, form } = this.props;
24 25 26 27 28 29 30 31 32 33
      const { getFieldDecorator } = form;
      return (
        <Modal
          visible={visible}
          title="Create a new collection"
          okText="Create"
          onCancel={onCancel}
          onOk={onCreate}
        >
          <Form layout="vertical">
A
afc163 已提交
34
            <Form.Item label="Title">
35 36
              {getFieldDecorator('title', {
                rules: [{ required: true, message: 'Please input the title of collection!' }],
A
afc163 已提交
37
              })(<Input />)}
A
afc163 已提交
38 39
            </Form.Item>
            <Form.Item label="Description">
40
              {getFieldDecorator('description')(<Input type="textarea" />)}
A
afc163 已提交
41
            </Form.Item>
42
            <Form.Item className="collection-create-form_last-form-item">
43 44 45 46 47 48
              {getFieldDecorator('modifier', {
                initialValue: 'public',
              })(
                <Radio.Group>
                  <Radio value="public">Public</Radio>
                  <Radio value="private">Private</Radio>
A
afc163 已提交
49
                </Radio.Group>,
50
              )}
A
afc163 已提交
51
            </Form.Item>
52 53 54 55
          </Form>
        </Modal>
      );
    }
A
afc163 已提交
56
  },
57 58
);

59 60 61 62
class CollectionsPage extends React.Component {
  state = {
    visible: false,
  };
A
afc163 已提交
63

64
  showModal = () => {
B
Benjy Cui 已提交
65
    this.setState({ visible: true });
A
afc163 已提交
66
  };
A
afc163 已提交
67

68
  handleCancel = () => {
B
Benjy Cui 已提交
69
    this.setState({ visible: false });
A
afc163 已提交
70
  };
A
afc163 已提交
71

72
  handleCreate = () => {
陈帅 已提交
73
    const { form } = this.formRef.props;
74 75 76 77
    form.validateFields((err, values) => {
      if (err) {
        return;
      }
B
Benjy Cui 已提交
78

79 80 81 82
      console.log('Received values of form: ', values);
      form.resetFields();
      this.setState({ visible: false });
    });
A
afc163 已提交
83
  };
A
afc163 已提交
84

A
afc163 已提交
85
  saveFormRef = formRef => {
86
    this.formRef = formRef;
A
afc163 已提交
87
  };
A
afc163 已提交
88

B
Benjy Cui 已提交
89 90 91
  render() {
    return (
      <div>
A
afc163 已提交
92 93 94
        <Button type="primary" onClick={this.showModal}>
          New Collection
        </Button>
95
        <CollectionCreateForm
96
          wrappedComponentRef={this.saveFormRef}
97 98 99 100
          visible={this.state.visible}
          onCancel={this.handleCancel}
          onCreate={this.handleCreate}
        />
B
Benjy Cui 已提交
101 102
      </div>
    );
103 104
  }
}
B
Benjy Cui 已提交
105

106
ReactDOM.render(<CollectionsPage />, mountNode);
A
afc163 已提交
107
```
B
Benjy Cui 已提交
108

A
afc163 已提交
109
```css
110 111 112
.collection-create-form_last-form-item {
  margin-bottom: 0;
}
A
afc163 已提交
113
```