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): handle nested reactive object in guardReactiveProps #11693

Open
wants to merge 8 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
16 changes: 15 additions & 1 deletion packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,25 @@ function _createVNode(
)
}

function generateProps(props: Data & VNodeProps) {
const target: Data & VNodeProps = {}
for (const key in props) {
if (isObject(props[key])) {
target[key] = generateProps(props[key])
} else {
target[key] = props[key]
}
}
return target
}

export function guardReactiveProps(
props: (Data & VNodeProps) | null,
): (Data & VNodeProps) | null {
if (!props) return null
return isProxy(props) || isInternalObject(props) ? extend({}, props) : props
return isProxy(props) || isInternalObject(props)
? generateProps(props)
: props
}

export function cloneVNode<T, U>(
Expand Down
45 changes: 44 additions & 1 deletion packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { patchProp } from '../src/patchProp'
import { h, nextTick, ref, render } from '../src'
import {
createElementBlock,
guardReactiveProps,
h,
nextTick,
normalizeProps,
reactive,
ref,
render,
toRefs,
} from '../src'

describe('runtime-dom: props patching', () => {
test('basic', () => {
Expand Down Expand Up @@ -351,4 +361,37 @@ describe('runtime-dom: props patching', () => {
expect(el.translate).toBeFalsy()
expect(el.getAttribute('translate')).toBe('no')
})

// #11691
test('should update the color style of the element from red to yellow', async () => {
const state = reactive({
obj: {
style: {
color: 'red',
},
},
})
const App = {
setup() {
const { obj } = toRefs(state)
return () => {
// <div v-bind="obj">msg</div>
return createElementBlock(
'div',
normalizeProps(guardReactiveProps(obj.value)),
'msg',
17,
)
}
},
}
const root = document.createElement('div')
render(h(App), root)
const el = root.children[0] as HTMLSelectElement
expect(el.style.color).toBe('red')

state.obj.style.color = 'yellow'
await nextTick()
expect(el.style.color).toBe('yellow')
})
})