import { shallowRef } from 'vue' import { flattedChildren, isVNode } from '@element-plus/utils' import type { ComponentInternalInstance, VNode } from 'vue' const getOrderedChildren = ( vm: ComponentInternalInstance, childComponentName: string, children: Record ): T[] => { const nodes = flattedChildren(vm.subTree).filter( (n): n is VNode => isVNode(n) && (n.type as any)?.name === childComponentName && !!n.component ) const uids = nodes.map((n) => n.component!.uid) return uids.map((uid) => children[uid]).filter((p) => !!p) } export const useOrderedChildren = ( vm: ComponentInternalInstance, childComponentName: string ) => { const children: Record = {} const orderedChildren = shallowRef([]) const addChild = (child: T) => { children[child.uid] = child orderedChildren.value = getOrderedChildren(vm, childComponentName, children) } const removeChild = (uid: number) => { delete children[uid] orderedChildren.value = orderedChildren.value.filter( (children) => children.uid !== uid ) } return { children: orderedChildren, addChild, removeChild, } }