From 1b30834eb36b7e1730b01ce6af602444937d00ef Mon Sep 17 00:00:00 2001 From: oooplz <51434344+oooplz@users.noreply.github.com> Date: 星期三, 29 三月 2023 14:59:11 +0800 Subject: [PATCH] feat: 解构字段设置value (#2648) --- src/components/Form/src/hooks/useFormEvents.ts | 62 ++++++++++++++++++++++++++---- 1 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index c6b9b6e..ef60927 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -14,7 +14,7 @@ import { deepMerge } from '/@/utils'; import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; import { dateUtil } from '/@/utils/dateUtil'; -import { cloneDeep, uniqBy } from 'lodash-es'; +import { cloneDeep, set, uniqBy } from 'lodash-es'; import { error } from '/@/utils/log'; interface UseFormActionContext { @@ -27,6 +27,47 @@ schemaRef: Ref<FormSchema[]>; handleFormValues: Fn; } + +function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined { + const pattern = /^\[(.+)\]$/; + if (pattern.test(field)) { + const match = field.match(pattern); + if (match && match[1]) { + const keys = match[1].split(','); + if (!keys.length) { + return undefined; + } + + const result = []; + keys.forEach((k, index) => { + set(result, index, values[k.trim()]); + }); + + return result.length ? result : undefined; + } + } +} + +function tryConstructObject(field: string, values: Recordable = {}): Recordable | undefined { + const pattern = /^\{(.+)\}$/; + if (pattern.test(field)) { + const match = field.match(pattern); + if (match && match[1]) { + const keys = match[1].split(','); + if (!keys.length) { + return; + } + + const result = {}; + keys.forEach((k) => { + set(result, k.trim(), values[k.trim()]); + }); + + return Object.values(result).filter(Boolean).length ? result : undefined; + } + } +} + export function useFormEvents({ emit, getProps, @@ -69,7 +110,7 @@ const nestKeyArray = fields.filter((item) => String(item).indexOf(delimiter) >= 0); const validKeys: string[] = []; - Object.keys(values).forEach((key) => { + fields.forEach((key) => { const schema = unref(getSchema).find((item) => item.field === key); let value = values[key]; @@ -81,24 +122,28 @@ if (typeof componentProps === 'function') { _props = _props({ formModel: unref(formModel) }); } + + const constructValue = tryConstructArray(key, values) || tryConstructObject(key, values); + // 0| '' is allow - if (hasKey && fields.includes(key)) { + if (hasKey || !!constructValue) { + const fieldValue = constructValue || value; // time type if (itemIsDateType(key)) { - if (Array.isArray(value)) { + if (Array.isArray(fieldValue)) { const arr: any[] = []; - for (const ele of value) { + for (const ele of fieldValue) { arr.push(ele ? dateUtil(ele) : null); } unref(formModel)[key] = arr; } else { - unref(formModel)[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; + unref(formModel)[key] = fieldValue ? (_props?.valueFormat ? fieldValue : dateUtil(fieldValue)) : null; } } else { - unref(formModel)[key] = value; + unref(formModel)[key] = fieldValue; } if (_props?.onChange) { - _props?.onChange(value); + _props?.onChange(fieldValue); } validKeys.push(key); } else { @@ -335,4 +380,3 @@ setFieldsValue, scrollToField, }; -} -- Gitblit v1.8.0