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

setChoices keeps the previously selected value (default option ignored) #1261

Open
saeraphin opened this issue Feb 4, 2025 · 1 comment
Open
Labels

Comments

@saeraphin
Copy link

saeraphin commented Feb 4, 2025

Describe the bug
Up to v10, updating the choices with the setChoices method and replaceOptions true was properly discarding the base select value if it wasn't part of the new options.
Since v11, using setChoices even with replaceOptions true keeps the previously selected value, even if it isn't part of the newly provided options.
Furthermore, there is no way to restore the default disabled but selected option provided to the base select element.

To Reproduce
Steps to reproduce the behaviour:

  1. Create a new Choice instance without passing choices
  2. The base select tag should only contain one disabled but selected option ("please make a choice")
  3. Update the options a first time
  4. The disabled option is properly showing as the base selected one ("please make a choice")
  5. Select one enabled option
  6. Update the options a second time (spreading the _presetChoices to provide the default option)
  7. The previously selected option value remains selected
  8. The default option doesn't show up

Example
https://codepen.io/saeraphin/pen/ZYzNyoR

Expected behaviour
The previously selected option value shouldn't remain selected if not par of the new options values.
The default disabled option should appear as the placeholder.

Choices version and bundle

  • Version: [e.g. v11.0.3 choices.min.js]

Desktop

  • OS: MacOS, Windows
  • Browser: Chrome 132.0.6834.160
@saeraphin saeraphin added the bug label Feb 4, 2025
@saeraphin
Copy link
Author

Currently, I was able to update my code to v11 by calling the clearStore method before setChoices, but I'm not sure it's the good approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant