I have a multi-page form written in Formik. Based on responses to a couple of the form fields, an API is called, which returns a JSON object containing questions that need to be answered.
As you can see in the JSON below, the top level of this JSON array contains questions that would need to be answered every time the form is displayed (in the example, only one question would definitely need to be answered). Within each question, there can be an “answerYes” or “answerNo” key. The value associated with either of these keys is identical to the top level question element. Additionally, the second level question element can contain another “answerYes”/”answerNo” key and this can happen indefinitely.
These “answerYes”/”answerNo” questions need to be dynamically generated as Formik Fields, based on the answer to the top level question. This needs to be possible for any depth of questions – i.e. second level, third level, fourth level, etc.
Can anyone think of an elegant way to do this using Formik Fields based on the JSON below?
[
{
"questionCode": "APPLE-1",
"question": "Do you like apples?",
"answerNo": {
"questionCode": "APPLE-2",
"question": "Are you sure you don't like apples?",
"answerYes": {
"questionCode": "APPLE-3",
"question": "Ok, do you like oranges?",
"answerYes": {
"questionCode": "ORANGE-1",
"question": "Ok, but do you like peeling oranges?"
}
}
}
}
]
