Skip to content

Commit 5e9a205

Browse files
committed
Fix null option being selected when it doesn't match the bound value
1 parent bd1bff8 commit 5e9a205

File tree

2 files changed

+7
-1
lines changed
  • packages/svelte
    • src/internal/client
    • tests/runtime-legacy/samples/binding-select-null-placeholder-2

2 files changed

+7
-1
lines changed

packages/svelte/src/internal/client/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ export function select_option(select, value, mounting) {
458458
}
459459
}
460460
if (!mounting || value !== undefined) {
461-
select.value = '';
461+
select.selectedIndex = -1; // no option should be selected
462462
}
463463
}
464464

packages/svelte/tests/runtime-legacy/samples/binding-select-null-placeholder-2/_config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ export default test({
2424
component.foo = items[0];
2525
assert.equal(options[0].selected, false);
2626
assert.equal(options[1].selected, true);
27+
28+
component.foo = { id: 'c' }; // doesn't match an option
29+
assert.equal(select.value, '');
30+
assert.equal(select.selectedIndex, -1);
31+
assert.equal(options[0].selected, false);
32+
assert.equal(options[1].selected, false);
2733
}
2834
});

0 commit comments

Comments
 (0)