Skip to content

fix(runtime-core): should not fallthrough attr if it has been declared as a prop in the root component #8976

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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,4 +729,39 @@ describe('attribute fallthrough', () => {
expect(textBar).toBe('from GrandChild')
expect(textFoo).toBe('from Child')
})

// #9039
it('should not fallthrough attr if it has been declared as a prop in the root component', () => {
const App = defineComponent({
setup() {
return () =>
h(Child, {
foo: '123'
})
}
})

const Child = defineComponent({
setup(_props) {
const foo = ref('456')
return () =>
h(GrandChild, {
foo: foo.value
})
}
})
const GrandChild = defineComponent({
props: ['foo'],
setup(_props) {
return () => h('span', null, _props.foo)
}
})

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

const node = root.children[0] as HTMLElement
expect(node.innerHTML).toBe('456')
})
})
29 changes: 26 additions & 3 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
ComponentInternalInstance,
FunctionalComponent,
Data,
getComponentName
getComponentName,
ConcreteComponent
} from './component'
import {
VNode,
Expand All @@ -15,7 +16,14 @@ import {
blockStack
} from './vnode'
import { handleError, ErrorCodes } from './errorHandling'
import { PatchFlags, ShapeFlags, isOn, isModelListener } from '@vue/shared'
import {
PatchFlags,
ShapeFlags,
isOn,
isModelListener,
isObject,
isArray
} from '@vue/shared'
import { warn } from './warning'
import { isHmrUpdating } from './hmr'
import { NormalizedProps } from './componentProps'
Expand Down Expand Up @@ -133,8 +141,23 @@ export function renderComponentRoot(
}

if (fallthroughAttrs && inheritAttrs !== false) {
const { shapeFlag, type, props } = root
// fix #8969 should not fallthrough attr if it has been declared as a prop in the root component
if (shapeFlag & ShapeFlags.COMPONENT && props) {
Object.keys(fallthroughAttrs).forEach(key => {
if (key in props) {
const propsDef = (type as ConcreteComponent).props
if (
propsDef &&
((isObject(propsDef) && key in propsDef) ||
(isArray(propsDef) && propsDef.includes(key)))
)
delete fallthroughAttrs![key]
}
})
}

const keys = Object.keys(fallthroughAttrs)
const { shapeFlag } = root
if (keys.length) {
if (shapeFlag & (ShapeFlags.ELEMENT | ShapeFlags.COMPONENT)) {
if (propsOptions && keys.some(isModelListener)) {
Expand Down