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