Skip to content

Commit 8148a7a

Browse files
authored
each binding with store props (#5289)
1 parent c752ed3 commit 8148a7a

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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))
1212
* Fix setting one-way bound `<input>` `value` to `undefined` when it has spread attributes ([#5270](https://github.com/sveltejs/svelte/issues/5270))
13+
* Fix deep two-way bindings inside an `{#each}` involving a store ([#5286](https://github.com/sveltejs/svelte/issues/5286))
1314

1415
## 3.24.1
1516

src/compiler/compile/render_dom/Block.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import { b, x } from 'code-red';
44
import { Node, Identifier, ArrayPattern } from 'estree';
55
import { is_head } from './wrappers/shared/is_head';
66

7+
export interface Bindings {
8+
object: Identifier;
9+
property: Identifier;
10+
snippet: Node;
11+
store: string;
12+
tail: Node;
13+
modifier: (node: Node) => Node;
14+
}
15+
716
export interface BlockOptions {
817
parent?: Block;
918
name: Identifier;
1019
type: string;
1120
renderer?: Renderer;
1221
comment?: string;
1322
key?: Identifier;
14-
bindings?: Map<string, {
15-
object: Identifier;
16-
property: Identifier;
17-
snippet: Node;
18-
store: string;
19-
tail: Node;
20-
modifier: (node: Node) => Node;
21-
}>;
23+
bindings?: Map<string, Bindings>;
2224
dependencies?: Set<string>;
2325
}
2426

@@ -36,14 +38,7 @@ export default class Block {
3638

3739
dependencies: Set<string> = new Set();
3840

39-
bindings: Map<string, {
40-
object: Identifier;
41-
property: Identifier;
42-
snippet: Node;
43-
store: string;
44-
tail: Node;
45-
modifier: (node: Node) => Node;
46-
}>;
41+
bindings: Map<string, Bindings>;
4742

4843
chunks: {
4944
declarations: Array<Node | Node[]>;

src/compiler/compile/render_dom/wrappers/EachBlock.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import FragmentWrapper from './Fragment';
77
import { b, x } from 'code-red';
88
import ElseBlock from '../../nodes/ElseBlock';
99
import { Identifier, Node } from 'estree';
10+
import get_object from '../../utils/get_object';
1011

1112
export class ElseBlockWrapper extends Wrapper {
1213
node: ElseBlock;
@@ -139,11 +140,8 @@ export default class EachBlockWrapper extends Wrapper {
139140
view_length: fixed_length === null ? x`${iterations}.length` : fixed_length
140141
};
141142

142-
const store =
143-
node.expression.node.type === 'Identifier' &&
144-
node.expression.node.name[0] === '$'
145-
? node.expression.node.name.slice(1)
146-
: null;
143+
const object = get_object(node.expression.node);
144+
const store = object.type === 'Identifier' && object.name[0] === '$' ? object.name.slice(1) : null;
147145

148146
node.contexts.forEach(prop => {
149147
this.block.bindings.set(prop.key.name, {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
async test({ assert, target, window }) {
3+
const input = target.querySelector('input');
4+
5+
const event = new window.Event('input');
6+
input.value = 'changed';
7+
await input.dispatchEvent(event);
8+
9+
assert.htmlEqual(target.innerHTML, `
10+
<input>
11+
<p>changed</p>
12+
`);
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
let itemStore = writable({prop: {things: [{name: "item store"}]}});
5+
</script>
6+
7+
{#each $itemStore.prop.things as thing }
8+
<input bind:value={thing.name} >
9+
{/each}
10+
11+
<p>{$itemStore.prop.things[0].name}</p>

0 commit comments

Comments
 (0)