diff --git a/src/components/Form/src/components/ApiTransfer.vue b/src/components/Form/src/components/ApiTransfer.vue index e163aaa7..b15b1fcf 100644 --- a/src/components/Form/src/components/ApiTransfer.vue +++ b/src/components/Form/src/components/ApiTransfer.vue @@ -56,8 +56,8 @@ const getdataSource = computed(() => { }, [] as TransferItem[]) }) const getTargetKeys = computed(() => { - if (unref(_targetKeys).length > 0) - return unref(_targetKeys) + // if (unref(_targetKeys).length > 0) + // return unref(_targetKeys) if (Array.isArray(props.value)) return props.value diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index 984e3e3a..8d743a89 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -346,7 +346,8 @@ export function useFormEvents({ } async function validateFields(nameList?: NamePath[] | undefined) { - return unref(formElRef)?.validateFields(nameList) + const values = unref(formElRef)?.validateFields(nameList) + return handleFormValues(values) } async function validate(nameList?: NamePath[] | false | undefined) { @@ -357,7 +358,8 @@ export function useFormEvents({ else _nameList = nameList === Array.isArray(nameList) ? nameList : undefined - return await unref(formElRef)?.validate(_nameList) + const values = await unref(formElRef)?.validate(_nameList) + return handleFormValues(values) } async function clearValidate(name?: string | string[]) { @@ -383,8 +385,7 @@ export function useFormEvents({ return try { const values = await validate() - const res = handleFormValues(values) - emit('submit', res) + emit('submit', values) } catch (error: any) { if (error?.outOfDate === false && error?.errorFields) diff --git a/src/components/Form/src/hooks/useFormValues.ts b/src/components/Form/src/hooks/useFormValues.ts index 7e54fa35..05101e0a 100644 --- a/src/components/Form/src/hooks/useFormValues.ts +++ b/src/components/Form/src/hooks/useFormValues.ts @@ -1,9 +1,9 @@ import { unref } from 'vue' import type { ComputedRef, Ref } from 'vue' -import { cloneDeep, set } from 'lodash-es' +import { cloneDeep, get, set, unset } from 'lodash-es' import type { FormProps, FormSchema } from '../types/form' import { dateUtil } from '@/utils/dateUtil' -import { isArray, isFunction, isNullOrUnDef, isObject, isString } from '@/utils/is' +import { isArray, isFunction, isNotEmpty, isNullOrUnDef, isObject, isString } from '@/utils/is' interface UseFormValuesContext { defaultValueRef: Ref @@ -98,18 +98,22 @@ export function useFormValues({ defaultValueRef, getSchema, formModel, getProps 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] = formatTime(startTime, startTimeFormat) - values[endTimeKey] = formatTime(endTime, endTimeFormat) - Reflect.deleteProperty(values, field) + if (isNotEmpty(startTime)) + set(values, startTimeKey, formatTime(startTime, startTimeFormat)) + + if (isNotEmpty(endTime)) + set(values, endTimeKey, formatTime(endTime, endTimeFormat)) + + unset(values, field) } return values diff --git a/src/utils/is.ts b/src/utils/is.ts index d345a53b..72fa3ab6 100644 --- a/src/utils/is.ts +++ b/src/utils/is.ts @@ -1,3 +1,5 @@ +import { isNil } from 'lodash-es' + // eslint-disable-next-line @typescript-eslint/unbound-method const toString = Object.prototype.toString @@ -17,7 +19,14 @@ export function isObject(val: any): val is Record { return val !== null && is(val, 'Object') } +export function isNotEmpty(val: any): boolean { + return !isNil(val) && !isEmpty(val) +} + export function isEmpty(val: T): val is T { + if (isNil(val)) + return true + if (isArray(val) || isString(val)) return val.length === 0