Skip to content

Commit c5b92b1

Browse files
committed
fix handleEvent this
1 parent 502a772 commit c5b92b1

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

packages/svelte/src/internal/client/dom/elements/events.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ export function call_event_handler(handler, current_target, event, data = []) {
5757
if (typeof handler === 'function') {
5858
handler.call(current_target, event, ...data);
5959
} else {
60-
if (handler && handler.handleEvent == null) {
61-
// @ts-expect-error - do so to get a nicer "handler.handleEvent is not a function" error instead of "Cannot read properties of null (reading 'call')"
62-
handler.handleEvent();
63-
} else {
64-
handler?.handleEvent.call(current_target, event);
65-
}
60+
handler?.handleEvent(event);
6661
}
6762
}
6863

packages/svelte/tests/runtime-runes/samples/event-handler-object-dev/main.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
let count = $state(0);
33
44
let onclick = $state({
5-
handleEvent() {
6-
count += +this.dataset.step;
5+
handleEvent(ev) {
6+
count += +ev.currentTarget.dataset.step;
77
}
88
});
99
</script>

packages/svelte/tests/runtime-runes/samples/event-handler-object/_config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@ export default test({
1313
flushSync();
1414
buttons.forEach((b) => b.click());
1515
flushSync();
16-
assert.deepEqual(logs, [true, true, 'mutated', 'mutated', 'assigned', 'assigned']);
16+
assert.deepEqual(logs, [
17+
'click',
18+
true,
19+
'click',
20+
true,
21+
'mutated',
22+
true,
23+
'mutated',
24+
true,
25+
'assigned',
26+
true,
27+
'assigned',
28+
true
29+
]);
1730
assert.htmlEqual(target.innerHTML, '<button>a</button><button>b</button><button>next</button>');
1831
}
1932
});

packages/svelte/tests/runtime-runes/samples/event-handler-object/main.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
44
let onclick = $state.raw({
55
handleEvent(ev) {
6-
console.log(this === ev.currentTarget);
6+
console.log(ev.type, this === onclick);
77
}
88
});
99
1010
function click2() {
11-
console.log("mutated");
11+
console.log("mutated", this === onclick);
1212
}
1313
1414
function click3() {
15-
console.log("assigned");
15+
console.log("assigned", this === onclick);
1616
}
1717
1818
let btn;

0 commit comments

Comments
 (0)