| | |
| | | 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>; |
| | |
| | | } |
| | | |
| | | /** |
| | | * @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 = /^\[(.+)\]$/; |
| | |
| | | } |
| | | |
| | | /** |
| | | * @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 = /^\{(.+)\}$/; |
| | |
| | | } |
| | | // 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)) { |
| | | // 没有解构成功的,按原样赋值 |
| | |
| | | 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); |
| | | } |