我创建了“出生日期”组件,该组件应该具有“日期”,“月”和“年”的单独字段。该组件将合并值并以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>
...
如何将值从子组件的状态传递到父组件?
根据文档,我需要传递value
和onChange
事件,它应该可以工作。
https://redux-form.com/6.0.0-alpha.5/docs/faq/customcomponent.md/
我想我在父组件中缺少某些内容。 请帮忙。
答案 0 :(得分:0)
使组件正常工作。这是解决方法
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;
希望有帮助