Skip to content

Commit

Permalink
Set selectedIndex to -1 when no option matches bound select value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theodorejb committed Apr 6, 2021
1 parent c7e820e commit eec65b2
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 117 deletions.
Loading

0 comments on commit eec65b2

Please sign in to comment.