-
-
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: ensure dynamic event handlers are wrapped in a derived (#12563)
* fix: ensure dynamic event handlers are wrapped in a derived * fix test * feedback * more feedback * address feedback * we have .svelte.js files --------- Co-authored-by: Rich Harris <[email protected]>
- Loading branch information
1 parent
d73c5b8
commit d17755a
Showing
8 changed files
with
99 additions
and
36 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: ensure dynamic event handlers are wrapped in a derived |
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
15 changes: 15 additions & 0 deletions
15
packages/svelte/tests/runtime-legacy/samples/dynamic-element-event-handler3/_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,15 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
html: '<button>Click</button>', | ||
|
||
test({ assert, logs, target }) { | ||
const button = target.querySelector('button'); | ||
|
||
button?.click(); | ||
button?.click(); | ||
button?.click(); | ||
|
||
assert.deepEqual(logs, ['create', 'trigger', 'trigger', 'trigger']); | ||
} | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/svelte/tests/runtime-legacy/samples/dynamic-element-event-handler3/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,9 @@ | ||
<script> | ||
let makeHandler = null; | ||
makeHandler = () => { | ||
console.log('create'); | ||
return () => console.log('trigger'); | ||
}; | ||
</script> | ||
|
||
<button on:click={makeHandler()}>Click</button> |
18 changes: 8 additions & 10 deletions
18
packages/svelte/tests/runtime-runes/samples/event-attribute-import/_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 |
---|---|---|
@@ -1,20 +1,18 @@ | ||
import { test } from '../../test'; | ||
import { log, handler, log_a } from './event.js'; | ||
|
||
export default test({ | ||
before_test() { | ||
log.length = 0; | ||
handler.value = log_a; | ||
}, | ||
|
||
async test({ assert, target }) { | ||
const [b1, b2] = target.querySelectorAll('button'); | ||
async test({ assert, logs, target, component }) { | ||
const [b1, b2, b3] = target.querySelectorAll('button'); | ||
|
||
b1?.click(); | ||
assert.deepEqual(log, ['a']); | ||
assert.deepEqual(logs, ['a']); | ||
|
||
b2?.click(); | ||
b1?.click(); | ||
assert.deepEqual(log, ['a', 'b']); | ||
|
||
b3?.click(); | ||
b1?.click(); | ||
|
||
assert.deepEqual(logs, ['a', 'b', 'a']); | ||
} | ||
}); |
14 changes: 0 additions & 14 deletions
14
packages/svelte/tests/runtime-runes/samples/event-attribute-import/event.js
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/svelte/tests/runtime-runes/samples/event-attribute-import/event.svelte.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,18 @@ | ||
export const log_a = () => { | ||
console.log('a'); | ||
}; | ||
|
||
export const log_b = () => { | ||
console.log('b'); | ||
}; | ||
|
||
let handle = $state(log_a); | ||
|
||
export const handler = { | ||
get value() { | ||
return handle; | ||
}, | ||
set value(v) { | ||
handle = v; | ||
} | ||
}; |
5 changes: 3 additions & 2 deletions
5
packages/svelte/tests/runtime-runes/samples/event-attribute-import/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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<script> | ||
import {handler, log_b} from './event.js'; | ||
import { handler, log_a, log_b } from './event.svelte.js'; | ||
</script> | ||
|
||
<button onclick={handler.value}>click</button> | ||
<button onclick={() => handler.value = log_b}>change</button> | ||
<button onclick={() => (handler.value = log_b)}>change</button> | ||
<button onclick={() => (handler.value = log_a)}>change back</button> |