radix-ui / primitives

Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
https://radix-ui.com/primitives
MIT License
15.95k stars 834 forks source link

Select component inside a form acts indeterminately #3135

Open mhsattarian opened 2 months ago

mhsattarian commented 2 months ago

Bug report

Current Behavior

If a controlled <Select /> component is used inside a form, after changing the value passed to it, the onValueChange 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 the onValueChanged 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.

CleanShot 2024-09-23 at 16 55 50

Your environment

Software Name(s) Version
Radix Package(s) Select 2.1.1
React n/a 18
Browser chrome 128
Assistive tech
Node n/a
npm/yarn
Operating System
eessadrati commented 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

mhsattarian commented 2 months ago

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.