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 logpull/58/MERGE
parent
68ab73bdb5
commit
b75a8e6a2b
|
@ -16,3 +16,11 @@ export function formatDate(time: number | string, format = 'YYYY-MM-DD') {
|
||||||
export function formatDateTime(time: number | string) {
|
export function formatDateTime(time: number | string) {
|
||||||
return formatDate(time, 'YYYY-MM-DD HH:mm:ss');
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,8 @@ import { Store } from '@vben-core/shared/store';
|
||||||
import {
|
import {
|
||||||
bindMethods,
|
bindMethods,
|
||||||
createMerge,
|
createMerge,
|
||||||
|
isDate,
|
||||||
|
isDayjsObject,
|
||||||
isFunction,
|
isFunction,
|
||||||
isObject,
|
isObject,
|
||||||
mergeWithArrayOverride,
|
mergeWithArrayOverride,
|
||||||
|
@ -252,10 +254,19 @@ export class FormApi {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并算法有待改进,目前的算法不支持object类型的值。
|
||||||
|
* antd的日期时间相关组件的值类型为dayjs对象
|
||||||
|
* element-plus的日期时间相关组件的值类型可能为Date对象
|
||||||
|
* 以上两种类型需要排除深度合并
|
||||||
|
*/
|
||||||
const fieldMergeFn = createMerge((obj, key, value) => {
|
const fieldMergeFn = createMerge((obj, key, value) => {
|
||||||
if (key in obj) {
|
if (key in obj) {
|
||||||
obj[key] =
|
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)
|
? fieldMergeFn(obj[key], value)
|
||||||
: value;
|
: value;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue