Эх сурвалжийг харах

fix(BasicForm): solve the error about setFieldValue array (#3775)

* fix(util): resolve notable to setFieldvalue array in the form

* perf(util): simpify the form event
Electrolux 1 жил өмнө
parent
commit
f5cd3ad593

+ 7 - 61
src/components/Form/src/hooks/useFormEvents.ts

@@ -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 {
@@ -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,
@@ -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 || {};
@@ -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;
       };
@@ -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((_) => {});