Commit c639e493a5a32789e397990953189541170169c8
1 parent
7e2668f6
feat(form): adding resetSchema method
Showing
4 changed files
with
29 additions
and
0 deletions
src/components/Form/src/BasicForm.vue
@@ -168,6 +168,7 @@ | @@ -168,6 +168,7 @@ | ||
168 | validateFields, | 168 | validateFields, |
169 | getFieldsValue, | 169 | getFieldsValue, |
170 | updateSchema, | 170 | updateSchema, |
171 | + resetSchema, | ||
171 | appendSchemaByField, | 172 | appendSchemaByField, |
172 | removeSchemaByFiled, | 173 | removeSchemaByFiled, |
173 | resetFields, | 174 | resetFields, |
@@ -230,6 +231,7 @@ | @@ -230,6 +231,7 @@ | ||
230 | setFieldsValue, | 231 | setFieldsValue, |
231 | resetFields, | 232 | resetFields, |
232 | updateSchema, | 233 | updateSchema, |
234 | + resetSchema, | ||
233 | setProps, | 235 | setProps, |
234 | removeSchemaByFiled, | 236 | removeSchemaByFiled, |
235 | appendSchemaByField, | 237 | appendSchemaByField, |
src/components/Form/src/hooks/useForm.ts
@@ -65,6 +65,11 @@ export function useForm(props?: Props): UseFormReturnType { | @@ -65,6 +65,11 @@ export function useForm(props?: Props): UseFormReturnType { | ||
65 | form.updateSchema(data); | 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 | clearValidate: async (name?: string | string[]) => { | 73 | clearValidate: async (name?: string | string[]) => { |
69 | const form = await getForm(); | 74 | const form = await getForm(); |
70 | form.clearValidate(name); | 75 | form.clearValidate(name); |
src/components/Form/src/hooks/useFormEvents.ts
@@ -137,6 +137,26 @@ export function useFormEvents({ | @@ -137,6 +137,26 @@ export function useFormEvents({ | ||
137 | schemaRef.value = schemaList; | 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 | async function updateSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) { | 160 | async function updateSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) { |
141 | let updateData: Partial<FormSchema>[] = []; | 161 | let updateData: Partial<FormSchema>[] = []; |
142 | if (isObject(data)) { | 162 | if (isObject(data)) { |
@@ -227,6 +247,7 @@ export function useFormEvents({ | @@ -227,6 +247,7 @@ export function useFormEvents({ | ||
227 | validateFields, | 247 | validateFields, |
228 | getFieldsValue, | 248 | getFieldsValue, |
229 | updateSchema, | 249 | updateSchema, |
250 | + resetSchema, | ||
230 | appendSchemaByField, | 251 | appendSchemaByField, |
231 | removeSchemaByFiled, | 252 | removeSchemaByFiled, |
232 | resetFields, | 253 | resetFields, |
src/components/Form/src/types/form.ts
@@ -31,6 +31,7 @@ export interface FormActionType { | @@ -31,6 +31,7 @@ export interface FormActionType { | ||
31 | getFieldsValue: () => Recordable; | 31 | getFieldsValue: () => Recordable; |
32 | clearValidate: (name?: string | string[]) => Promise<void>; | 32 | clearValidate: (name?: string | string[]) => Promise<void>; |
33 | updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>; | 33 | updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>; |
34 | + resetSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>; | ||
34 | setProps: (formProps: Partial<FormProps>) => Promise<void>; | 35 | setProps: (formProps: Partial<FormProps>) => Promise<void>; |
35 | removeSchemaByFiled: (field: string | string[]) => Promise<void>; | 36 | removeSchemaByFiled: (field: string | string[]) => Promise<void>; |
36 | appendSchemaByField: ( | 37 | appendSchemaByField: ( |