Browse Source

feat: 解构字段设置value

main
xingyuv 2 years ago
parent
commit
adbbff5ea9
  1. 61
      src/components/Form/src/hooks/useFormEvents.ts

61
src/components/Form/src/hooks/useFormEvents.ts

@ -6,7 +6,7 @@ import { isArray, isFunction, isObject, isString, isDef, isNullOrUnDef, isEmpty
import { deepMerge } from '@/utils'
import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'
import { dateUtil } from '@/utils/dateUtil'
import { cloneDeep, uniqBy } from 'lodash-es'
import { cloneDeep, set, uniqBy } from 'lodash-es'
import { error } from '@/utils/log'
interface UseFormActionContext {
@ -19,6 +19,47 @@ interface UseFormActionContext {
schemaRef: Ref<FormSchema[]>
handleFormValues: Fn
}
function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined {
const pattern = /^\[(.+)\]$/
if (pattern.test(field)) {
const match = field.match(pattern)
if (match && match[1]) {
const keys = match[1].split(',')
if (!keys.length) {
return undefined
}
const result = []
keys.forEach((k, index) => {
set(result, index, values[k.trim()])
})
return result.length ? result : undefined
}
}
}
function tryConstructObject(field: string, values: Recordable = {}): Recordable | undefined {
const pattern = /^\{(.+)\}$/
if (pattern.test(field)) {
const match = field.match(pattern)
if (match && match[1]) {
const keys = match[1].split(',')
if (!keys.length) {
return
}
const result = {}
keys.forEach((k) => {
set(result, k.trim(), values[k.trim()])
})
return Object.values(result).filter(Boolean).length ? result : undefined
}
}
}
export function useFormEvents({
emit,
getProps,
@ -61,7 +102,7 @@ export function useFormEvents({
const nestKeyArray = fields.filter((item) => String(item).indexOf(delimiter) >= 0)
const validKeys: string[] = []
Object.keys(values).forEach((key) => {
fields.forEach((key) => {
const schema = unref(getSchema).find((item) => item.field === key)
let value = values[key]
@ -73,24 +114,28 @@ export function useFormEvents({
if (typeof componentProps === 'function') {
_props = _props({ formModel: unref(formModel) })
}
const constructValue = tryConstructArray(key, values) || tryConstructObject(key, values)
// 0| '' is allow
if (hasKey && fields.includes(key)) {
if (hasKey || !!constructValue) {
const fieldValue = constructValue || value
// time type
if (itemIsDateType(key)) {
if (Array.isArray(value)) {
if (Array.isArray(fieldValue)) {
const arr: any[] = []
for (const ele of value) {
for (const ele of fieldValue) {
arr.push(ele ? dateUtil(ele) : null)
}
unref(formModel)[key] = arr
} else {
unref(formModel)[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null
unref(formModel)[key] = fieldValue ? (_props?.valueFormat ? fieldValue : dateUtil(fieldValue)) : null
}
} else {
unref(formModel)[key] = value
unref(formModel)[key] = fieldValue
}
if (_props?.onChange) {
_props?.onChange(value)
_props?.onChange(fieldValue)
}
validKeys.push(key)
} else {