/* * @Description: tsx 相关函数 * @FilePath: \base-framework\packages\shared\src\utils\tsxHelper.tsx * @Author: freud * @Date: 2023-07-13 15:03:05 * @LastEditTime: 2023-10-27 11:05:00 * @LastEditors: freud * @Reference: */ import { Slots } from 'vue'; import { isFunction } from './inference'; /** * @description: Get slot to prevent empty error */ export function getSlot( slots: Slots, slot = 'default', data?: any, opts?: { disabled: boolean; [key: string]: any; }, ) { if (!slots || !Reflect.has(slots, slot)) { return null; } if (!isFunction(slots[slot])) { console.error(`${slot} is not a function!`); return null; } const slotFn = slots[slot]; if (!slotFn) return null; const params = { ...data, ...opts }; return slotFn(params); } /** * extends slots * @param slots * @param excludeKeys */ export function extendSlots(slots: Slots, excludeKeys: string[] = []) { const slotKeys = Object.keys(slots); const ret: any = {}; slotKeys.map((key) => { if (excludeKeys.includes(key)) { return null; } ret[key] = (data?: any) => getSlot(slots, key, data); }); return ret; }