From b75a8e6a2ba10da4837ff1fffb899b2c7fcc06f6 Mon Sep 17 00:00:00 2001 From: Netfan Date: Sat, 7 Dec 2024 11:09:55 +0800 Subject: [PATCH] fix: form setValues not support `dayjs` and `Date` value (#5064) * fix: setFormValues not support `dayjs object` value * fix: setFormValues not support `Date` value * chore: remove console log --- packages/@core/base/shared/src/utils/date.ts | 8 ++++++++ packages/@core/ui-kit/form-ui/src/form-api.ts | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/@core/base/shared/src/utils/date.ts b/packages/@core/base/shared/src/utils/date.ts index 522e99465f1..3736b9ad591 100644 --- a/packages/@core/base/shared/src/utils/date.ts +++ b/packages/@core/base/shared/src/utils/date.ts @@ -16,3 +16,11 @@ export function formatDate(time: number | string, format = 'YYYY-MM-DD') { export function formatDateTime(time: number | string) { return formatDate(time, 'YYYY-MM-DD HH:mm:ss'); } + +export function isDate(value: any): value is Date { + return value instanceof Date; +} + +export function isDayjsObject(value: any): value is dayjs.Dayjs { + return dayjs.isDayjs(value); +} diff --git a/packages/@core/ui-kit/form-ui/src/form-api.ts b/packages/@core/ui-kit/form-ui/src/form-api.ts index 4c96f3d8719..585afaff2fc 100644 --- a/packages/@core/ui-kit/form-ui/src/form-api.ts +++ b/packages/@core/ui-kit/form-ui/src/form-api.ts @@ -14,6 +14,8 @@ import { Store } from '@vben-core/shared/store'; import { bindMethods, createMerge, + isDate, + isDayjsObject, isFunction, isObject, mergeWithArrayOverride, @@ -252,10 +254,19 @@ export class FormApi { return; } + /** + * 合并算法有待改进,目前的算法不支持object类型的值。 + * antd的日期时间相关组件的值类型为dayjs对象 + * element-plus的日期时间相关组件的值类型可能为Date对象 + * 以上两种类型需要排除深度合并 + */ const fieldMergeFn = createMerge((obj, key, value) => { if (key in obj) { obj[key] = - !Array.isArray(obj[key]) && isObject(obj[key]) + !Array.isArray(obj[key]) && + isObject(obj[key]) && + !isDayjsObject(obj[key]) && + !isDate(obj[key]) ? fieldMergeFn(obj[key], value) : value; }