Hello Retool community,
I'm trying to solve a dynamic UI problem. I have a resource that returns a JSON array, where each object defines a field that needs to be rendered in a form. The challenge is that the length of this array is unknown, and the input type for each field is specified within the object itself.
Here's an example of the data structure I receive from getFormSchema.data:
[
{
"id": "user_name",
"label": "Full Name",
"type": "textinput",
"defaultValue": "John Smith",
"required": true
},
{
"id": "user_age",
"label": "Age",
"type": "numberinput",
"defaultValue": 30,
"placeholder": "Enter your age"
},
{
"id": "is_subscribed",
"label": "Subscribe to newsletter?",
"type": "switch",
"defaultValue": true
},
{
"id": "user_department",
"label": "Department",
"type": "select",
"defaultValue": "eng",
"options": [
{"label": "Engineering", "value": "eng"},
{"label": "Marketing", "value": "mkt"},
{"label": "Sales", "value": "sls"}
]
}
]
My goal is to:
- Render this array as a form, where each object becomes a form field (e.g., a
TextInputfor "textinput", aSwitchfor "switch"). - Store the user's input in a way that can be easily submitted.
- On submit, I need to send back a single key-value JSON object, like
{"user_name": "Jane Doe", "user_age": 35, "is_subscribed": false, "user_department": "mkt"}.
I'm thinking of using a ListView component, but I'm struggling with how to dynamically render different component types within it and how to aggregate all the state from these dynamic components into a single object for submission. What's the best practice for achieving this?