I'm storing default values of multiple radioGroups in an State array object in react. Current state object formRating has about 22 elements where in each is radioGroup's defaulValue is stored
Once stored into the state, the array object looks like below;
formRating: Array[1]
0: Array[22]
0:{…}
condId: "C2.1(a)"
rate: "3"
1:{…}
2:{…}
.... goes till 21:{...}
Upon the user selecting a different value on the radioGroup, i want that to be updated in the state array.
For example say user selected the radio button of value 2 of the radioGroup with the name or ID "C2.1(a)", I want the onChange method to update the state as follows. (just the rate of the element with matching condId changes while rest remains the same.)
formRating: Array[1]
0: Array[22]
0:{…}
condId: "C2.1(a)"
rate: "2"
1:{…}
2:{…}
.... goes till 21:{...}
Ive tried the following onChange method.
onChange={this.changeButton('C2.1(a)')}
changeButton = (condId)=> e =>{
e.preventDefault();
this.props.handleChangeRadio(condId);
}
handleChangeRadio =(condId) => e =>{
console.log(e, condId);
this.setState(prevState => ({
formRating: prevState.formRating.map(
el => el.condId === [condId]? { ...el, rate: e.target.value }: el
)
}))
}
Currently I am using a button to initiate the state array object formRating.
in the render
const formValues= [{condId :'C2.1(a)',rate:'3'},
{condId :'C2.2(b)',rate:'3'},
{condId :'C2.2(a)',rate:'3'},
{condId :'NTS1.1(a)',rate:'3'},
{condId :'C2.3(a)',rate:'3'}]; //22 such items in the array
and in the form
<RaisedButton label="Select Default Values" secondary={true} onClick={this.defaultButton(formValues)} style={{width:300}}/>
defaultButton =(formValues)=> e => {
e.preventDefault();
this.props.setRadio(formValues);
}
setRadio= (formValues) => {
const {formRating} = this.state;
this.setState({
formRating: [formValues]})
}
Any help is appreciated