繁体   English   中英

无法将受控的组件值传递给redux-form

[英]Can't pass controlled component values to redux-form

我创建了“出生日期”组件,该组件应该具有“日期”,“月”和“年”的单独字段。 该组件将合并值并以YYYY-MM-DD格式提供日期字符串。

Date of birth代码部分

import * as React from 'react';
import * as _ from 'lodash';
import { Col, Input } from 'antd';

const InputGroup = Input.Group;

class DateOfBirthInput extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      value:{}
    };
    console.log(props);
  }

  public handleChange = (event: any) => {
    const date:any = _.get(this.state,"value");
    if(event.target.id === 'day'){
      date.day = event.target.value;
    }
    if(event.target.id === 'month'){
      date.month = event.target.value;
    }
    if(event.target.id === 'year'){
      date.year = event.target.value;
    }
    this.setState({
      value: date
    });
  }

  public render() {
    return (
      <div>
        <div className="ant-form-item-label"><label title="Date of birth">Date of birth</label></div>
        <InputGroup>
          <Col span={4}>
            <Input
              placeholder="MM"
              id="month"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col span={1}/>
          <Col span={4}>
            <Input
              placeholder="DD"
              id="day"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col span={1}/>
          <Col span={5}>
            <Input
              placeholder="YYYY"
              id="year"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col>
            <h4>Date of birth output: {JSON.stringify(this.state.value)}</h4>
          </Col>
        </InputGroup>
      </div>
    );
  }
}

export default DateOfBirthInput;

父组件

...
<InputGroup>
  <Col span={24}>
    <Field
      label="Date of Birth"
      name="userInfo.dateOfBirth"
      placeholder="Date of Birth"
      component={DateOfBirthInput}
     />
  </Col>
</InputGroup>
...

如何获取从子组件状态到父组件的值传递?

根据文档,我需要传递valueonChange事件,它应该可以工作。 https://redux-form.com/6.0.0-alpha.5/docs/faq/customcomponent.md/

我想我在父组件中缺少某些内容。 请帮忙。

使组件正常工作。 这是解决方案

import * as React from 'react';
import * as _ from 'lodash';
import { Col, Input } from 'antd';

const InputGroup = Input.Group;

class DateOfBirthInput extends React.Component<any, any> {
  public value:any = 'abc';

  constructor(props: any) {
    super(props);
    this.state = {
      value:{}
    };
    this.value = "DateSr";
    console.log(props);
  }

  public handleChange = (event: any) => {
    const date:any = _.get(this.state,"value");
    if(event.target.id === 'day'){
      date.day = event.target.value;
    }
    if(event.target.id === 'month'){
      date.month = event.target.value;
    }
    if(event.target.id === 'year'){
      date.year = event.target.value;
    }
    this.setState({
      value: date
    });
    const DateString:string = date.year+ '-' + date.month + '-' + date.day;
    this.props.input.onChange(DateString);
  }

  public render() {
    return (
      <div>
        <div className="ant-form-item-label"><label title="Date of birth">Date of birth</label></div>
        <InputGroup>
          <Col span={4}>
            <Input
              placeholder="MM"
              name="legalInfo.dateOfBirth"
              id="month"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col span={1}/>
          <Col span={4}>
            <Input
              placeholder="DD"
              name="legalInfo.dateOfBirth"
              id="day"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col span={1}/>
          <Col span={5}>
            <Input
              placeholder="YYYY"
              name="legalInfo.dateOfBirth"
              id="year"
              type="text"
              onChange={this.handleChange} />
          </Col>
          <Col>
            <h4>Date of birth output: {JSON.stringify(this.state.value)}</h4>
          </Col>
        </InputGroup>
      </div>
    );
  }
}

export default DateOfBirthInput;

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM