How to reset second Select when choosing item from first Select? #1105
-
Hello,
The problem arises if a user selects let's say Movies, then the sub-category Comedy, and then goes back and chooses Music. The variables for each will read:
Which is out of sync with what they should be able to choose or select. Is there a way to reset the second Select 'onSelectedChange' of the first select, so that the user is forced to choose a new sub-category whenever a Category is selected? Many thanks for any help - it's been driving me crazy! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi, @MattHeslington Mind sharing your working snippet? Meanwhile, my guess is turning Regardless, I'm curious about this too, and sharing your snippet might offer some context. 🙂 |
Beta Was this translation helpful? Give feedback.
-
Hi, @shyakadavis Certainly, there's not much to it, though I am already using a recactive variable. When a user chooses an option in the first Select component, we call the getSubCategories function which returns the subCategories for the chosen option and set the subCategory to an empty string. This does display the new options in the second select when the user clicks on it.
After posting this yesterday I realised it's not an issue to do with this (great) library, it's a UX / Javascript issue. We are setting the subCategory to an empty string for the second select when a choice is made on the first select, so with form handling we can prevent submission, but the OCD in me just wants to be able to reset the second select each time, so that the placeholder is displayed, instead of showing the last chosen option. EDIT: I did notice in the bits.ui documentation there's an 'el' property on the <Select.Content> tag described as "The underlying DOM element being rendered. You can bind to this to programatically interact with the element", though I haven't worked out how to do this yet |
Beta Was this translation helpful? Give feedback.
Hey, @MattHeslington
Got it working with a weird hack 😅
I created a reactive object variable:
And assigned it to the selected prop of the second select component.
Now, any changes to the first select will reset the second one. 🤷♂️
Screen.Recording.2024-05-22.at.12.16.23.mov