From 1384174f03c9a009cfbb3ae99aaeec21f177e4c2 Mon Sep 17 00:00:00 2001 From: yyg1378265336 <1378265336@qq.com> Date: 星期四, 27 二月 2025 09:26:30 +0800 Subject: [PATCH] 生产工具信息 --- src/components/Form/src/hooks/useFormValues.ts | 63 +++++++++++++++++++++---------- 1 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/components/Form/src/hooks/useFormValues.ts b/src/components/Form/src/hooks/useFormValues.ts index 4d3bac9..0329f5c 100644 --- a/src/components/Form/src/hooks/useFormValues.ts +++ b/src/components/Form/src/hooks/useFormValues.ts @@ -1,9 +1,9 @@ -import { isArray, isFunction, isObject, isString, isNullOrUnDef } from '/@/utils/is'; -import { dateUtil } from '/@/utils/dateUtil'; +import { isArray, isFunction, isEmpty, isObject, isString, isNil } from '@/utils/is'; +import { dateUtil } from '@/utils/dateUtil'; import { unref } from 'vue'; import type { Ref, ComputedRef } from 'vue'; -import type { FormProps, FormSchema } from '../types/form'; -import { cloneDeep, set } from 'lodash-es'; +import type { FormProps, FormSchemaInner as FormSchema } from '../types/form'; +import { cloneDeep, get, set, unset } from 'lodash-es'; interface UseFormValuesContext { defaultValueRef: Ref<any>; @@ -13,7 +13,7 @@ } /** - * @desription deconstruct array-link key. This method will mutate the target. + * @description deconstruct array-link key. This method will mutate the target. */ function tryDeconstructArray(key: string, value: any, target: Recordable) { const pattern = /^\[(.+)\]$/; @@ -31,7 +31,7 @@ } /** - * @desription deconstruct object-link key. This method will mutate the target. + * @description deconstruct object-link key. This method will mutate the target. */ function tryDeconstructObject(key: string, value: any, target: Recordable) { const pattern = /^\{(.+)\}$/; @@ -76,12 +76,7 @@ } // Remove spaces if (isString(value)) { - // remove params from URL - if (value === '') { - value = undefined; - } else { - value = value.trim(); - } + value = value.trim(); } if (!tryDeconstructArray(key, value, res) && !tryDeconstructObject(key, value, res)) { // 娌℃湁瑙f瀯鎴愬姛鐨勶紝鎸夊師鏍疯祴鍊� @@ -106,35 +101,63 @@ continue; } // If the value to be converted is empty, remove the field - if (!values[field]) { - Reflect.deleteProperty(values, field); + if (!get(values, field)) { + unset(values, field); continue; } - const [startTime, endTime]: string[] = values[field]; + const [startTime, endTime]: string[] = get(values, field); const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format]; - values[startTimeKey] = dateUtil(startTime).format(startTimeFormat); - values[endTimeKey] = dateUtil(endTime).format(endTimeFormat); - Reflect.deleteProperty(values, field); + if (!isNil(startTime) && !isEmpty(startTime)) { + set(values, startTimeKey, formatTime(startTime, startTimeFormat)); + } + if (!isNil(endTime) && !isEmpty(endTime)) { + set(values, endTimeKey, formatTime(endTime, endTimeFormat)); + } + unset(values, field); } return values; + } + + function formatTime(time: string, format: string) { + if (format === 'timestamp') { + return dateUtil(time).unix(); + } else if (format === 'timestampStartDay') { + return dateUtil(time).startOf('day').unix(); + } + return dateUtil(time).format(format); } function initDefault() { const schemas = unref(getSchema); const obj: Recordable = {}; schemas.forEach((item) => { - const { defaultValue } = item; - if (!isNullOrUnDef(defaultValue)) { + const { defaultValue, defaultValueObj, componentProps = {} } = item; + const fieldKeys = Object.keys(defaultValueObj || {}); + if (fieldKeys.length) { + fieldKeys.forEach((field) => { + obj[field] = defaultValueObj![field]; + if (formModel[field] === undefined) { + formModel[field] = defaultValueObj![field]; + } + }); + } + if (!isNil(defaultValue)) { obj[item.field] = defaultValue; if (formModel[item.field] === undefined) { formModel[item.field] = defaultValue; } } + if (!isNil(componentProps?.defaultValue)) { + obj[item.field] = componentProps?.defaultValue; + if (formModel[item.field] === undefined) { + formModel[item.field] = componentProps?.defaultValue; + } + } }); defaultValueRef.value = cloneDeep(obj); } -- Gitblit v1.9.3