antd添加form表单行

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;
React是一种流的JavaScript库,用于构建用户界面。Ant Design是一种基于React的UI框架,提供了丰富的组件和工具,用于构建高质量的用户界面。 在React中动态添加Ant Design的Form表单,可以使用以下步骤: 1. 创建一个React组件,用于封装Form表单。可以使用Ant Design的Form组件来创建表单,并将其添加到组件中。 ```jsx import React from 'react'; import { Form, Input } from 'antd'; const DynamicForm = () => { return ( <Form> <Form.Item> <Input /> </Form.Item> </Form> ); }; ``` 2. 使用JavaScript动态地创建Form表单元素,并将它们添加到DOM中。可以使用React的`createElement`函数来创建元素,并使用`setState`方法来更新组件的状态。当需要添加新的表单元素时,可以调用`setState`方法来更新组件的状态,并在DOM中动态地创建新的表单元素。 ```jsx class FormContainer extends React.Component { state = { dynamicFields: ['name', 'age'], // 初始字段列表 }; addField = () => { const { dynamicFields } = this.state; dynamicFields.push('email'); // 添加新的字段到列表中 this.setState({ dynamicFields }); // 更新状态并触发重新渲染组件 }; render() { return ( <div> <button onClick={this.addField}>添加字段</button> <DynamicForm /> // 使用动态生成的表单元素渲染动态表单 </div> ); } } ``` 在上面的代码中,我们创建了一个名为`FormContainer`的React组件,它包含一个状态变量`dynamicFields`,用于存储动态生成的表单字段列表。当用户点击“添加字段”按钮时,将触发`addField`方法,将一个新的字段添加到列表中,并使用`setState`方法更新状态。这将触发组件的重新渲染,并使用动态生成的表单元素渲染动态表单。 请注意,动态添加表单元素需要使用适当的布局和样式来正确显示它们。Ant Design的Form组件提供了一些有用的布局和样式选,可以帮助您更好地控制表单的外观和为。您可以参考Ant Design的文档来了解更多关于Form组件的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值