Skip to content

Commit 5811236

Browse files
authored
fix: improve select handling of dynamic value with placeholders (#12181)
* fix: improve select handling of dynamic value with placeholders * format
1 parent 59f7bd6 commit 5811236

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.changeset/wicked-ways-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: improve select handling of dynamic value with placeholders

packages/svelte/src/internal/client/dom/elements/bindings/select.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export function select_option(select, value, mounting) {
3939
* @param {() => V} [get_value]
4040
*/
4141
export function init_select(select, get_value) {
42+
let mounting = true;
4243
effect(() => {
4344
if (get_value) {
44-
select_option(select, untrack(get_value));
45+
select_option(select, untrack(get_value), mounting);
4546
}
47+
mounting = false;
4648

4749
var observer = new MutationObserver(() => {
4850
// @ts-ignore
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
let { children, ...rest } = $props();
3+
</script>
4+
<select name="pets" id="pet-select1" {...rest}>
5+
{@render children()}
6+
</select>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<div><label for="pet-select">Choose a pet 1:</label><select id="pet-select1" name="pets">
5+
<option value="">--Please choose an option--</option><option value="dog">Dog</option><option value="cat">Cat</option></select></div><div><label for="pet-select">Choose a pet 2:</label>
6+
<select id="pet-select2" name="pets"><option value="">--Please choose an option--</option><option value="dog">Dog</option><option value="cat">Cat</option></select></div>`
7+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script>
2+
import Select from './Select.svelte'
3+
</script>
4+
5+
<div>
6+
<label for="pet-select">Choose a pet 1:</label>
7+
<Select>
8+
<option value="">--Please choose an option--</option>
9+
<option value="dog">Dog</option>
10+
<option value="cat">Cat</option>
11+
</Select>
12+
</div>
13+
14+
<div>
15+
<label for="pet-select">Choose a pet 2:</label>
16+
<select name="pets" id="pet-select2">
17+
<option value="">--Please choose an option--</option>
18+
<option value="dog">Dog</option>
19+
<option value="cat">Cat</option>
20+
</select>
21+
</div>

0 commit comments

Comments
 (0)