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.91k stars 831 forks source link

Controlled empty select element submits first value by default #3198

Open jeffreysnell opened 3 weeks ago

jeffreysnell commented 3 weeks ago

Bug report

Using a controlled Select element with a placeholder in a form causes the form to submit the first item as the selected value, even if the user hasn’t made a selection.

Current Behavior

When submitting a form with a controlled Select component, the first item in the Select is submitted as the value, even if no selection has been made. See screenshot below:

Screenshot 2024-10-28 at 8 06 59 AM

I also attempted to use a placeholder item to give the Select a valid default which resulted in an error:

A <Select.Item /> must have a value prop that is not an empty string. This is because the Select value can be set to an empty string to clear the selection and show the placeholder.

Expected behavior

Reproducible example

Select example

Suggested solution

Ideally, if the user makes no selection, the Select value should be "". However, to avoid a breaking change for current users, an optional flag to control this behavior could be provided.

An alternate solution would be to allow a placeholder item to be inserted in the first element of the select and to select that item by default.

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/react-select 2.1.2
React n/a 18
Browser Chrome latest
Assistive tech None
Node n/a
npm/yarn npm
Operating System OSX