|
|
@ -1,8 +1,16 @@
|
|
|
|
import { filterEmpty } from './props-util';
|
|
|
|
import { filterEmpty } from './props-util';
|
|
|
|
|
|
|
|
import type { VNode, VNodeProps } from 'vue';
|
|
|
|
import { cloneVNode } from 'vue';
|
|
|
|
import { cloneVNode } from 'vue';
|
|
|
|
import warning from './warning';
|
|
|
|
import warning from './warning';
|
|
|
|
|
|
|
|
import type { RefObject } from './createRef';
|
|
|
|
|
|
|
|
|
|
|
|
export function cloneElement(vnode, nodeProps = {}, override = true, mergeRef = false) {
|
|
|
|
export function cloneElement<T, U>(
|
|
|
|
|
|
|
|
vnode: VNode<T, U> | VNode<T, U>[],
|
|
|
|
|
|
|
|
nodeProps: Record<string, any> &
|
|
|
|
|
|
|
|
Omit<VNodeProps, 'ref'> & { ref?: VNodeProps['ref'] | RefObject } = {},
|
|
|
|
|
|
|
|
override = true,
|
|
|
|
|
|
|
|
mergeRef = false,
|
|
|
|
|
|
|
|
): VNode<T, U> {
|
|
|
|
let ele = vnode;
|
|
|
|
let ele = vnode;
|
|
|
|
if (Array.isArray(vnode)) {
|
|
|
|
if (Array.isArray(vnode)) {
|
|
|
|
ele = filterEmpty(vnode)[0];
|
|
|
|
ele = filterEmpty(vnode)[0];
|
|
|
@ -10,10 +18,10 @@ export function cloneElement(vnode, nodeProps = {}, override = true, mergeRef =
|
|
|
|
if (!ele) {
|
|
|
|
if (!ele) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const node = cloneVNode(ele, nodeProps, mergeRef);
|
|
|
|
const node = cloneVNode(ele as VNode<T, U>, nodeProps as any, mergeRef);
|
|
|
|
|
|
|
|
|
|
|
|
// cloneVNode内部是合并属性,这里改成覆盖属性
|
|
|
|
// cloneVNode内部是合并属性,这里改成覆盖属性
|
|
|
|
node.props = override ? { ...node.props, ...nodeProps } : node.props;
|
|
|
|
node.props = (override ? { ...node.props, ...nodeProps } : node.props) as any;
|
|
|
|
warning(typeof node.props.class !== 'object', 'class must be string');
|
|
|
|
warning(typeof node.props.class !== 'object', 'class must be string');
|
|
|
|
return node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|