react-antd中弹出层form内容传递给父组件

19 篇文章 0 订阅

子组件:

// jshint esversion:6
import React, { Component } from 'react';
import { Form, Input} from 'antd';

const FormItem = Form.Item;

class Forms extends Component{
    getItemsValue = ()=>{    //3、自定义方法,用来传递数据(需要在父组件中调用获取数据)
        const values= this.props.form.getFieldsValue();       //4、getFieldsValue:获取一组输入控件的值,如不传入参数,则获取全部组件的值
        return values;
    }
    render(){
        const { form } = this.props;
        const { getFieldDecorator } = form;    //1、将getFieldDecorator 解构出来,用于和表单进行双向绑定
        return(
            <Form layout="vertical">
                <FormItem label="姓名">
                    {getFieldDecorator('name')(    //2、getFieldDecorator 的使用方法,这种写法真的很蛋疼
                        <Input />
                    )}
                </FormItem>
                <FormItem label="年龄">
                    {getFieldDecorator('age')(
                        <Input />
                    )}
                </FormItem>
                <FormItem label="城市">
                    {getFieldDecorator('address')(
                        <Input />
                    )}
                </FormItem>
            </Form>
        )
    }
}

export default Form.create()(Forms);        //创建form实例

 

父组件:

import React, { Component } from 'react';
import { Modal } from 'antd';
import Forms from './Forms'

export default class Modals extends Component {
    handleCancel = () => {
        this.props.closeModal(false);
    }
    handleCreate = () => {
        console.log(this.formRef.getItemsValue());     //6、调用子组件的自定义方法getItemsValue。注意:通过  this.formRef 才能拿到数据
        this.props.getFormRef(this.formRef.getItemsValue());
        this.props.closeModal(false);
    }
    render() {
        const { visible } = this.props;
        return (
            <Modal
                visible={visible}
                title="新增"
                okText="保存"
                onCancel={this.handleCancel}
                onOk={this.handleCreate}
            >
                <Forms
                    wrappedComponentRef={(form) => this.formRef = form}       //5、使用wrappedComponentRef 拿到子组件传递过来的ref(官方写法)
                />
            </Modal>
        );
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值