Skip to content

fix(runtime-core): handle prop starts with on, but is not an event handler #7872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
26 changes: 24 additions & 2 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
transformVNodeArgs,
} from '../src/vnode'
import type { Data } from '../src/component'
import { PatchFlags, ShapeFlags } from '@vue/shared'
import { PatchFlags, ShapeFlags, isArray } from '@vue/shared'
import { h, isReactive, reactive, ref, setBlockTracking, withCtx } from '../src'
import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
import { setCurrentRenderingInstance } from '../src/componentRenderContext'
Expand Down Expand Up @@ -478,11 +478,33 @@ describe('vnode', () => {
let props1: Data = { foo: 'c' }
let props2: Data = { foo: {}, bar: ['cc'] }
let props3: Data = { baz: { ccc: true } }
expect(mergeProps(props1, props2, props3)).toMatchObject({
let props4: Data = { 'on-foo': false }
let props5: Data = { 'on-bar': 0 }
let props6: Data = { 'on-baz': () => {} }
let props7: Data = { 'on-x': '' }
let props8: Data = { 'on-baz': () => {} }
const props = mergeProps(
props1,
props2,
props3,
props4,
props5,
props6,
props7,
props8,
)
expect(props).toMatchObject({
foo: {},
bar: ['cc'],
baz: { ccc: true },
'on-foo': false,
'on-bar': 0,
'on-x': '',
})

// should merge props6 and prop8 into an array
expect(isArray(props['on-baz'])).equal(true)
expect((props['on-baz'] as any).length).toBe(2)
})
})

Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,16 @@ export function mergeProps(...args: (Data & VNodeProps)[]): Data {
const incoming = toMerge[key]
if (
incoming &&
(isFunction(incoming) ||
(isArray(incoming) && incoming.every(isFunction))) &&
existing !== incoming &&
!(isArray(existing) && existing.includes(incoming))
) {
ret[key] = existing
? [].concat(existing as any, incoming as any)
: incoming
} else if (incoming !== undefined) {
ret[key] = incoming
}
} else if (key !== '') {
ret[key] = toMerge[key]
Expand Down