Skip to content

Commit 1e3e1ef

Browse files
authored
fix(compiler-vapor): wrap handler values in functions for dynamic v-on (#14218)
1 parent ddc1bae commit 1e3e1ef

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ exports[`compiler: element transform > component > v-on="obj" 1`] = `
291291
export function render(_ctx) {
292292
const _component_Foo = _resolveComponent("Foo")
293293
const n0 = _createComponentWithFallback(_component_Foo, { $: [
294-
() => (_toHandlers(_ctx.obj))
294+
() => (_toHandlers(_ctx.obj, false, true))
295295
] }, null, true)
296296
return n0
297297
}"

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformSlotOutlet.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export function render(_ctx) {
181181
const n0 = _createSlot("default", {
182182
onClick: () => _ctx.foo,
183183
$: [
184-
() => (_toHandlers(_ctx.bar)),
184+
() => (_toHandlers(_ctx.bar, false, true)),
185185
{ baz: () => (_ctx.qux) }
186186
]
187187
})

packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('compiler: element transform', () => {
364364
const { code, ir } = compileWithElementTransform(`<Foo v-on="obj" />`)
365365
expect(code).toMatchSnapshot()
366366
expect(code).contains(`[
367-
() => (_toHandlers(_ctx.obj))
367+
() => (_toHandlers(_ctx.obj, false, true))
368368
]`)
369369
expect(ir.block.dynamic.children[0].operation).toMatchObject({
370370
type: IRNodeTypes.CREATE_COMPONENT_NODE,

packages/compiler-vapor/src/generators/component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ function genDynamicProps(
219219
expr = genMulti(DELIMITERS_OBJECT, genProp(p, context))
220220
else {
221221
expr = genExpression(p.value, context)
222-
if (p.handler) expr = genCall(helper('toHandlers'), expr)
222+
if (p.handler)
223+
expr = genCall(
224+
helper('toHandlers'),
225+
expr,
226+
`false`, // preserveCaseIfNecessary: false, not needed for component
227+
`true`, // wrap handler values in functions
228+
)
223229
}
224230
}
225231
frags.push(['() => (', ...expr, ')'])

packages/runtime-core/src/helpers/toHandlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { warn } from '../warning'
88
export function toHandlers(
99
obj: Record<string, any>,
1010
preserveCaseIfNecessary?: boolean,
11+
needWrap?: boolean,
1112
): Record<string, any> {
1213
const ret: Record<string, any> = {}
1314
if (__DEV__ && !isObject(obj)) {
@@ -19,7 +20,7 @@ export function toHandlers(
1920
preserveCaseIfNecessary && /[A-Z]/.test(key)
2021
? `on:${key}`
2122
: toHandlerKey(key)
22-
] = obj[key]
23+
] = needWrap ? () => obj[key] : obj[key]
2324
}
2425
return ret
2526
}

packages/runtime-vapor/__tests__/componentEmits.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ describe('component: emit', () => {
8787
expect(fooSpy).toHaveBeenCalledTimes(1)
8888
})
8989

90-
// #3527
9190
test('trigger mixed case handlers', () => {
9291
const { render } = define({
9392
setup(_, { emit }) {
@@ -100,10 +99,17 @@ describe('component: emit', () => {
10099
const fooSpy = vi.fn()
101100
const barSpy = vi.fn()
102101
render(
103-
toHandlers({
104-
'test-event': () => fooSpy,
105-
testEvent: () => barSpy,
106-
}),
102+
// v-on="{ testEvent: handler }"
103+
// will be compiled to
104+
// toHandlers({ testEvent: handler }, false, true)
105+
toHandlers(
106+
{
107+
'test-event': fooSpy,
108+
testEvent: barSpy,
109+
},
110+
false,
111+
true,
112+
),
107113
)
108114
expect(fooSpy).toHaveBeenCalledTimes(1)
109115
expect(barSpy).toHaveBeenCalledTimes(1)

0 commit comments

Comments
 (0)