UNPKG

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