Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle component binding mutation #10786

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-readers-provide.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<input bind:value={value.name}>
Original file line number Diff line number Diff line change
@@ -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>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Widget from './Widget.svelte';

export let deep = {
name: 'foo'
};
</script>

<Widget bind:value={deep}/>

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