ant-design-vue/components/_util/vnode.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-06-10 10:21:16 +00:00
import { filterEmpty } from './props-util';
import type { Slots, VNode, VNodeArrayChildren, VNodeProps } from 'vue';
import { cloneVNode, isVNode, Comment, Fragment, render as VueRender } from 'vue';
2020-07-20 08:29:46 +00:00
import warning from './warning';
2022-03-20 02:57:44 +00:00
import type { RefObject } from './createRef';
type NodeProps = Record<string, any> &
Omit<VNodeProps, 'ref'> & { ref?: VNodeProps['ref'] | RefObject };
2019-07-24 11:21:27 +00:00
2022-03-20 02:57:44 +00:00
export function cloneElement<T, U>(
vnode: VNode<T, U> | VNode<T, U>[],
nodeProps: NodeProps = {},
2022-03-20 02:57:44 +00:00
override = true,
mergeRef = false,
): VNode<T, U> {
2020-07-06 14:31:07 +00:00
let ele = vnode;
if (Array.isArray(vnode)) {
ele = filterEmpty(vnode)[0];
2018-01-29 10:57:20 +00:00
}
if (!ele) {
2019-01-12 03:33:27 +00:00
return null;
2018-01-29 10:57:20 +00:00
}
2022-03-20 02:57:44 +00:00
const node = cloneVNode(ele as VNode<T, U>, nodeProps as any, mergeRef);
2018-03-03 11:14:03 +00:00
2020-06-10 10:21:16 +00:00
// cloneVNode内部是合并属性这里改成覆盖属性
2022-03-20 02:57:44 +00:00
node.props = (override ? { ...node.props, ...nodeProps } : node.props) as any;
2020-10-02 07:44:10 +00:00
warning(typeof node.props.class !== 'object', 'class must be string');
2019-01-12 03:33:27 +00:00
return node;
2017-12-14 04:13:15 +00:00
}
2020-07-06 14:31:07 +00:00
2020-10-02 07:44:10 +00:00
export function cloneVNodes(vnodes, nodeProps = {}, override = true) {
2020-07-06 14:31:07 +00:00
return vnodes.map(vnode => cloneElement(vnode, nodeProps, override));
}
export function deepCloneElement<T, U>(
vnode: VNode<T, U> | VNode<T, U>[],
nodeProps: NodeProps = {},
override = true,
mergeRef = false,
) {
if (Array.isArray(vnode)) {
return vnode.map(item => deepCloneElement(item, nodeProps, override, mergeRef));
} else {
// 需要判断是否为vnode方可进行clone操作
if (!isVNode(vnode)) {
return vnode;
}
const cloned = cloneElement(vnode, nodeProps, override, mergeRef);
if (Array.isArray(cloned.children)) {
cloned.children = deepCloneElement(cloned.children as VNode<T, U>[]);
}
return cloned;
}
}
export function triggerVNodeUpdate(vm: VNode, attrs: Record<string, any>, dom: any) {
VueRender(cloneVNode(vm, { ...attrs }), dom);
}
const ensureValidVNode = (slot: VNodeArrayChildren | null) => {
return (slot || []).some(child => {
if (!isVNode(child)) return true;
if (child.type === Comment) return false;
if (child.type === Fragment && !ensureValidVNode(child.children as VNodeArrayChildren))
return false;
return true;
})
? slot
: null;
};
export function customRenderSlot(
slots: Slots,
name: string,
props: Record<string, unknown>,
fallback?: () => VNodeArrayChildren,
) {
const slot = slots[name]?.(props);
if (ensureValidVNode(slot)) {
return slot;
}
return fallback?.();
}