tsxHelper.tsx 819 B

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