Skip to content

Commit 351d7dc

Browse files
committed
fix(): allow setting target event for event actions
1 parent 13468db commit 351d7dc

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/runtime/src/internal/bindListeners.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ describe("listenerFactory for event.*", () => {
679679
listenerFactory(
680680
{
681681
action: "event.preventDefault",
682-
args: ["<% EVENT.detail %>"],
682+
args: ["<% EVENT %>"],
683683
},
684684
runtimeContext
685685
)(event);
@@ -688,14 +688,34 @@ describe("listenerFactory for event.*", () => {
688688

689689
test("event.stopPropagation", () => {
690690
const event = { stopPropagation: jest.fn() } as any;
691+
listenerFactory(
692+
{
693+
action: "event.stopPropagation",
694+
},
695+
runtimeContext
696+
)(event);
697+
expect(event.stopPropagation).toHaveBeenCalledWith();
698+
});
699+
700+
test("non-Event object", () => {
701+
consoleError.mockReturnValue();
702+
const event = {
703+
stopPropagation: jest.fn(),
704+
detail: { someProp: 123 },
705+
} as any;
691706
listenerFactory(
692707
{
693708
action: "event.stopPropagation",
694709
args: ["<% EVENT.detail %>"],
695710
},
696711
runtimeContext
697712
)(event);
713+
expect(consoleError).toHaveBeenCalledWith(
714+
"call event.stopPropagation() on non-Event object:",
715+
{ someProp: 123 }
716+
);
698717
expect(event.stopPropagation).toHaveBeenCalledWith();
718+
consoleError.mockReset();
699719
});
700720
});
701721

packages/runtime/src/internal/bindListeners.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,8 @@ export function listenerFactory(
206206
break;
207207

208208
case "event.preventDefault":
209-
event.preventDefault();
210-
break;
211209
case "event.stopPropagation":
212-
event.stopPropagation();
210+
handleEventAction(event, method, handler.args, runtimeContext);
213211
break;
214212

215213
case "console.log":
@@ -338,6 +336,27 @@ export function listenerFactory(
338336
};
339337
}
340338

339+
function handleEventAction(
340+
event: Event,
341+
method: "preventDefault" | "stopPropagation",
342+
args: unknown[] | undefined,
343+
runtimeContext: RuntimeContext
344+
) {
345+
const computedArgs = argsFactory(args, runtimeContext, event, {
346+
useEventAsDefault: true,
347+
});
348+
const [e] = computedArgs as [Event];
349+
if (e instanceof Event) {
350+
e[method]();
351+
} else {
352+
// eslint-disable-next-line no-console
353+
console.error(`call event.${method}() on non-Event object:`, e);
354+
355+
// For compatibility, fallback to the original event.
356+
event[method]();
357+
}
358+
}
359+
341360
function handleUseProviderAction(
342361
event: Event,
343362
handler: UseProviderEventHandler,

0 commit comments

Comments
 (0)