Commit c639e493a5a32789e397990953189541170169c8

Authored by zuihou
1 parent 7e2668f6

feat(form): adding resetSchema method

src/components/Form/src/BasicForm.vue
... ... @@ -168,6 +168,7 @@
168 168 validateFields,
169 169 getFieldsValue,
170 170 updateSchema,
  171 + resetSchema,
171 172 appendSchemaByField,
172 173 removeSchemaByFiled,
173 174 resetFields,
... ... @@ -230,6 +231,7 @@
230 231 setFieldsValue,
231 232 resetFields,
232 233 updateSchema,
  234 + resetSchema,
233 235 setProps,
234 236 removeSchemaByFiled,
235 237 appendSchemaByField,
... ...
src/components/Form/src/hooks/useForm.ts
... ... @@ -65,6 +65,11 @@ export function useForm(props?: Props): UseFormReturnType {
65 65 form.updateSchema(data);
66 66 },
67 67  
  68 + resetSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
  69 + const form = await getForm();
  70 + form.resetSchema(data);
  71 + },
  72 +
68 73 clearValidate: async (name?: string | string[]) => {
69 74 const form = await getForm();
70 75 form.clearValidate(name);
... ...
src/components/Form/src/hooks/useFormEvents.ts
... ... @@ -137,6 +137,26 @@ export function useFormEvents({
137 137 schemaRef.value = schemaList;
138 138 }
139 139  
  140 + async function resetSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) {
  141 + let updateData: Partial<FormSchema>[] = [];
  142 + if (isObject(data)) {
  143 + updateData.push(data as FormSchema);
  144 + }
  145 + if (isArray(data)) {
  146 + updateData = [...data];
  147 + }
  148 +
  149 + const hasField = updateData.every((item) => Reflect.has(item, 'field') && item.field);
  150 +
  151 + if (!hasField) {
  152 + error(
  153 + 'All children of the form Schema array that need to be updated must contain the `field` field'
  154 + );
  155 + return;
  156 + }
  157 + schemaRef.value = updateData as FormSchema[];
  158 + }
  159 +
140 160 async function updateSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) {
141 161 let updateData: Partial<FormSchema>[] = [];
142 162 if (isObject(data)) {
... ... @@ -227,6 +247,7 @@ export function useFormEvents({
227 247 validateFields,
228 248 getFieldsValue,
229 249 updateSchema,
  250 + resetSchema,
230 251 appendSchemaByField,
231 252 removeSchemaByFiled,
232 253 resetFields,
... ...
src/components/Form/src/types/form.ts
... ... @@ -31,6 +31,7 @@ export interface FormActionType {
31 31 getFieldsValue: () => Recordable;
32 32 clearValidate: (name?: string | string[]) => Promise<void>;
33 33 updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
  34 + resetSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
34 35 setProps: (formProps: Partial<FormProps>) => Promise<void>;
35 36 removeSchemaByFiled: (field: string | string[]) => Promise<void>;
36 37 appendSchemaByField: (
... ...