Dynamic form in Reactjs
I think you just need to add a "hiddenField" key which contains all keys need to hide whenever the field value have changed.
[
...
{
"hiddenField": {
"vaccinated": ["reason", "moreDetails"],
"not_vaccinated": [
"dose",
"doseOneDetails",
"doseTwoDetails",
"moreDetails"
],
"other": ["reason", "dose", "doseOneDetails", "doseTwoDetails"]
}
}
...
]
And then we change your entire if..else code to this
if (Array.isArray(name) && name.length) {
const [fieldName] = name;
if (fieldName === "vaccination_status") {
setFieldsToHide((data[1] as any).hiddenField[value]);
}
}
Here is an example