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

[Select] Controlled components Allows Value Change Without onValueChange Handler #1331

Open
nizhnyk opened this issue Jan 13, 2025 · 2 comments
Labels
component: select This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@nizhnyk
Copy link

nizhnyk commented Jan 13, 2025

Bug report

Current behavior

When a state variable is passed to the value prop of the Base UI Select component, the Select menu can still be opened, and a different option can be selected without using the onValueChange function. This behavior is unexpected for a controlled component since changes to its value should only occur through the onValueChange handler.

Expected behavior

The Select component should function as a controlled component. Changes to the selected value should only be possible when handled via the onValueChange function. The value prop should strictly determine the displayed selected value.

Reproducible example

Link to a CodeSandbox

Steps to reproduce:

  1. Pass a state variable to the value prop of the Select component. (Already done)
  2. Do not handle the onValueChange function. (Already commented out)
  3. Open the Select menu and try selecting a different option.
  4. Notice that the selected value changes without using onValueChange.

Base UI version

v1.0.0-alpha.5

Which browser are you using?

Chrome, Safari

Which OS are you using?

Mac OS

Additional context

This behavior contradicts the expected behavior of controlled components. It might cause inconsistencies in state management when using Select in forms or other UI workflows.

@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 13, 2025
@mj12albert
Copy link
Member

mj12albert commented Jan 14, 2025

Changes to the selected value should only be possible when handled via the onValueChange function. The value prop should strictly determine the displayed selected value.

This makes sense, Radix's select does this (sandbox), and React will throw an error for controlled inputs that are missing the value change handler.

Though I wonder in the Radix example when the value just doesn't change and there's no console warning/error, if it may be immediately obvious enough what is going on (in the context of a real project and not just a minimal example)

Currently I think all our form controls do this in controlled mode (e.g. NumberField, Slider)

What do you think? @michaldudak @atomiks @colmtuite

@mj12albert mj12albert changed the title [Select] Controlled Select Component Allows Value Change Without onValueChange Handler [Select] Controlled components Allows Value Change Without onValueChange Handler Jan 14, 2025
@mj12albert mj12albert added the component: select This is the name of the generic UI component, not the React module! label Jan 14, 2025
@atomiks
Copy link
Contributor

atomiks commented Jan 14, 2025

Aren't there use cases for a controlled component where you want to change the value externally via setX(value) and still have it update (or maybe this behavior is just limited to Select?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants