Skip to content

Commit a2fbef2

Browse files
authored
fix: improve import event handler support (#10592)
* fix: improve import event handler support * simplify
1 parent 66b6244 commit a2fbef2

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

.changeset/yellow-taxis-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: improve import event handler support

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ function serialize_event_handler(node, { state, visit }) {
13141314
binding !== null &&
13151315
(binding.kind === 'state' ||
13161316
binding.kind === 'frozen_state' ||
1317+
binding.declaration_kind === 'import' ||
13171318
binding.kind === 'legacy_reactive' ||
13181319
binding.kind === 'derived' ||
13191320
binding.kind === 'prop' ||
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { test } from '../../test';
2+
import { log, handler, log_a } from './event.js';
3+
4+
export default test({
5+
before_test() {
6+
log.length = 0;
7+
handler.value = log_a;
8+
},
9+
10+
async test({ assert, target }) {
11+
const [b1, b2] = target.querySelectorAll('button');
12+
13+
b1?.click();
14+
assert.deepEqual(log, ['a']);
15+
16+
b2?.click();
17+
b1?.click();
18+
assert.deepEqual(log, ['a', 'b']);
19+
}
20+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @type {any[]} */
2+
export const log = [];
3+
4+
export const log_a = () => {
5+
log.push('a');
6+
};
7+
8+
export const log_b = () => {
9+
log.push('b');
10+
};
11+
12+
export const handler = {
13+
value: log_a
14+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import {handler, log_b} from './event.js';
3+
</script>
4+
5+
<button onclick={handler.value}>click</button>
6+
<button onclick={() => handler.value = log_b}>change</button>

0 commit comments

Comments
 (0)