Blame view

src/utils/helper/tsxHelper.tsx 928 Bytes
陈文彬 authored
1
2
import { Slots } from 'vue';
import { isFunction } from '/@/utils/is';
3
import { RenderOpts } from '/@/components/Form';
陈文彬 authored
4
5
6
7

/**
 * @description:  Get slot to prevent empty error
 */
8
export function getSlot(slots: Slots, slot = 'default', data?: any, opts?: RenderOpts) {
陈文彬 authored
9
10
11
12
13
14
15
16
17
  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;
18
19
  const params = { ...data, ...opts };
  return slotFn(params);
陈文彬 authored
20
21
22
23
24
}

/**
 * extends slots
 * @param slots
vben authored
25
 * @param excludeKeys
陈文彬 authored
26
27
28
29
30
31
32
33
 */
export function extendSlots(slots: Slots, excludeKeys: string[] = []) {
  const slotKeys = Object.keys(slots);
  const ret: any = {};
  slotKeys.map((key) => {
    if (excludeKeys.includes(key)) {
      return null;
    }
34
    ret[key] = (data?: any) => getSlot(slots, key, data);
陈文彬 authored
35
36
37
  });
  return ret;
}