Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlled empty select element submits first value by default #3198

Open
jeffreysnell opened this issue Oct 28, 2024 · 0 comments
Open

Controlled empty select element submits first value by default #3198

jeffreysnell opened this issue Oct 28, 2024 · 0 comments

Comments

@jeffreysnell
Copy link

jeffreysnell commented Oct 28, 2024

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

  • If Select is not required: The form should submit an empty value ("") when no selection is made.
  • If Select is required: The form should throw an invalid event when attempting to submit without a selection.

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
@jeffreysnell jeffreysnell changed the title Unselected Controlled Select Element Submits First Value by Default Controlled empty select element submits first value by default Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant