I created a Date of Birth component which is supposed to have separate fields for Date, Month and Year. The component will combine the values and provide a date string in YYYY-MM-DD format.
Code for Date of birth
component
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;
Parent component
...
<InputGroup>
<Col span={24}>
<Field
label="Date of Birth"
name="userInfo.dateOfBirth"
placeholder="Date of Birth"
component={DateOfBirthInput}
/>
</Col>
</InputGroup>
...
How do I get the pass the value from child component's state to parent component?
As per the documentation, I need to pass value
and onChange
event and it should work. https://redux-form.com/6.0.0-alpha.5/docs/faq/customcomponent.md/
I guess I am missing something in the parent component. Please help.
Got the component working. Here is the solution
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;
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.