-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
First option in select dropdown appears selected when no option matches the bound value #6126
Comments
Does this even work in plain HTML? That's why every Example: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select Edit: here's another point to think about: How would a user undo their selection (go back to the no-selected state) without having an explicit empty |
@Prinzhorn Thank you for the reply and thoughtful questions. For single select elements in plain HTML, I think you're right that there isn't a concept of not having an option selected. However, in plain HTML there also isn't a concept of binding a select value to an object, With a plain HTML form where the page is reloaded on submission, the submitted value always matches that of the selected option. But this expectation is broken in Svelte if an option is shown as selected when it doesn't match the bound value.
For optional dropdowns, you are correct that an explicit empty option is needed. For such forms it works fine to add a It seems like the only way I can get close to the expected behavior for required dropdowns is to add Adding an extra option element and binding to a blank string seems a bit like a hacky workaround, though. My expectation is that when using a two-way binding Svelte would automatically ensure that an option is only selected if it matches the bound select value. |
I think I misunderstood the original issue and the discussion was missing the point. In HTML there is no So to rephrase your feature request: you want Here's what I would do, which matches how basically every select I've ever encountered worked: https://svelte.dev/repl/1cdd3422d8ef4a6eb12ddf0b25be6e82?version=3.35.0 Edit: I personally would be very cautions with binding objects and stick to your Edit2: Also regarding the |
@Prinzhorn Thank you for the additional perspective.
Correct. This is how it works in Angular and Vue.js.
If I may push back on this, the state actually would be consistent: no option would be selected since none of them match the
While this approach (manually disabling the submit button when the bound value is E.g. in my production app I have a form with six required select dropdowns, along with a few text inputs. The native Furthermore, even if I add all those
I have the same issue whether I bind to objects or just IDs. Svelte still shows the first option as selected if the input is bound to I did some experimentation with the Svelte code and it looks like a fix for this issue could be as simple as adding |
Maybe we're having different interpretations of the term "consistent". No option is rendered as selected to the user but your To me personally it's a bug if your value doesn't match any of the options. Like I said to me having a special Something like this <select selectedIndex={questions.indexOf(selected)} on:change={e => selected = questions[e.target.selectedIndex]}> Edit: on a second thought I'm not against it anymore. I personally always have my selected value in the list of options so your use-case does not apply to me. And having |
You can also use a custom action specific for this task https://svelte.dev/repl/368ce1cecc1b4bb4beb6259c70e0444e?version=3.35.0 |
Previously, when the bound value of a select element was set to `null` or any other value not matching one of the options, the first option would appear selected. So even if the select element was `required`, users could submit the form and end up sending a different value than what it appeared they were submitting. With this change, if a select element is bound to a value that doesn't match one of its options, then none of the options will appear selected. This helps catch bugs during development when the bound value inadvertently doesn't match an option. It also makes it possible to bind a `required` select element to `null`, and cause the browser to require users to explicitly select an option before the form can be submitted. Resolves sveltejs#6126. In order for the test to pass, it was necessary to update the jsdom dependency to the latest 15.x release, since v15.2.0 fixed `selectEl.value` when no `<option>` is selected to return the empty string. See https://github.com/jsdom/jsdom/blob/15.2.1/Changelog.md#1520 and jsdom/jsdom@699ed6b.
Previously, when the bound value of a select element was set to `null` or any other value not matching one of the options, the first option would appear selected. So even if the select element was `required`, users could submit the form and end up sending a different value than what it appeared they were submitting. With this change, if a select element is bound to a value that doesn't match one of its options, then none of the options will appear selected. This helps catch bugs during development when the bound value inadvertently doesn't match an option. It also makes it possible to bind a `required` select element to `null`, and cause the browser to require users to explicitly select an option before the form can be submitted. Resolves sveltejs#6126. In order for the test to pass, it was necessary to update the jsdom dependency to the latest 15.x release, since v15.2.0 fixed `selectEl.value` when no `<option>` is selected to return the empty string. See https://github.com/jsdom/jsdom/blob/15.2.1/Changelog.md#1520 and jsdom/jsdom@699ed6b.
@Prinzhorn I created PR #6170 to resolve this now. The fix only required adding a single line to Svelte's |
Previously, when the bound value of a select element was set to `null` or any other value not matching one of the options, the first option would appear selected. So even if the select element was `required`, users could submit the form and end up sending a different value than what it appeared they were submitting. With this change, if a select element is bound to a value that doesn't match one of its options, then none of the options will appear selected. This helps catch bugs during development when the bound value inadvertently doesn't match an option. It also makes it possible to bind a `required` select element to `null`, and cause the browser to require users to explicitly select an option before the form can be submitted. Resolves sveltejs#6126. In order for the test to pass, it was necessary to update the jsdom dependency to the latest 15.x release, since v15.2.0 fixed `selectEl.value` when no `<option>` is selected to return the empty string. See https://github.com/jsdom/jsdom/blob/15.2.1/Changelog.md#1520 and jsdom/jsdom@699ed6b.
Previously, when the bound value of a select element was set to `null` or any other value not matching one of the options, the first option would appear selected. So even if the select element was `required`, users could submit the form and end up sending a different value than what it appeared they were submitting. With this change, if a select element is bound to a value that doesn't match one of its options, then none of the options will appear selected. This helps catch bugs during development when the bound value inadvertently doesn't match an option. It also makes it possible to bind a `required` select element to `null`, and cause the browser to require users to explicitly select an option before the form can be submitted. Resolves sveltejs#6126.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
The issue is still relevant. Hopefully the PR that addresses it can be reviewed soon. |
This should be fixed in 3.42.2 - https://svelte.dev/repl/a7fdeec83c504c00b9706edc679330c6?version=3.42.2 |
Describe the bug
When a select dropdown value is bound to a component variable, and this variable doesn't match any of the options in the dropdown, Svelte shows the first option as selected rather than blank. This causes bugs/unexpected behavior when the form is submitted (the submitted value doesn't match what the user sees).
To Reproduce
https://svelte.dev/repl/a7fdeec83c504c00b9706edc679330c6?version=3.35.0
Expected behavior
No option should be selected when none of the options match the bound value.
Uncomment the two
setTimeout
calls in the above REPL to see the expected behavior.Information about your Svelte project:
Severity
I'm currently using Angular and would like to use Svelte instead. For my app it is important that select dropdowns be blank when none of the options match the bound value.
The text was updated successfully, but these errors were encountered: