MCPcopy
hub / github.com/NervJS/nerv / cloneElement

Function cloneElement

packages/nerv/src/clone-element.ts:7–59  ·  view source on GitHub ↗
(vnode, props?: object, ...children)

Source from the content-addressed store, hash-verified

5import { createVoid } from './vdom/create-void'
6
7export default function cloneElement (vnode, props?: object, ...children): any {
8 if (isVText(vnode)) {
9 return createVText(vnode.text)
10 }
11 if (isString(vnode) || isNumber(vnode)) {
12 return createVText(vnode)
13 }
14 if (isInvalid(vnode)
15 || (!isInvalid(vnode) && isPortal(vnode.vtype, vnode))
16 || (vnode && (vnode.vtype & VType.Void))) {
17 return createVoid()
18 }
19 const properties = clone(extend(clone(vnode.props), props))
20 if (vnode.namespace) {
21 properties.namespace = vnode.namespace
22 }
23 if ((vnode.vtype & VType.Composite) && !isNullOrUndef(vnode.ref)) {
24 properties.ref = vnode.ref
25 }
26 let childrenTmp =
27 (arguments.length > 2 ?
28 [].slice.call(arguments, 2) :
29 vnode.children || properties.children) || []
30 if (childrenTmp.length) {
31 if (childrenTmp.length === 1) {
32 childrenTmp = children[0]
33 }
34 }
35 if (isArray(vnode)) {
36 return vnode.map((item) => {
37 return cloneElement(item)
38 })
39 }
40 const newVNode = createElement(vnode.type, properties)
41 if (isArray(childrenTmp)) {
42 let _children = childrenTmp.map((child) => {
43 return cloneElement(child, child.props)
44 })
45 if (_children.length === 0) {
46 _children = EMPTY_CHILDREN
47 }
48 if (isVNode(newVNode)) {
49 newVNode.children = _children
50 }
51 newVNode.props.children = _children
52 } else if (childrenTmp) {
53 if (isVNode(newVNode)) {
54 newVNode.children = cloneElement(childrenTmp)
55 }
56 newVNode.props.children = cloneElement(childrenTmp, childrenTmp.props)
57 }
58 return newVNode
59}

Callers 2

component.spec.jsFile · 0.90

Calls 12

isVTextFunction · 0.90
isStringFunction · 0.90
isNumberFunction · 0.90
isInvalidFunction · 0.90
isPortalFunction · 0.90
createVoidFunction · 0.90
cloneFunction · 0.90
isNullOrUndefFunction · 0.90
isVNodeFunction · 0.90
createVTextFunction · 0.85
extendFunction · 0.85
createElementFunction · 0.70

Tested by

no test coverage detected