UNPKG

940 BJavaScriptView Raw
1import { assign } from './util';
2import { EMPTY_ARR } from './constants';
3import { createVNode } from './create-element';
4
5/**
6 * Clones the given VNode, optionally adding attributes/props and replacing its children.
7 * @param {import('./internal').VNode} vnode The virtual DOM element to clone
8 * @param {object} props Attributes/props to add when cloning
9 * @param {Array<import('./index').ComponentChildren>} rest Any additional arguments will be used as replacement children.
10 * @returns {import('./internal').VNode}
11 */
12export function cloneElement(vnode, props) {
13 props = assign(assign({}, vnode.props), props);
14 if (arguments.length > 2) props.children = EMPTY_ARR.slice.call(arguments, 2);
15 let normalizedProps = {};
16 for (const i in props) {
17 if (i !== 'key' && i !== 'ref') normalizedProps[i] = props[i];
18 }
19
20 return createVNode(
21 vnode.type,
22 normalizedProps,
23 props.key || vnode.key,
24 props.ref || vnode.ref,
25 null
26 );
27}