tsxHelper.tsx 926 B

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