Skip to content
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

fix(runtime-core): improve dedupe listeners when attr fallthrough #4912

Merged
merged 2 commits into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts
Expand Up @@ -11,7 +11,8 @@ import {
createBlock,
FunctionalComponent,
createCommentVNode,
Fragment
Fragment,
withModifiers
} from '@vue/runtime-dom'
import { PatchFlags } from '@vue/shared/src'

Expand Down Expand Up @@ -383,6 +384,45 @@ describe('attribute fallthrough', () => {
expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
})

it('should dedupe same listeners when $attrs is used during render', () => {
const click = jest.fn()
const count = ref(0)

function inc() {
count.value++
click()
}

const Parent = {
render() {
return h(Child, { onClick: inc })
}
}

const Child = defineComponent({
render() {
return h(
'div',
mergeProps(
{
onClick: withModifiers(() => {}, ['prevent', 'stop'])
},
this.$attrs
)
)
}
})

const root = document.createElement('div')
document.body.appendChild(root)
render(h(Parent), root)

const node = root.children[0] as HTMLElement
node.dispatchEvent(new CustomEvent('click'))
expect(click).toHaveBeenCalledTimes(1)
expect(count.value).toBe(1)
})

it('should not warn when $attrs is used during render', () => {
const Parent = {
render() {
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/vnode.ts
Expand Up @@ -791,7 +791,10 @@ export function mergeProps(...args: (Data & VNodeProps)[]) {
} else if (isOn(key)) {
const existing = ret[key]
const incoming = toMerge[key]
if (existing !== incoming) {
if (
existing !== incoming &&
!(isArray(existing) && existing.includes(incoming))
) {
ret[key] = existing
? [].concat(existing as any, incoming as any)
: incoming
Expand Down