Commit d09e998ae78d2fbe3d0537c6fca932a2ac484629
Committed by
GitHub
1 parent
e0976000
表单field支持a.b.c的写法 (#1549)
* chore: table size放到settings * chore(TableAction): 操作确认框增加placement属性支持 * chore(Form): 表单field支持a.b.c嵌套写法 Co-authored-by: jinmao88 <50581550+jinmao88@users.noreply.github.com>
Showing
1 changed file
with
20 additions
and
1 deletions
src/components/Form/src/hooks/useFormEvents.ts
... | ... | @@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue'; |
2 | 2 | import type { FormProps, FormSchema, FormActionType } from '../types/form'; |
3 | 3 | import type { NamePath } from 'ant-design-vue/lib/form/interface'; |
4 | 4 | import { unref, toRaw, nextTick } from 'vue'; |
5 | -import { isArray, isFunction, isNullOrUnDef, isObject, isString } from '/@/utils/is'; | |
5 | +import { isArray, isFunction, isObject, isString, isDef } from '/@/utils/is'; | |
6 | 6 | import { deepMerge } from '/@/utils'; |
7 | 7 | import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; |
8 | 8 | import { dateUtil } from '/@/utils/dateUtil'; |
... | ... | @@ -55,6 +55,10 @@ export function useFormEvents({ |
55 | 55 | .map((item) => item.field) |
56 | 56 | .filter(Boolean); |
57 | 57 | |
58 | + // key 支持 a.b.c 的嵌套写法 | |
59 | + const delimiter = '.'; | |
60 | + const nestKeyArray = fields.filter((item) => item.indexOf(delimiter) >= 0); | |
61 | + | |
58 | 62 | const validKeys: string[] = []; |
59 | 63 | Object.keys(values).forEach((key) => { |
60 | 64 | const schema = unref(getSchema).find((item) => item.field === key); |
... | ... | @@ -85,6 +89,21 @@ export function useFormEvents({ |
85 | 89 | formModel[key] = value; |
86 | 90 | } |
87 | 91 | validKeys.push(key); |
92 | + } else { | |
93 | + nestKeyArray.forEach((nestKey: string) => { | |
94 | + try { | |
95 | + const value = eval('values' + delimiter + nestKey); | |
96 | + if (isDef(value)) { | |
97 | + formModel[nestKey] = value; | |
98 | + validKeys.push(nestKey); | |
99 | + } | |
100 | + } catch (e) { | |
101 | + // key not exist | |
102 | + if (isDef(defaultValueRef.value[nestKey])) { | |
103 | + formModel[nestKey] = defaultValueRef.value[nestKey]; | |
104 | + } | |
105 | + } | |
106 | + }); | |
88 | 107 | } |
89 | 108 | }); |
90 | 109 | validateFields(validKeys).catch((_) => {}); | ... | ... |