Skip to content

Commit

Permalink
handle undefined input value with spread (sveltejs#5291)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and taylorzane committed Dec 17, 2020
1 parent 8c9a541 commit 2b2cb2a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Include selector in message of `unused-css-selector` warning ([#5252](https://github.com/sveltejs/svelte/issues/5252))
* Fix using `<Namespaced.Component/>`s in child `{#await}`/`{#each}` contexts ([#5255](https://github.com/sveltejs/svelte/issues/5255))
* Fix using `<svelte:component>` in `{:catch}` ([#5259](https://github.com/sveltejs/svelte/issues/5259))
* Fix setting one-way bound `<input>` `value` to `undefined` when it has spread attributes ([#5270](https://github.com/sveltejs/svelte/issues/5270))

## 3.24.1

Expand Down
12 changes: 12 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,18 @@ export default class ElementWrapper extends Wrapper {
block.chunks.update.push(b`
if (${block.renderer.dirty(Array.from(dependencies))} && ${data}.multiple) @select_options(${this.var}, ${data}.value);
`);
} else if (this.node.name === 'input' && this.attributes.find(attr => attr.node.name === 'value')) {
const type = this.node.get_static_attribute_value('type');
if (type === null || type === "" || type === "text" || type === "email" || type === "password") {
block.chunks.mount.push(b`
${this.var}.value = ${data}.value;
`);
block.chunks.update.push(b`
if ('value' in ${data}) {
${this.var}.value = ${data}.value;
}
`);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
async test({ assert, component, target, window }) {
const input = target.querySelector("input");
component.value = undefined;

assert.equal(input.value, "undefined");

component.value = "foobar";

assert.equal(input.value, "foobar");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value = '';
</script>

<input {value} {...{}} />

0 comments on commit 2b2cb2a

Please sign in to comment.