I wirte a component of radio,when i make 3 radios, radio1 value is 1,radio2 value is 2,radio 2 value is 3,
Then,when i click radio 1,console.log(this.state.value)
return me nothing,
when i click radio 2,console.log(this.state.value)
return me 1,
when i click radio 3,console.log(this.state.value)
return me 2,
Is there anything wrong?
import React, { Component } from 'react';
import { FormGroup } from 'react-bootstrap';
import ContentWrapper from '../../Layout/ContentWrapper';
class Single extends Component {
constructor(props) {
super(props);
this.state = {
value: props.value
}
}
valueChange = (e) => {
this.setState({value: e.target.value});
console.log(this.state.value);
}
getSingleChoice = (qItems) => {
let items = Object.keys(qItems).map((k, index) => {
let item = qItems[k];
return (
<div
className="radio c-radio"
id={this.props.id + "_" + k}
key={this.props.id + "_" + k}
onChange={e => this.valueChange(e)}>
<label>
<input
type="radio"
name={"radio" + this.props.id}
value={item.value}
defaultChecked={this.state.value == item.value}/>
<em className="fa fa-circle"></em>{item.label}</label>
</div>
);
});
return (
<FormGroup>{items}</FormGroup>
);
};
render() {
let title = this.props.id + '. ' + this.props.title ';
return (
<ContentWrapper>
<div className="panel panel-default">
<div className="panel-heading">{title}</div>
<div className="panel-body">
{this.getSingleChoice(this.props.options)}
</div>
</div>
</ContentWrapper>
);
}
}
export default Single;