vben
2021-08-24 56a966cfbf8db5b29a42185f0f25a0e800c30dbb
src/components/Form/src/hooks/useForm.ts
@@ -1,21 +1,16 @@
import { ref, onUnmounted, unref, nextTick, watchEffect } from 'vue';
import { isInSetup } from '/@/utils/helper/vueHelper';
import type { FormProps, FormActionType, UseFormReturnType, FormSchema } from '../types/form';
import type { NamePath } from 'ant-design-vue/lib/form/interface';
import type { DynamicProps } from '/#/utils';
import { ref, onUnmounted, unref, nextTick, watch } from 'vue';
import { isProdMode } from '/@/utils/env';
import { error } from '/@/utils/log';
import { getDynamicProps } from '/@/utils';
import type { FormProps, FormActionType, UseFormReturnType, FormSchema } from '../types/form';
import type { NamePath } from 'ant-design-vue/lib/form/interface';
import type { DynamicProps } from '/@/types/utils';
export declare type ValidateFields = (nameList?: NamePath[]) => Promise<Recordable>;
type Props = Partial<DynamicProps<FormProps>>;
export function useForm(props?: Props): UseFormReturnType {
  isInSetup();
  const formRef = ref<Nullable<FormActionType>>(null);
  const loadedRef = ref<Nullable<boolean>>(false);
@@ -23,7 +18,7 @@
    const form = unref(formRef);
    if (!form) {
      error(
        'The form instance has not been obtained, please make sure that the form has been rendered when performing the form operation!'
        'The form instance has not been obtained, please make sure that the form has been rendered when performing the form operation!',
      );
    }
    await nextTick();
@@ -39,12 +34,18 @@
    if (unref(loadedRef) && isProdMode() && instance === unref(formRef)) return;
    formRef.value = instance;
    loadedRef.value = true;
    watchEffect(() => {
      props && instance.setProps(getDynamicProps(props));
    });
    watch(
      () => props,
      () => {
        props && instance.setProps(getDynamicProps(props));
      },
      {
        immediate: true,
        deep: true,
      },
    );
  }
  const methods: FormActionType = {
@@ -62,6 +63,11 @@
      form.updateSchema(data);
    },
    resetSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
      const form = await getForm();
      form.resetSchema(data);
    },
    clearValidate: async (name?: string | string[]) => {
      const form = await getForm();
      form.clearValidate(name);
@@ -74,8 +80,7 @@
    },
    removeSchemaByFiled: async (field: string | string[]) => {
      const form = await getForm();
      form.removeSchemaByFiled(field);
      unref(formRef)?.removeSchemaByFiled(field);
    },
    // TODO promisify
@@ -88,9 +93,13 @@
      form.setFieldsValue<T>(values);
    },
    appendSchemaByField: async (schema: FormSchema, prefixField?: string | undefined) => {
    appendSchemaByField: async (
      schema: FormSchema,
      prefixField: string | undefined,
      first: boolean,
    ) => {
      const form = await getForm();
      form.appendSchemaByField(schema, prefixField);
      form.appendSchemaByField(schema, prefixField, first);
    },
    submit: async (): Promise<any> => {