useTableForm.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import type { ComputedRef, Slots } from 'vue';
  2. import type { BasicTableProps, FetchParams } from '../types/table';
  3. import { unref, computed } from 'vue';
  4. import type { FormProps } from '/@/components/Form';
  5. import { isFunction } from '/@/utils/is';
  6. export function useTableForm(
  7. propsRef: ComputedRef<BasicTableProps>,
  8. slots: Slots,
  9. fetch: (opt?: FetchParams | undefined) => Promise<void>
  10. ) {
  11. const getFormProps = computed(
  12. (): Partial<FormProps> => {
  13. const { formConfig } = unref(propsRef);
  14. return {
  15. showAdvancedButton: true,
  16. ...formConfig,
  17. compact: true,
  18. };
  19. }
  20. );
  21. const getFormSlotKeys = computed(() => {
  22. const keys = Object.keys(slots);
  23. return keys.map((item) => (item.startsWith('form-') ? item : null)).filter(Boolean);
  24. });
  25. function replaceFormSlotKey(key: string) {
  26. if (!key) return '';
  27. return key?.replace?.(/form\-/, '') ?? '';
  28. }
  29. function handleSearchInfoChange(info: Recordable) {
  30. const { handleSearchInfoFn } = unref(propsRef);
  31. if (handleSearchInfoFn && isFunction(handleSearchInfoFn)) {
  32. info = handleSearchInfoFn(info) || info;
  33. }
  34. fetch({ searchInfo: info, page: 1 });
  35. }
  36. return {
  37. getFormProps,
  38. replaceFormSlotKey,
  39. getFormSlotKeys,
  40. handleSearchInfoChange,
  41. };
  42. }