I'm currently refactoring an older legacy application and use React to rebuild some of the former functionality. This application has a form which has +10 different input fields. The aim is to kind of built a wizard that leads the user through fields, instead of bombarding them with +10 fields at once.
Now, I've thought a little about how to tackle this. I could have a component for each step (lets say three step) and each one contains some detailed information about the form. This could then be wrapped up with a parent-component to deal with the state.
But this would imply that each "step"-component is very specific to its fields and that makes very static.
Maybe instead of writing out all the steps into individual components, I could have a very generic one, which takes a number of elements, but doesn't care about anything regarding to this specific form. The idea would be to externalise the form's structure into a json object, which will define the form and provides the information on how to build it.
Something like:
{"user_input_form": {
"steps": [{
"index": 1,
"fields": {
"first_name": {
"required": true,
"label": "The first name label",
"value": "",
"type": "text",
},
"last_name": {
"required": true,
"label": "The last name label",
"value": "",
"type": "text",
}
}
},
{
"index": 2,
"fields": {
"date_of_birth": {
"required": true,
"label": "The birthday label",
"value": "",
"type": "date",
},
"home_country": {
"required": false,
"label": "The last name label",
"value": "",
"type": "select",
"options": ["England", "Wales", "Scotland", "Northern Ireland"]
}
}
}]
}
Whilst the form component is tightly tied to the json structure, it would not be tied to the underlying data.
Is that a common thing to do or rather something to avoid?
Cheers