-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve bind:this support for each blocks (#10510)
- Loading branch information
Showing
6 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte": patch | ||
--- | ||
|
||
fix: improve bind:this support for each blocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/svelte/tests/runtime-runes/samples/state-each-bind-this/Paragraph.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
const { text } = $props(); | ||
let boundParagraph = $state(); | ||
export function changeBackgroundToRed() { | ||
boundParagraph.style.backgroundColor = 'red'; | ||
} | ||
</script> | ||
|
||
<p bind:this={boundParagraph}> | ||
{text} | ||
</p> |
35 changes: 35 additions & 0 deletions
35
packages/svelte/tests/runtime-runes/samples/state-each-bind-this/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { flushSync } from '../../../../src/main/main-client'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
async test({ assert, target }) { | ||
const [btn, btn2, btn3] = target.querySelectorAll('button'); | ||
|
||
flushSync(() => { | ||
btn?.click(); | ||
}); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
`<div><p style="background-color: red;">b1</p><button>change</button><button>delete</button></div><div><p>b2</p><button>change</button><button>delete</button></div>` | ||
); | ||
|
||
flushSync(() => { | ||
btn2?.click(); | ||
}); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
`<div><p>b2</p><button>change</button><button>delete</button></div>` | ||
); | ||
|
||
flushSync(() => { | ||
btn3?.click(); | ||
}); | ||
|
||
assert.htmlEqual( | ||
target.innerHTML, | ||
`<div><p style="background-color: red;">b2</p><button>change</button><button>delete</button></div>` | ||
); | ||
} | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/svelte/tests/runtime-runes/samples/state-each-bind-this/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script> | ||
import Paragraph from './Paragraph.svelte'; | ||
let boundParagraphs = $state([]); | ||
let store = $state([ | ||
{ id: 1, text: 'b1' }, | ||
{ id: 2, text: 'b2' } | ||
]); | ||
</script> | ||
|
||
{#each store as text, i (text.id)} | ||
<div> | ||
<Paragraph bind:this={boundParagraphs[i]} text={text.text}></Paragraph> | ||
<button onclick={() => boundParagraphs[i].changeBackgroundToRed()}> | ||
change | ||
</button> | ||
<button onclick={() => store.splice(store.indexOf(text), 1)}> | ||
delete | ||
</button> | ||
</div> | ||
{/each} |