当前位置:   article > 正文

antd添加form表单行_antpro modalform实现新增一行

antpro modalform实现新增一行
import {Button, Icon, Form, Row, Col, Input, Tag, Modal} from "antd";
import React, {Component} from "react";

const FormItem = Form.Item;


const CollectionCreateForm = Form.create()(
    // eslint-disable-next-line
    class extends React.Component {
        render() {
            const {
                visible, onCancel, onCreate, form,
            } = this.props;
            const {getFieldDecorator} = form;
            return (
                <Modal
                    visible={visible}
                    title="Create a new collection"
                    okText="Create"
                    onCancel={onCancel}
                    onOk={onCreate}
                >
                    <Form layout="vertical">
                        <FormItem label="hallCode">
                            {getFieldDecorator('hallCode', {
                                rules: [{required: false, message: 'Please input hallCode!'}],
                            })(
                                <Input placeholder="hallCode"/>
                            )}
                        </FormItem>
                        <FormItem label="libraryName">
                            {getFieldDecorator('libraryName', {
                                rules: [{required: false, message: 'Please input libraryName!'}],
                            })(
                                <Input placeholder="libraryName"/>
                            )}
                        </FormItem>
                        <FormItem label="relatedLibraryName">
                            {getFieldDecorator('relatedLibraryName', {
                                rules: [{required: false, message: 'Please input relatedLibraryName!'}],
                            })(
                                <Input placeholder="relatedLibraryName"/>
                            )}
                        </FormItem>
                        <FormItem label="address">
                            {getFieldDecorator('address', {
                                rules: [{required: false, message: 'Please input address!'}],
                            })(
                                <Input placeholder="address"/>
                            )}
                        </FormItem>
                    </Form>

                </Modal>
            );
        }
    }
);


class CollectionsPage extends React.Component {
    state = {
        visible: false,
    };

    showModal = () => {
        this.setState({visible: true});
    }

    handleCancel = () => {
        this.setState({visible: false});
    }

    handleCreate = () => {
        const form = this.formRef.props.form;
        form.validateFields((err, values) => {
            if (err) {
                return;
            }

            console.log('Received values of form: ', values);
            this.props.addLibrary(values)
            form.resetFields();
            this.setState({visible: false});
        });
    }

    saveFormRef = (formRef) => {
        this.formRef = formRef;
    }

    render() {
        return (
            <div>
                <div align="right">
                    <Button type="primary" onClick={this.showModal}>New Collection</Button>
                </div>
                <CollectionCreateForm
                    wrappedComponentRef={this.saveFormRef}
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    onCreate={this.handleCreate}
                />
            </div>
        );
    }
}


class ProjectLibraries extends Component {
    constructor(props) {
        super(props);
        this.state = {uuid: 0, libraries: []}
        this.add = this.add.bind(this)
    }

    add(library) {
        console.log("父组件接收到了library:" + library)
        this.state.libraries.push(library)
        this.setState({libraries: this.state.libraries})
    }


    render() {
        const getFieldDecorator = this.props.getFieldDecorator
        const formItemLayout = this.props.formItemLayout
        const formItems = this.state.libraries.map((value, index) => {
            return (<div>
                <Row gutter={24} type="flex" justify="center">
                    <Col span={6}>
                            <FormItem {...formItemLayout} wrapperCol={{span: 24, offset: 0}}>
                                {getFieldDecorator('projectLibraryMapDtos.hallCode', {
                                    rules: [{required: false, message: 'Please input hallCode!'}],
                                    initialValue: value.hallCode,
                                })(
                                    <Input placeholder="hallCode" disabled/>
                                )}
                            </FormItem>
                    </Col>
                    <Col span={6}>
                        <FormItem {...formItemLayout} wrapperCol={{span: 24, offset: 0}}>
                            {getFieldDecorator('projectLibraryMapDtos.libraryName', {
                                rules: [{required: false, message: 'Please input libraryName!'}],
                                initialValue: value.libraryName,
                            })(
                                <Input placeholder="馆名" disabled/>
                            )}
                        </FormItem>
                    </Col>
                    <Col span={6}>
                        <FormItem {...formItemLayout} wrapperCol={{span: 24, offset: 0}}>
                            {getFieldDecorator('projectLibraryMapDtos.relatedLibraryName', {
                                rules: [{required: false, message: 'Please input relatedLibraryName!'}],
                                initialValue: value.relatedLibraryName,
                            })(
                                <Input placeholder="关联馆馆号" disabled/>
                            )}
                        </FormItem>
                    </Col>
                    <Col span={6}>
                        <FormItem {...formItemLayout} wrapperCol={{span: 24, offset: 0}}>
                            {getFieldDecorator('projectLibraryMapDtos.address', {
                                rules: [{required: false, message: 'Please input address!'}],
                                initialValue: value.address,
                            })(
                                <Input placeholder="地址" disabled/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
            </div>)
        })
        return (
            <div>
                <Row gutter={24} type="flex" justify="center">
                    <Col span={6}>
                        <div style={{textAlign:"center"}}>hallCode</div>
                    </Col>
                    <Col span={6}>
                        <div style={{textAlign:"center"}}>馆名</div>
                    </Col>
                    <Col span={6}>
                        <div style={{textAlign:"center"}}>关联馆馆号</div>
                    </Col>
                    <Col span={6}>
                        <div style={{textAlign:"center"}}>地址</div>
                    </Col>
                </Row>
                {formItems}
                <CollectionsPage addLibrary={(value) => {
                    this.add(value)
                }}/>
            </div>
        );
    }
}

export default ProjectLibraries;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/91610
推荐阅读