fix: prevent undefined value when remount keyed input element with spread props (#7699)

Fixes: #7578

When remounting a keyed input element (e.g. because the element order has changed) with spread properties, the input value gets undefined. This has happened because data_value is updated before remounting and it won't contain a value for input-value (because the value hasn't changed). When calling mount() an undefined value was assigned because of a missing check.
pull/8335/head
Nico Beierle 2 years ago committed by GitHub
parent 8cf037c904
commit 474a13ad90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -886,7 +886,9 @@ export default class ElementWrapper extends Wrapper {
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;
if ('value' in ${data}) {
${this.var}.value = ${data}.value;
}
`);
block.chunks.update.push(b`
if ('value' in ${data}) {

@ -0,0 +1,11 @@
export default {
test({ assert, component, target }) {
const [input1, input2] = target.querySelectorAll('input');
assert.equal(input1.value, 'value1');
assert.equal(input2.value, 'value2');
component.items = component.items.reverse();
assert.equal(input1.value, 'value1');
assert.equal(input2.value, 'value2');
}
};

@ -0,0 +1,7 @@
<script>
export let items = ['value1', 'value2'];
</script>
{#each items as item (item)}
<input value={item} {...{}} />
{/each}
Loading…
Cancel
Save