(vnode, props?: object, ...children)
| 5 | import { createVoid } from './vdom/create-void' |
| 6 | |
| 7 | export 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 | } |
no test coverage detected