提交 | 用户 | age
2f6253 1 import { Slots } from 'vue';
2 import { isFunction } from '/@/utils/is';
a065de 3 import { RenderOpts } from '/@/components/Form';
2f6253 4
5 /**
6  * @description:  Get slot to prevent empty error
7  */
a065de 8 export function getSlot(slots: Slots, slot = 'default', data?: any, opts?: RenderOpts) {
2f6253 9   if (!slots || !Reflect.has(slots, slot)) {
10     return null;
11   }
12   if (!isFunction(slots[slot])) {
13     console.error(`${slot} is not a function!`);
14     return null;
15   }
16   const slotFn = slots[slot];
17   if (!slotFn) return null;
a065de 18   const params = { ...data, ...opts };
L 19   return slotFn(params);
2f6253 20 }
21
22 /**
23  * extends slots
24  * @param slots
ecfb70 25  * @param excludeKeys
2f6253 26  */
27 export function extendSlots(slots: Slots, excludeKeys: string[] = []) {
28   const slotKeys = Object.keys(slots);
29   const ret: any = {};
30   slotKeys.map((key) => {
31     if (excludeKeys.includes(key)) {
32       return null;
33     }
266c33 34     ret[key] = (data?: any) => getSlot(slots, key, data);
2f6253 35   });
36   return ret;
37 }