ant-design-vue/components/_util/vnode.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-12-26 19:04:28 +08:00
import clonedeep from 'lodash.clonedeep'
2017-12-14 12:13:15 +08:00
export function cloneVNode (vnode, deep) {
const cloned = new vnode.constructor(
vnode.tag,
2017-12-26 19:04:28 +08:00
clonedeep(vnode.data),
2017-12-14 12:13:15 +08:00
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
2017-12-26 19:04:28 +08:00
clonedeep(vnode.componentOptions),
2017-12-14 12:13:15 +08:00
vnode.asyncFactory
)
cloned.ns = vnode.ns
cloned.isStatic = vnode.isStatic
cloned.key = vnode.key
cloned.isComment = vnode.isComment
cloned.isCloned = true
if (deep && vnode.children) {
cloned.children = cloneVNodes(vnode.children)
}
return cloned
}
export function cloneVNodes (vnodes, deep) {
const len = vnodes.length
const res = new Array(len)
for (let i = 0; i < len; i++) {
res[i] = cloneVNode(vnodes[i], deep)
}
return res
}
export function cloneElement (node, nodeProps) {
2017-12-26 19:04:28 +08:00
const { props, key } = nodeProps
2017-12-14 12:13:15 +08:00
if (node.componentOptions) {
Object.assign(node.componentOptions.propsData, props)
}
2017-12-25 18:08:36 +08:00
const data = node.data || {}
const { style = data.style,
class: cls = data.class,
attrs = data.attrs,
on = data.on,
} = nodeProps
2017-12-26 19:04:28 +08:00
node.data = Object.assign(data, { style, attrs, class: cls, on })
2017-12-14 12:13:15 +08:00
if (key !== undefined) {
node.key = key
}
return node
}