Skip to content

Commit

Permalink
fix: select first enabled option by default when initial value is und…
Browse files Browse the repository at this point in the history
…efined

Fixes an unintended regression introduced in sveltejs#6170.

Resolves sveltejs#7041
  • Loading branch information
theodorejb committed Feb 26, 2023
1 parent ca53151 commit 04eafad
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ export function select_option(select, value) {
}
}

select.selectedIndex = -1; // no option should be selected
if (value !== undefined) {
select.selectedIndex = -1; // no option should be selected
}
}

export function select_options(select, value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
<p>selected: a</p>
<select>
<option disabled='' value='x'>x</option>
<option value='a'>a</option>
<option value='b'>b</option>
<option value='c'>c</option>
Expand All @@ -18,7 +19,8 @@ export default {
const select = target.querySelector('select');
const options = [...target.querySelectorAll('option')];

// first enabled option should be selected
assert.equal(select.value, 'a');
assert.ok(options[0].selected);
assert.ok(options[1].selected);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<p>selected: {selected}</p>

<select bind:value={selected}>
<option disabled>x</option>
<option>a</option>
<option>b</option>
<option>c</option>
Expand Down

0 comments on commit 04eafad

Please sign in to comment.