Skip to content

Commit

Permalink
fix: handle component binding mutation (#10786)
Browse files Browse the repository at this point in the history
* fix: handle component binding mutation

#10359 (comment)

* alternative approach to mutating props (#10788)

Co-authored-by: Rich Harris <rich.harris@vercel.com>

---------

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
3 people committed Mar 13, 2024
1 parent 2cb78ac commit 339782f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-readers-provide.md
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: handle component binding mutation
14 changes: 8 additions & 6 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Expand Up @@ -470,12 +470,14 @@ export function serialize_set_binding(node, context, fallback, options) {
if (binding.kind === 'prop') {
return b.call(
left,
b.assignment(
node.operator,
/** @type {import('estree').Pattern} */ (visit(node.left)),
value
),
b.literal(true)
b.sequence([
b.assignment(
node.operator,
/** @type {import('estree').Pattern} */ (visit(node.left)),
value
),
b.call(left)
])
);
} else {
return b.call(
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/internal/client/reactivity/props.js
Expand Up @@ -210,7 +210,7 @@ export function prop(props, key, flags, initial) {

if (!immutable) current_value.equals = safe_equals;

return function (/** @type {V} */ value, mutation = false) {
return function (/** @type {V} */ value) {
var current = get(current_value);

// legacy nonsense — need to ensure the source is invalidated when necessary
Expand All @@ -226,9 +226,9 @@ export function prop(props, key, flags, initial) {
}

if (arguments.length > 0) {
if (mutation || (immutable ? value !== current : safe_not_equal(value, current))) {
if (!current_value.equals(value)) {
from_child = true;
set(inner_current_value, mutation ? current : value);
set(inner_current_value, value);
get(current_value); // force a synchronisation immediately
}

Expand Down
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<input bind:value={value.name}>
@@ -0,0 +1,32 @@
import { ok, test } from '../../test';

export default test({
html: `
<input>
<p>foo</p>
`,

ssrHtml: `
<input value=foo>
<p>foo</p>
`,

async test({ assert, component, target, window }) {
const event = new window.MouseEvent('input');
const input = target.querySelector('input');
ok(input);

input.value = 'blah';
await input.dispatchEvent(event);
await Promise.resolve();

assert.deepEqual(component.deep, { name: 'blah' });
assert.htmlEqual(
target.innerHTML,
`
<input>
<p>blah</p>
`
);
}
});
@@ -0,0 +1,11 @@
<script>
import Widget from './Widget.svelte';
export let deep = {
name: 'foo'
};
</script>

<Widget bind:value={deep}/>

<p>{deep.name}</p>

0 comments on commit 339782f

Please sign in to comment.