Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve the error about setFieldValue array & pref the code #3775

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading