-
-
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: handle dynamic selects with falsy select values (#9471)
when options are added later, we need to ensure the select value still stays in sync fixes #9412
- Loading branch information
1 parent
19f84ca
commit c1f6ee0
Showing
6 changed files
with
184 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: handle dynamic selects with falsy select values |
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
27 changes: 27 additions & 0 deletions
27
...s/svelte/tests/runtime-legacy/samples/binding-select-initial-value-undefined-3/_config.js
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,27 @@ | ||
import { ok, test } from '../../test'; | ||
|
||
export default test({ | ||
skip_if_ssr: 'permanent', | ||
|
||
html: ` | ||
<p>selected: a</p> | ||
<select> | ||
<option disabled=''>x</option> | ||
<option value="a">a</option> | ||
<option value="b">b</option> | ||
<option value="c">c</option> | ||
</select> | ||
`, | ||
|
||
test({ assert, component, target }) { | ||
assert.equal(component.selected, 'a'); | ||
const select = target.querySelector('select'); | ||
ok(select); | ||
const options = [...target.querySelectorAll('option')]; | ||
|
||
// first enabled option should be selected | ||
assert.equal(select.value, 'a'); | ||
assert.ok(options[1].selected); | ||
} | ||
}); |
12 changes: 12 additions & 0 deletions
12
.../svelte/tests/runtime-legacy/samples/binding-select-initial-value-undefined-3/main.svelte
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,12 @@ | ||
<script> | ||
export let selected; | ||
</script> | ||
|
||
<p>selected: {selected}</p> | ||
|
||
<select bind:value={selected}> | ||
<option disabled>x</option> | ||
{#each ["a", "b", "c"] as val} | ||
<option>{val}</option> | ||
{/each} | ||
</select> |
112 changes: 112 additions & 0 deletions
112
packages/svelte/tests/runtime-legacy/samples/binding-select-unmatched-2/_config.js
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,112 @@ | ||
import { ok, test } from '../../test'; | ||
|
||
// // same test as the previous one, but with a dynamic each block | ||
export default test({ | ||
html: ` | ||
<p>selected: </p> | ||
<select> | ||
<option value="a">a</option> | ||
<option value="b">b</option> | ||
<option value="c">c</option> | ||
</select> | ||
<p>selected: </p> | ||
`, | ||
|
||
async test({ assert, component, target }) { | ||
const select = target.querySelector('select'); | ||
ok(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> | ||
` | ||
); | ||
|
||
component.selected = 'b'; // second option should now be selected | ||
assert.equal(select.value, 'b'); | ||
assert.ok(options[1].selected); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
` | ||
<p>selected: b</p> | ||
<select> | ||
<option value="a">a</option> | ||
<option value="b">b</option> | ||
<option value="c">c</option> | ||
</select> | ||
<p>selected: b</p> | ||
` | ||
); | ||
|
||
component.selected = undefined; // also 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: </p> | ||
<select> | ||
<option value="a">a</option> | ||
<option value="b">b</option> | ||
<option value="c">c</option> | ||
</select> | ||
<p>selected: </p> | ||
` | ||
); | ||
} | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/svelte/tests/runtime-legacy/samples/binding-select-unmatched-2/main.svelte
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}> | ||
{#each ['a', 'b', 'c'] as letter} | ||
<option>{letter}</option> | ||
{/each} | ||
</select> | ||
|
||
<p>selected: {selected}</p> |