未验证 提交 1b30834e 编写于 作者: O oooplz 提交者: GitHub

feat: 解构字段设置value (#2648)

上级 c5f97b9d
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
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 {
...@@ -27,6 +27,47 @@ interface UseFormActionContext { ...@@ -27,6 +27,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,
...@@ -69,7 +110,7 @@ export function useFormEvents({ ...@@ -69,7 +110,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];
...@@ -81,24 +122,28 @@ export function useFormEvents({ ...@@ -81,24 +122,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 {
...@@ -335,4 +380,3 @@ export function useFormEvents({ ...@@ -335,4 +380,3 @@ export function useFormEvents({
setFieldsValue, setFieldsValue,
scrollToField, scrollToField,
}; };
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册