From c639e493a5a32789e397990953189541170169c8 Mon Sep 17 00:00:00 2001 From: zuihou <244387066@qq.com> Date: Thu, 22 Apr 2021 22:51:00 +0800 Subject: [PATCH] feat(form): adding resetSchema method --- src/components/Form/src/BasicForm.vue | 2 ++ src/components/Form/src/hooks/useForm.ts | 5 +++++ .../Form/src/hooks/useFormEvents.ts | 21 +++++++++++++++++++ src/components/Form/src/types/form.ts | 1 + 4 files changed, 29 insertions(+) diff --git a/src/components/Form/src/BasicForm.vue b/src/components/Form/src/BasicForm.vue index 209ebaf0..4640ecce 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 5061f5ef..c6badb65 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 3240d75d..ce34911b 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 b0440e35..6ea672c2 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: (