Skip to content

Commit

Permalink
fix: form setValues not support dayjs and Date value (#5064)
Browse files Browse the repository at this point in the history
* fix: setFormValues not support  `dayjs object` value

* fix: setFormValues not support `Date` value

* chore: remove console log
  • Loading branch information
mynetfan authored Dec 7, 2024
1 parent 68ab73b commit b75a8e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/@core/base/shared/src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 12 additions & 1 deletion packages/@core/ui-kit/form-ui/src/form-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Store } from '@vben-core/shared/store';
import {
bindMethods,
createMerge,
isDate,
isDayjsObject,
isFunction,
isObject,
mergeWithArrayOverride,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b75a8e6

Please sign in to comment.