I tried following the instructions here: http://react.tips/radio-buttons-in-reactjs/
This code nearly works but I can't seem to figure out what is wrong. With two radio options clicking the selected radio button changes the selection to the other option. Clicking the radio that is unselected does nothing. I've tried a bunch of things but nothing seems to get the radio to work as expected. Clicking an unselected option selects it.
Here is my code: (*Updated)
var RadioInput = React.createClass({
getInitialState: function () {
return {selectedOption: "0"};
},
handleOptionChange: function (changeEvent) {
this.setState({selectedOption: changeEvent.target.value});
console.log(this.state.selectedOption, changeEvent.target, this.props.inputValue);
},
render: function () {
var isChecked = this.state.selectedOption === this.props.inputValue;
return (<p><label>
<input type="radio" name={this.props.inputName} value={this.props.inputValue} checked={isChecked} onChange={this.handleOptionChange}/>{this.props.labelText}
</label></p>);
}
});
My hope is that with this one generic RadioInput I can reuse it in other components that compile a bunch of form inputs together... Like I'm doing below....
var MediaInfo = React.createClass({
render: function () {
return (
<div className="thumbnail">
<div className="caption text-center">
<h3>Media Contact Options</h3>
<form className="text-left">
<p>Please indicate your contact preferences for media inquiries below:</p>
<div className="form-group">
<div className="col-md-12">
{this.props.fields.options.map (function (data, i) {
return (<RadioInput key={i} inputName={data.inputName} labelText={data.labelText} inputValue={data.inputValue} />);
})}
</div>
</div>
{this.props.fields.fields.map (function (data, i) {
return (<TextInput key={i} inputName={data.inputName} labelText={data.labelText} placeholderText={data.placeholderText} inputValue={data.inputValue} />);
})}
</form>
</div>
</div>
)
}
});
var MemberInfo = React.createClass({
getInitialState: function () {
return {
contactInfo: [ {inputName:"fullName", labelText:"Display Name", placeholderText:"Enter Full Name", inputValue:"some name"},
{inputName:"phoneNumber", labelText:"Phone Number", placeholderText:"Enter Phone Number", inputValue:"001-555-1234"},
{inputName:"email", labelText:"Email", placeholderText:"Enter Email", inputValue:"[email protected]"},
{inputName:"address", labelText:"Address", placeholderText:"Enter Address", inputValue:"123 Main St. Podunk, FL"}
],
mediaContact: {fields: [ {inputName: "email", labelText: "Media Email", placeholderText:"Enter Medial Contact Email", inputValue:"[email protected]"},
{inputName: "phone", labelText: "Media Phone", placeholderText:"Media Contact Phone Number", inputValue:"001-555-1234"},
],
options: [
{inputName: "mediaConsent", labelText: " Yes, I would like to be contacted by the Media.", inputValue:"1"},
{inputName: "mediaConsent", labelText: " No, I do not wish to be contacted by the Media.", inputValue:"0"}
]
}
}
},
render: function () {
return (
<div className="panel panel-default">
<div className="panel-heading">
<h3 className="panel-title">Manage Your Contact Info</h3> </div>
<div className="panel-body">
<div className="col-md-6">
<div className="row">
<div className="col-xs-12">
<ContactInfo fields={this.state.contactInfo} />
</div>
</div>
</div>
<div className="col-md-6">
<div className="row">
<div className="col-xs-12">
<MediaInfo fields={this.state.mediaContact} />
</div>
</div>
</div>
</div>
</div>
);
},
});