-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] set selectedIndex to -1 when no option matches bound <select> v…
…alue (#6170)
- Loading branch information
1 parent
ca096b7
commit 8cb72d9
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export default { | ||
html: ` | ||
<p>selected: null</p> | ||
<select> | ||
<option value='a'>a</option> | ||
<option value='b'>b</option> | ||
<option value='c'>c</option> | ||
</select> | ||
<p>selected: null</p> | ||
`, | ||
|
||
async test({ assert, component, target }) { | ||
const select = target.querySelector('select'); | ||
const options = [...target.querySelectorAll('option')]; | ||
|
||
assert.equal(component.selected, null); | ||
|
||
// no option should be selected since none of the options matches the bound value | ||
assert.equal(select.value, ''); | ||
assert.equal(select.selectedIndex, -1); | ||
assert.ok(!options[0].selected); | ||
|
||
component.selected = 'a'; // first option should now be selected | ||
assert.equal(select.value, 'a'); | ||
assert.ok(options[0].selected); | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<p>selected: a</p> | ||
<select> | ||
<option value='a'>a</option> | ||
<option value='b'>b</option> | ||
<option value='c'>c</option> | ||
</select> | ||
<p>selected: a</p> | ||
`); | ||
|
||
component.selected = 'd'; // doesn't match an option | ||
|
||
// now no option should be selected again | ||
assert.equal(select.value, ''); | ||
assert.equal(select.selectedIndex, -1); | ||
assert.ok(!options[0].selected); | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<p>selected: d</p> | ||
<select> | ||
<option value='a'>a</option> | ||
<option value='b'>b</option> | ||
<option value='c'>c</option> | ||
</select> | ||
<p>selected: d</p> | ||
`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script> | ||
// set as null so no option will be selected by default | ||
export let selected = null; | ||
</script> | ||
|
||
<p>selected: {selected}</p> | ||
|
||
<select bind:value={selected}> | ||
<option>a</option> | ||
<option>b</option> | ||
<option>c</option> | ||
</select> | ||
|
||
<p>selected: {selected}</p> |