diff --git a/src/components/Form/src/BasicForm.vue b/src/components/Form/src/BasicForm.vue index 209ebaf0fc7..4640ecce3ac 100644 --- a/src/components/Form/src/BasicForm.vue +++ b/src/components/Form/src/BasicForm.vue @@ -168,6 +168,7 @@ validateFields, getFieldsValue, updateSchema, + resetSchema, appendSchemaByField, removeSchemaByFiled, resetFields, @@ -230,6 +231,7 @@ setFieldsValue, resetFields, updateSchema, + resetSchema, setProps, removeSchemaByFiled, appendSchemaByField, diff --git a/src/components/Form/src/hooks/useForm.ts b/src/components/Form/src/hooks/useForm.ts index 5061f5efabb..c6badb656ac 100644 --- a/src/components/Form/src/hooks/useForm.ts +++ b/src/components/Form/src/hooks/useForm.ts @@ -65,6 +65,11 @@ export function useForm(props?: Props): UseFormReturnType { form.updateSchema(data); }, + resetSchema: async (data: Partial | Partial[]) => { + const form = await getForm(); + form.resetSchema(data); + }, + clearValidate: async (name?: string | string[]) => { const form = await getForm(); form.clearValidate(name); diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index 3240d75da48..ce34911b560 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -137,6 +137,26 @@ export function useFormEvents({ schemaRef.value = schemaList; } + async function resetSchema(data: Partial | Partial[]) { + let updateData: Partial[] = []; + if (isObject(data)) { + updateData.push(data as FormSchema); + } + if (isArray(data)) { + updateData = [...data]; + } + + const hasField = updateData.every((item) => Reflect.has(item, 'field') && item.field); + + if (!hasField) { + error( + 'All children of the form Schema array that need to be updated must contain the `field` field' + ); + return; + } + schemaRef.value = updateData as FormSchema[]; + } + async function updateSchema(data: Partial | Partial[]) { let updateData: Partial[] = []; if (isObject(data)) { @@ -227,6 +247,7 @@ export function useFormEvents({ validateFields, getFieldsValue, updateSchema, + resetSchema, appendSchemaByField, removeSchemaByFiled, resetFields, diff --git a/src/components/Form/src/types/form.ts b/src/components/Form/src/types/form.ts index b0440e35da7..6ea672c2624 100644 --- a/src/components/Form/src/types/form.ts +++ b/src/components/Form/src/types/form.ts @@ -31,6 +31,7 @@ export interface FormActionType { getFieldsValue: () => Recordable; clearValidate: (name?: string | string[]) => Promise; updateSchema: (data: Partial | Partial[]) => Promise; + resetSchema: (data: Partial | Partial[]) => Promise; setProps: (formProps: Partial) => Promise; removeSchemaByFiled: (field: string | string[]) => Promise; appendSchemaByField: (