diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index ac10f32a..86946b44 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue'; import type { FormProps, FormSchema, FormActionType } from '../types/form'; import type { NamePath } from 'ant-design-vue/lib/form/interface'; import { unref, toRaw, nextTick } from 'vue'; -import { isArray, isFunction, isNullOrUnDef, isObject, isString } from '/@/utils/is'; +import { isArray, isFunction, isObject, isString, isDef } from '/@/utils/is'; import { deepMerge } from '/@/utils'; import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; import { dateUtil } from '/@/utils/dateUtil'; @@ -55,6 +55,10 @@ export function useFormEvents({ .map((item) => item.field) .filter(Boolean); + // key 支持 a.b.c 的嵌套写法 + const delimiter = '.'; + const nestKeyArray = fields.filter((item) => item.indexOf(delimiter) >= 0); + const validKeys: string[] = []; Object.keys(values).forEach((key) => { const schema = unref(getSchema).find((item) => item.field === key); @@ -85,6 +89,21 @@ export function useFormEvents({ formModel[key] = value; } validKeys.push(key); + } else { + nestKeyArray.forEach((nestKey: string) => { + try { + const value = eval('values' + delimiter + nestKey); + if (isDef(value)) { + formModel[nestKey] = value; + validKeys.push(nestKey); + } + } catch (e) { + // key not exist + if (isDef(defaultValueRef.value[nestKey])) { + formModel[nestKey] = defaultValueRef.value[nestKey]; + } + } + }); } }); validateFields(validKeys).catch((_) => {});