I am making a simple react app with form that has radio buttons.
Here there is a default data available like,
const defaultData = [{ ContactMode: 3 }, { ContactMode: 2 }, { ContactMode: 2 }];
Requirement:
-> Need to iterate this defaultData
and assign their respective ContactMode
mode as checked in each row.
Working Snippet:
const { useState } = React;
const App = () => {
const [formValue, setFormValue] = useState({
ContactMode: 1
});
const defaultData = [{ ContactMode: 3 }, { ContactMode: 2 }, { ContactMode: 2 }];
const handleInputChange = (e, index) => {
const { name, value } = event.target;
setFormValue({
...formValue,
[name]: value
});
};
const submitData = () => {
console.log(formValue);
};
return (
<div>
<form>
{defaultData.map((item, index) => {
return (<React.Fragment>
<label> Contact Mode </label>
<input
type="radio"
name="ContactMode"
checked={item.ContactMode == 1}
value={1}
onChange={(event) => handleInputChange(index, event)}
/>{" "}
Email
<input
type="radio"
name="ContactMode"
checked={item.ContactMode == 2}
value={2}
onChange={(event) => handleInputChange(index, event)}
/>{" "}
Text
<input
type="radio"
name="ContactMode"
checked={item.ContactMode == 3}
value={3}
onChange={(event) => handleInputChange(index, event)}
/>{" "}
Both
<br />
<br />
<button type="button">
Save
</button>
<br />
<br />
<br />
</React.Fragment>);
})}
</form>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Expected output:
Contact Mode Email Text Both (Checked)
Contact Mode Email Text(Checked) Both
Contact Mode Email Text(Checked) Both
Note: I can't modify the name attribute name="ContactMode"
.. Reason is I also have individual save button each row and on click of that save button that particular row will gets saved.. And each row should have name="ContactMode"
..
Edit:
As my question is not clear in what I am trying to achieve, I am going to give the entire code here.
-> I have a stepper form in which Step 2 is employment section and by default I have the values to set into the form,
const dynamicData = [
{
EmploymentID: 1,
companyName: 'Company One',
designation: 'Designation One',
ContactMode: 3,
},
{
EmploymentID: 2,
companyName: 'Company two',
designation: 'Designation One',
ContactMode: 2,
},
];
-> Inside useEffect
hook I set the form value like,
React.useEffect(() => {
setExpanded(0);
setValue((prev) => {
const companyDetails = [...dynamicData];
return { ...prev, companyDetails };
});
}, []);
Here the form values with input boxes are binded properly but the radio button value doesn't gets checked.
-> There are two data's available but the first one will be expanded by default and whereas the second one will gets opened on click of the Expand
button below the first one.
-> On click of the save
button, that particular object (inputField
) will be console logged (testing only) along with modified data..
Here the ISSUE is if we modify the data then things are working fine but the radio checked attribute alone not making the item checked.. (Checked indication is not working)..
Please go to step 2 in the below given codesandbox to see the issue.. To see the second item, click on the expand button..
Please kindly help me to set default radio button value in each row.
handleInputChange
to consume(e, index)
, but in the UI you invert the orderhandleInputChange(index, event)
. YourdefaultData
also has two{ ContactMode: 2 }
and no{ ContactMode: 1 }
, maybe this is intentional. As @vkvkvkv points out, using the samename
attribute across all radio inputs places them all in the same radio group and there can be only a single selected radio group value. – TristaExpand
andShrink
option which makes each data expandable on clickExpand
.. – RicadynamicData
the only other references toContactMode
are the radio inputs, and each group of them should have a unique name. The save button doesn't reference anything byContactMode
. – Trista