I have a requirement where I need one form dropdown to trigger the options in another based on the selection. The common general use case for this would be the example of three dropdowns about location. The first is often the 'city', the second 'town' and the third would be the 'suburb'. Obviously the child options are not available until the parent has been selected.
I use this pattern in react-hook-form and I now have it working with tanstack form, but I'm using listeners to achieve this. But it doesn't seem quite right.
I was wondering if anyone has had any experience with this for tanstack form?
This is the concept I have so far:
//the parent select
<form.AppField
listeners={{
onChange: ({ value }) => {
const subjects = subjectTree?.child?.find(subject => subject.code === value)
const children = subjects?.child
const options = children?.map(child => ({
label: child.name,
value: child.code,
}))
setSubjectOptions(options ?? [])
},
}}
children={field => (
<field.SelectField
selectItems={subfields}
/>
)}
/>
//the dependent select
<form.AppField
name='subject'
children={field => (
<field.SelectField
selectItems={subjectOptions}
/>
)}
/>