Skip to content

Commit

Permalink
fix(BasicForm): solve the error about setFieldValue array (#3775)
Browse files Browse the repository at this point in the history
* fix(util): resolve notable to setFieldvalue array in the form

* perf(util): simpify the form event
  • Loading branch information
electroluxcode authored Apr 23, 2024
1 parent 4e8ac0e commit f5cd3ad
Showing 1 changed file with 7 additions and 61 deletions.
68 changes: 7 additions & 61 deletions src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isIncludeSimpleComponents,
} from '../helper';
import { dateUtil } from '@/utils/dateUtil';
import { cloneDeep, set, uniqBy, get } from 'lodash-es';
import { cloneDeep, has, uniqBy, get } from 'lodash-es';
import { error } from '@/utils/log';

interface UseFormActionContext {
Expand All @@ -25,46 +25,6 @@ interface UseFormActionContext {
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.filter(Boolean).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,
Expand Down Expand Up @@ -114,15 +74,11 @@ export function useFormEvents({

const fields = getAllFields();

// key 支持 a.b.c 的嵌套写法
const delimiter = '.';
const nestKeyArray = fields.filter((item) => String(item).indexOf(delimiter) >= 0);

const validKeys: string[] = [];
fields.forEach((key) => {
const schema = unref(getSchema).find((item) => item.field === key);
let value = get(values, key);
const hasKey = Reflect.has(values, key);
const hasKey = has(values, key);

value = handleInputNumberValue(schema?.component, value);
const { componentProps } = schema || {};
Expand All @@ -134,7 +90,7 @@ export function useFormEvents({
});
}

const constructValue = tryConstructArray(key, values) || tryConstructObject(key, values);
const constructValue = get(value, key);
const setDateFieldValue = (v) => {
return v ? (_props?.valueFormat ? v : dateUtil(v)) : null;
};
Expand All @@ -161,20 +117,10 @@ export function useFormEvents({
}
validKeys.push(key);
} else {
nestKeyArray.forEach((nestKey: string) => {
try {
const value = get(values, nestKey);
if (isDef(value)) {
unref(formModel)[nestKey] = unref(value);
validKeys.push(nestKey);
}
} catch (e) {
// key not exist
if (isDef(defaultValueRef.value[nestKey])) {
unref(formModel)[nestKey] = cloneDeep(unref(defaultValueRef.value[nestKey]));
}
}
});
// key not exist
if (isDef(get(defaultValueRef.value, key))) {
unref(formModel)[key] = cloneDeep(unref(get(defaultValueRef.value, key)));
}
}
});
validateFields(validKeys).catch((_) => {});
Expand Down

0 comments on commit f5cd3ad

Please sign in to comment.