Skip to content

Commit 8cb72d9

Browse files
authored
[fix] set selectedIndex to -1 when no option matches bound <select> value (#6170)
1 parent ca096b7 commit 8cb72d9

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/runtime/internal/dom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ export function select_option(select, value) {
542542
return;
543543
}
544544
}
545+
546+
select.selectedIndex = -1; // no option should be selected
545547
}
546548

547549
export function select_options(select, value) {

test/runtime/samples/binding-select-initial-value-undefined/_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default {
1414
`,
1515

1616
test({ assert, component, target }) {
17+
assert.equal(component.selected, 'a');
1718
const select = target.querySelector('select');
1819
const options = [...target.querySelectorAll('option')];
1920

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export default {
2+
html: `
3+
<p>selected: null</p>
4+
5+
<select>
6+
<option value='a'>a</option>
7+
<option value='b'>b</option>
8+
<option value='c'>c</option>
9+
</select>
10+
11+
<p>selected: null</p>
12+
`,
13+
14+
async test({ assert, component, target }) {
15+
const select = target.querySelector('select');
16+
const options = [...target.querySelectorAll('option')];
17+
18+
assert.equal(component.selected, null);
19+
20+
// no option should be selected since none of the options matches the bound value
21+
assert.equal(select.value, '');
22+
assert.equal(select.selectedIndex, -1);
23+
assert.ok(!options[0].selected);
24+
25+
component.selected = 'a'; // first option should now be selected
26+
assert.equal(select.value, 'a');
27+
assert.ok(options[0].selected);
28+
29+
assert.htmlEqual(target.innerHTML, `
30+
<p>selected: a</p>
31+
32+
<select>
33+
<option value='a'>a</option>
34+
<option value='b'>b</option>
35+
<option value='c'>c</option>
36+
</select>
37+
38+
<p>selected: a</p>
39+
`);
40+
41+
component.selected = 'd'; // doesn't match an option
42+
43+
// now no option should be selected again
44+
assert.equal(select.value, '');
45+
assert.equal(select.selectedIndex, -1);
46+
assert.ok(!options[0].selected);
47+
48+
assert.htmlEqual(target.innerHTML, `
49+
<p>selected: d</p>
50+
51+
<select>
52+
<option value='a'>a</option>
53+
<option value='b'>b</option>
54+
<option value='c'>c</option>
55+
</select>
56+
57+
<p>selected: d</p>
58+
`);
59+
}
60+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
// set as null so no option will be selected by default
3+
export let selected = null;
4+
</script>
5+
6+
<p>selected: {selected}</p>
7+
8+
<select bind:value={selected}>
9+
<option>a</option>
10+
<option>b</option>
11+
<option>c</option>
12+
</select>
13+
14+
<p>selected: {selected}</p>

0 commit comments

Comments
 (0)