tsxHelper.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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] = () => getSlot(slots, key);
  31. });
  32. return ret;
  33. }
  34. // Get events on attrs
  35. export function getListeners(attrs: Record<string, unknown>) {
  36. const listeners: any = {};
  37. Object.keys(attrs).forEach((key) => {
  38. if (/^on/.test(key)) {
  39. listeners[key] = attrs[key];
  40. }
  41. });
  42. return listeners;
  43. }