Commit d09e998ae78d2fbe3d0537c6fca932a2ac484629

Authored by Joyboo
Committed by GitHub
1 parent e0976000

表单field支持a.b.c的写法 (#1549)

* chore: table size放到settings

* chore(TableAction): 操作确认框增加placement属性支持

* chore(Form): 表单field支持a.b.c嵌套写法

Co-authored-by: jinmao88 <50581550+jinmao88@users.noreply.github.com>
src/components/Form/src/hooks/useFormEvents.ts
@@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from &#39;vue&#39;; @@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from &#39;vue&#39;;
2 import type { FormProps, FormSchema, FormActionType } from '../types/form'; 2 import type { FormProps, FormSchema, FormActionType } from '../types/form';
3 import type { NamePath } from 'ant-design-vue/lib/form/interface'; 3 import type { NamePath } from 'ant-design-vue/lib/form/interface';
4 import { unref, toRaw, nextTick } from 'vue'; 4 import { unref, toRaw, nextTick } from 'vue';
5 -import { isArray, isFunction, isNullOrUnDef, isObject, isString } from '/@/utils/is'; 5 +import { isArray, isFunction, isObject, isString, isDef } from '/@/utils/is';
6 import { deepMerge } from '/@/utils'; 6 import { deepMerge } from '/@/utils';
7 import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; 7 import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper';
8 import { dateUtil } from '/@/utils/dateUtil'; 8 import { dateUtil } from '/@/utils/dateUtil';
@@ -55,6 +55,10 @@ export function useFormEvents({ @@ -55,6 +55,10 @@ export function useFormEvents({
55 .map((item) => item.field) 55 .map((item) => item.field)
56 .filter(Boolean); 56 .filter(Boolean);
57 57
  58 + // key 支持 a.b.c 的嵌套写法
  59 + const delimiter = '.';
  60 + const nestKeyArray = fields.filter((item) => item.indexOf(delimiter) >= 0);
  61 +
58 const validKeys: string[] = []; 62 const validKeys: string[] = [];
59 Object.keys(values).forEach((key) => { 63 Object.keys(values).forEach((key) => {
60 const schema = unref(getSchema).find((item) => item.field === key); 64 const schema = unref(getSchema).find((item) => item.field === key);
@@ -85,6 +89,21 @@ export function useFormEvents({ @@ -85,6 +89,21 @@ export function useFormEvents({
85 formModel[key] = value; 89 formModel[key] = value;
86 } 90 }
87 validKeys.push(key); 91 validKeys.push(key);
  92 + } else {
  93 + nestKeyArray.forEach((nestKey: string) => {
  94 + try {
  95 + const value = eval('values' + delimiter + nestKey);
  96 + if (isDef(value)) {
  97 + formModel[nestKey] = value;
  98 + validKeys.push(nestKey);
  99 + }
  100 + } catch (e) {
  101 + // key not exist
  102 + if (isDef(defaultValueRef.value[nestKey])) {
  103 + formModel[nestKey] = defaultValueRef.value[nestKey];
  104 + }
  105 + }
  106 + });
88 } 107 }
89 }); 108 });
90 validateFields(validKeys).catch((_) => {}); 109 validateFields(validKeys).catch((_) => {});