Commit aaa30fbf10611de9a9c181d9262f3bcde3d7fc34
Committed by
GitHub
1 parent
9092c34c
feat(form): appendSchemaByField和updateSchema支持defaultValue (#1608)
1. 兼容appendSchemaByField和updateSchema支持设置默认值
Showing
1 changed file
with
29 additions
and
1 deletions
src/components/Form/src/hooks/useFormEvents.ts
@@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue'; | @@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue'; | ||
2 | import type { FormProps, FormSchema, FormActionType } from '../types/form'; | 2 | import type { FormProps, FormSchema, FormActionType } from '../types/form'; |
3 | import type { NamePath } from 'ant-design-vue/lib/form/interface'; | 3 | import type { NamePath } from 'ant-design-vue/lib/form/interface'; |
4 | import { unref, toRaw, nextTick } from 'vue'; | 4 | import { unref, toRaw, nextTick } from 'vue'; |
5 | -import { isArray, isFunction, isObject, isString } from '/@/utils/is'; | 5 | +import { isArray, isFunction, isNullOrUnDef, isObject, isString } from '/@/utils/is'; |
6 | import { deepMerge } from '/@/utils'; | 6 | import { deepMerge } from '/@/utils'; |
7 | import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; | 7 | import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; |
8 | import { dateUtil } from '/@/utils/dateUtil'; | 8 | import { dateUtil } from '/@/utils/dateUtil'; |
@@ -132,11 +132,14 @@ export function useFormEvents({ | @@ -132,11 +132,14 @@ export function useFormEvents({ | ||
132 | if (!prefixField || index === -1 || first) { | 132 | if (!prefixField || index === -1 || first) { |
133 | first ? schemaList.unshift(schema) : schemaList.push(schema); | 133 | first ? schemaList.unshift(schema) : schemaList.push(schema); |
134 | schemaRef.value = schemaList; | 134 | schemaRef.value = schemaList; |
135 | + _setDefaultValue(schema); | ||
135 | return; | 136 | return; |
136 | } | 137 | } |
137 | if (index !== -1) { | 138 | if (index !== -1) { |
138 | schemaList.splice(index + 1, 0, schema); | 139 | schemaList.splice(index + 1, 0, schema); |
139 | } | 140 | } |
141 | + _setDefaultValue(schema); | ||
142 | + | ||
140 | schemaRef.value = schemaList; | 143 | schemaRef.value = schemaList; |
141 | } | 144 | } |
142 | 145 | ||
@@ -192,9 +195,34 @@ export function useFormEvents({ | @@ -192,9 +195,34 @@ export function useFormEvents({ | ||
192 | } | 195 | } |
193 | }); | 196 | }); |
194 | }); | 197 | }); |
198 | + _setDefaultValue(schema); | ||
199 | + | ||
195 | schemaRef.value = uniqBy(schema, 'field'); | 200 | schemaRef.value = uniqBy(schema, 'field'); |
196 | } | 201 | } |
197 | 202 | ||
203 | + function _setDefaultValue(data: FormSchema | FormSchema[]) { | ||
204 | + let schemas: FormSchema[] = []; | ||
205 | + if (isObject(data)) { | ||
206 | + schemas.push(data as FormSchema); | ||
207 | + } | ||
208 | + if (isArray(data)) { | ||
209 | + schemas = [...data]; | ||
210 | + } | ||
211 | + | ||
212 | + const obj: Recordable = {}; | ||
213 | + schemas.forEach((item) => { | ||
214 | + if ( | ||
215 | + item.component != 'Divider' && | ||
216 | + Reflect.has(item, 'field') && | ||
217 | + item.field && | ||
218 | + !isNullOrUnDef(item.defaultValue) | ||
219 | + ) { | ||
220 | + obj[item.field] = item.defaultValue; | ||
221 | + } | ||
222 | + }); | ||
223 | + setFieldsValue(obj); | ||
224 | + } | ||
225 | + | ||
198 | function getFieldsValue(): Recordable { | 226 | function getFieldsValue(): Recordable { |
199 | const formEl = unref(formElRef); | 227 | const formEl = unref(formElRef); |
200 | if (!formEl) return {}; | 228 | if (!formEl) return {}; |