Open mhsattarian opened 2 months ago
same here, any solution? the problem happens only when i use Select inside a form, if i use a simple div instead it works perfectyl
any solution?
what I did in my onChange
function was to check if options
are not loaded or the value passed to it was an empty string I would return early. not the best solution but works fine.
Bug report
Current Behavior
If a controlled
<Select />
component is used inside a form, after changing the value passed to it, theonValueChange
method will get called with an empty string value (''
).https://github.com/user-attachments/assets/30c48587-782d-4de8-ae12-042d8be7ab95
in the video above, I'm using the Shadcn/ui select component, note that the component is inside a form and I have a
console.log
statement inside the onChange passed to theonValueChanged
prop. on the initial render, the controlled value is undefined, after setting this value the onChange function is called with a value of an empty string.Expected behavior
I believe that the onChange method should only be called when a user interaction changes the value of Select and it's like so when the Select component is not used inside a form.
Reproducible example
https://stackblitz.com/edit/vitejs-vite-gua7y3?file=src%2FApp.tsx
Suggested solution
I guess this behavior is somewhat related to supporting browser auto-complete and I don't really know now how to fix this without interfering with that but I probably would prevent this onChange from happening if the Select component is being used as a controlled input.
Your environment