fix(runtime-core): fix key/ref resolution for cloneVNode

fix #1041
This commit is contained in:
Evan You 2020-04-24 12:42:46 -04:00
parent dcf2458fa8
commit d7379c7647
2 changed files with 33 additions and 9 deletions

View File

@ -198,6 +198,24 @@ describe('vnode', () => {
expect(cloned2).toEqual(node2) expect(cloned2).toEqual(node2)
expect(cloneVNode(node2)).toEqual(node2) expect(cloneVNode(node2)).toEqual(node2)
expect(cloneVNode(node2)).toEqual(cloned2) expect(cloneVNode(node2)).toEqual(cloned2)
// #1041 should use reoslved key/ref
expect(cloneVNode(createVNode('div', { key: 1 })).key).toBe(1)
expect(cloneVNode(createVNode('div', { key: 1 }), { key: 2 }).key).toBe(2)
expect(cloneVNode(createVNode('div'), { key: 2 }).key).toBe(2)
// ref normalizes to [currentRenderingInstance, ref]
expect(cloneVNode(createVNode('div', { ref: 'foo' })).ref).toEqual([
null,
'foo'
])
expect(
cloneVNode(createVNode('div', { ref: 'foo' }), { ref: 'bar' }).ref
).toEqual([null, 'bar'])
expect(cloneVNode(createVNode('div'), { ref: 'bar' }).ref).toEqual([
null,
'bar'
])
}) })
describe('mergeProps', () => { describe('mergeProps', () => {

View File

@ -312,9 +312,9 @@ function _createVNode(
_isVNode: true, _isVNode: true,
type, type,
props, props,
key: props && props.key !== undefined ? props.key : null, key: props && props.key != null ? props.key : null,
ref: ref:
props && props.ref !== undefined props && props.ref != null
? [currentRenderingInstance!, props.ref] ? [currentRenderingInstance!, props.ref]
: null, : null,
scopeId: currentScopeId, scopeId: currentScopeId,
@ -362,18 +362,24 @@ export function cloneVNode<T, U>(
vnode: VNode<T, U>, vnode: VNode<T, U>,
extraProps?: Data & VNodeProps extraProps?: Data & VNodeProps
): VNode<T, U> { ): VNode<T, U> {
const props = (extraProps
? vnode.props
? mergeProps(vnode.props, extraProps)
: extend({}, extraProps)
: vnode.props) as any
// This is intentionally NOT using spread or extend to avoid the runtime // This is intentionally NOT using spread or extend to avoid the runtime
// key enumeration cost. // key enumeration cost.
return { return {
_isVNode: true, _isVNode: true,
type: vnode.type, type: vnode.type,
props: extraProps props,
? vnode.props key: props && props.key != null ? props.key : null,
? mergeProps(vnode.props, extraProps) ref:
: extend({}, extraProps) props && props.ref != null
: vnode.props, ? isArray(props.ref)
key: vnode.key, ? props.ref
ref: vnode.ref, : [currentRenderingInstance!, props.ref]
: null,
scopeId: vnode.scopeId, scopeId: vnode.scopeId,
children: vnode.children, children: vnode.children,
target: vnode.target, target: vnode.target,