ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import type { DynamicProps } from '@jnpf/utils';
import type { NamePath } from 'ant-design-vue/lib/form/interface';
 
import type { FormActionType, FormProps, FormSchema, UseFormReturnType } from '../types/form';
 
import { nextTick, onUnmounted, ref, unref, watch } from 'vue';
 
import { getDynamicProps } from '@jnpf/utils';
 
import { useAppConfig } from '@vben/hooks';
 
export declare type ValidateFields = (nameList?: NamePath[]) => Promise<Recordable>;
 
type Props = Partial<DynamicProps<FormProps>>;
 
const { isProdMode } = useAppConfig(import.meta.env, import.meta.env.PROD);
 
export function useForm(props?: Props): UseFormReturnType {
  const formRef = ref<Nullable<FormActionType>>(null);
  const loadedRef = ref<Nullable<boolean>>(false);
 
  async function getForm() {
    const form = unref(formRef);
    if (!form) {
      throw new Error('The form instance has not been obtained, please make sure that the form has been rendered when performing the form operation!');
    }
    await nextTick();
    return form as FormActionType;
  }
 
  function register(instance: FormActionType) {
    isProdMode() &&
      onUnmounted(() => {
        formRef.value = null;
        loadedRef.value = null;
      });
    if (unref(loadedRef) && isProdMode() && instance === unref(formRef)) return;
 
    formRef.value = instance;
    loadedRef.value = true;
 
    watch(
      () => props,
      () => {
        props && instance.setProps(getDynamicProps(props));
      },
      {
        deep: true,
        immediate: true,
      },
    );
  }
 
  const methods: FormActionType = {
    appendSchemaByField: async (schema: FormSchema | FormSchema[], prefixField: string | undefined, first?: boolean) => {
      const form = await getForm();
      form.appendSchemaByField(schema, prefixField, first);
    },
    clearValidate: async (name?: string | string[]) => {
      const form = await getForm();
      form.clearValidate(name);
    },
 
    // TODO promisify
    getFieldsValue: <T>() => {
      return unref(formRef)?.getFieldsValue() as T;
    },
 
    removeSchemaByField: async (field: string | string[]) => {
      unref(formRef)?.removeSchemaByField(field);
    },
 
    resetFields: async () => {
      getForm().then(async (form) => {
        await form.resetFields();
      });
    },
 
    resetSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
      const form = await getForm();
      form.resetSchema(data);
    },
 
    scrollToField: async (name: NamePath, options?: ScrollOptions | undefined) => {
      const form = await getForm();
      form.scrollToField(name, options);
    },
 
    setFieldsValue: async <T extends Recordable<any>>(values: T) => {
      const form = await getForm();
      form.setFieldsValue<T>(values);
    },
 
    setProps: async (formProps: Partial<FormProps>) => {
      const form = await getForm();
      form.setProps(formProps);
    },
 
    submit: async (): Promise<any> => {
      const form = await getForm();
      return form.submit();
    },
 
    updateSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
      const form = await getForm();
      form.updateSchema(data);
    },
 
    validate: async (nameList?: NamePath[]): Promise<Recordable> => {
      const form = await getForm();
      return form.validate(nameList);
    },
 
    validateFields: async (nameList?: NamePath[]): Promise<Recordable> => {
      const form = await getForm();
      return form.validateFields(nameList);
    },
  };
 
  return [register, methods];
}