Skip to content

Commit 2b2cb2a

Browse files
tanhauhautaylorzane
authored andcommitted
handle undefined input value with spread (sveltejs#5291)
1 parent 8c9a541 commit 2b2cb2a

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Include selector in message of `unused-css-selector` warning ([#5252](https://github.com/sveltejs/svelte/issues/5252))
1010
* Fix using `<Namespaced.Component/>`s in child `{#await}`/`{#each}` contexts ([#5255](https://github.com/sveltejs/svelte/issues/5255))
1111
* Fix using `<svelte:component>` in `{:catch}` ([#5259](https://github.com/sveltejs/svelte/issues/5259))
12+
* Fix setting one-way bound `<input>` `value` to `undefined` when it has spread attributes ([#5270](https://github.com/sveltejs/svelte/issues/5270))
1213

1314
## 3.24.1
1415

src/compiler/compile/render_dom/wrappers/Element/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,18 @@ export default class ElementWrapper extends Wrapper {
725725
block.chunks.update.push(b`
726726
if (${block.renderer.dirty(Array.from(dependencies))} && ${data}.multiple) @select_options(${this.var}, ${data}.value);
727727
`);
728+
} else if (this.node.name === 'input' && this.attributes.find(attr => attr.node.name === 'value')) {
729+
const type = this.node.get_static_attribute_value('type');
730+
if (type === null || type === "" || type === "text" || type === "email" || type === "password") {
731+
block.chunks.mount.push(b`
732+
${this.var}.value = ${data}.value;
733+
`);
734+
block.chunks.update.push(b`
735+
if ('value' in ${data}) {
736+
${this.var}.value = ${data}.value;
737+
}
738+
`);
739+
}
728740
}
729741
}
730742

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
async test({ assert, component, target, window }) {
3+
const input = target.querySelector("input");
4+
component.value = undefined;
5+
6+
assert.equal(input.value, "undefined");
7+
8+
component.value = "foobar";
9+
10+
assert.equal(input.value, "foobar");
11+
}
12+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value = '';
3+
</script>
4+
5+
<input {value} {...{}} />

0 commit comments

Comments
 (0)