useForm.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import type { FormProps, FormActionType, UseFormReturnType, FormSchema } from '../types/form';
  2. import type { NamePath, ValidateOptions } from 'ant-design-vue/lib/form/interface';
  3. import type { DynamicProps } from '/#/utils';
  4. import { handleRangeValue } from '../utils/formUtils';
  5. import { ref, onUnmounted, unref, nextTick, watch } from 'vue';
  6. import { isProdMode } from '/@/utils/env';
  7. import { error } from '/@/utils/log';
  8. import { getDynamicProps, getValueType } from '/@/utils';
  9. // import { add } from "/@/components/Form/src/componentMap";
  10. //集成online专用控件
  11. // import { OnlineSelectCascade, LinkTableCard, LinkTableSelect } from '@jeecg/online';
  12. export declare type ValidateFields = (nameList?: NamePath[], options?: ValidateOptions) => Promise<Recordable>;
  13. type Props = Partial<DynamicProps<FormProps>>;
  14. export function useForm(props?: Props): UseFormReturnType {
  15. const formRef = ref<Nullable<FormActionType>>(null);
  16. const loadedRef = ref<Nullable<boolean>>(false);
  17. //集成online专用控件
  18. // add("OnlineSelectCascade", OnlineSelectCascade)
  19. // add("LinkTableCard", LinkTableCard)
  20. // add("LinkTableSelect", LinkTableSelect)
  21. async function getForm() {
  22. const form = unref(formRef);
  23. if (!form) {
  24. error('The form instance has not been obtained, please make sure that the form has been rendered when performing the form operation!');
  25. }
  26. await nextTick();
  27. return form as FormActionType;
  28. }
  29. function register(instance: FormActionType) {
  30. isProdMode() &&
  31. onUnmounted(() => {
  32. formRef.value = null;
  33. loadedRef.value = null;
  34. });
  35. if (unref(loadedRef) && isProdMode() && instance === unref(formRef)) return;
  36. formRef.value = instance;
  37. loadedRef.value = true;
  38. watch(
  39. () => props,
  40. () => {
  41. props && instance.setProps(getDynamicProps(props));
  42. },
  43. {
  44. immediate: true,
  45. deep: true,
  46. }
  47. );
  48. }
  49. const methods: FormActionType = {
  50. scrollToField: async (name: NamePath, options?: ScrollOptions | undefined) => {
  51. const form = await getForm();
  52. form.scrollToField(name, options);
  53. },
  54. setProps: async (formProps: Partial<FormProps>) => {
  55. const form = await getForm();
  56. form.setProps(formProps);
  57. },
  58. updateSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
  59. const form = await getForm();
  60. form.updateSchema(data);
  61. },
  62. resetSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
  63. const form = await getForm();
  64. form.resetSchema(data);
  65. },
  66. clearValidate: async (name?: string | string[]) => {
  67. const form = await getForm();
  68. form.clearValidate(name);
  69. },
  70. resetFields: async () => {
  71. getForm().then(async (form) => {
  72. await form.resetFields();
  73. });
  74. },
  75. removeSchemaByFiled: async (field: string | string[]) => {
  76. unref(formRef)?.removeSchemaByFiled(field);
  77. },
  78. // TODO promisify
  79. getFieldsValue: <T>() => {
  80. //update-begin-author:taoyan date:2022-7-5 for: VUEN-1341【流程】编码方式 流程节点编辑表单时,填写数据报错 包括用户组件、部门组件、省市区
  81. let values = unref(formRef)?.getFieldsValue() as T;
  82. if (values) {
  83. Object.keys(values).map((key) => {
  84. if (values[key] instanceof Array) {
  85. // update-begin-author:sunjianlei date:20221205 for: 【issues/4330】判断如果是对象数组,则不拼接
  86. let isObject = typeof (values[key][0] || '') === 'object';
  87. if (!isObject) {
  88. values[key] = values[key].join(',');
  89. }
  90. // update-end-author:sunjianlei date:20221205 for: 【issues/4330】判断如果是对象数组,则不拼接
  91. }
  92. });
  93. }
  94. return values;
  95. //update-end-author:taoyan date:2022-7-5 for: VUEN-1341【流程】编码方式 流程节点编辑表单时,填写数据报错 包括用户组件、部门组件、省市区
  96. },
  97. setFieldsValue: async <T>(values: T) => {
  98. const form = await getForm();
  99. form.setFieldsValue<T>(values);
  100. },
  101. appendSchemaByField: async (schema: FormSchema, prefixField: string | undefined, first: boolean) => {
  102. const form = await getForm();
  103. form.appendSchemaByField(schema, prefixField, first);
  104. },
  105. submit: async (): Promise<any> => {
  106. const form = await getForm();
  107. return form.submit();
  108. },
  109. /**
  110. * 表单验证并返回表单值
  111. * @update:添加表单值转换逻辑
  112. * @updateBy:zyf
  113. * @updateDate:2021-09-02
  114. */
  115. validate: async (nameList?: NamePath[]): Promise<Recordable> => {
  116. const form = await getForm();
  117. let getProps = props || form.getProps;
  118. let values = form.validate(nameList).then((values) => {
  119. for (let key in values) {
  120. if (values[key] instanceof Array) {
  121. let valueType = getValueType(getProps, key);
  122. if (valueType === 'string') {
  123. values[key] = values[key].join(',');
  124. }
  125. }
  126. }
  127. //--@updateBy-begin----author:liusq---date:20210916------for:处理区域事件字典信息------
  128. return handleRangeValue(getProps, values);
  129. //--@updateBy-end----author:liusq---date:20210916------for:处理区域事件字典信息------
  130. });
  131. return values;
  132. },
  133. validateFields: async (nameList?: NamePath[], options?: ValidateOptions): Promise<Recordable> => {
  134. const form = await getForm();
  135. return form.validateFields(nameList, options);
  136. },
  137. };
  138. return [register, methods];
  139. }