Просмотр исходного кода

fix: fixed arguments of callbacks in `formApi` (#5970)

* 修复 `handleValuesChange` 传递的参数不是处理后的表单值的问题

* 修复 `handleReset` 未能传递正确参数的问题
Netfan 3 недель назад
Родитель
Сommit
f7a4d13a4c

+ 1 - 1
docs/src/components/common-ui/vben-form.md

@@ -327,7 +327,7 @@ useVbenForm 返回的第二个参数,是一个对象,包含了一些表单
 
 ::: tip handleValuesChange
 
-`handleValuesChange` 回调函数的第一个参数`values`装载了表单改变后的当前值对象,第二个参数`fieldsChanged`是一个数组,包含了所有被改变的字段名。注意:第二个参数仅在v5.5.4(不含)以上版本可用。
+`handleValuesChange` 回调函数的第一个参数`values`装载了表单改变后的当前值对象,第二个参数`fieldsChanged`是一个数组,包含了所有被改变的字段名。注意:第二个参数仅在v5.5.4(不含)以上版本可用,并且传递的是已在schema中定义的字段名。如果你使用了字段映射并且需要检查是哪些字段发生了变化的话,请注意该参数并不会包含映射后的字段名
 
 :::
 

+ 1 - 1
packages/@core/ui-kit/form-ui/src/components/form-actions.vue

@@ -62,7 +62,7 @@ async function handleReset(e: Event) {
   e?.stopPropagation();
   const props = unref(rootProps);
 
-  const values = toRaw(props.formApi?.getValues());
+  const values = toRaw(await props.formApi?.getValues());
 
   if (isFunction(props.handleReset)) {
     await props.handleReset?.(values);

+ 5 - 2
packages/@core/ui-kit/form-ui/src/vben-use-form.vue

@@ -72,7 +72,7 @@ onMounted(async () => {
   await nextTick();
   watch(
     () => form.values,
-    (newVal) => {
+    async (newVal) => {
       if (forward.value.handleValuesChange) {
         const fields = state.value.schema?.map((item) => {
           return item.fieldName;
@@ -91,7 +91,10 @@ onMounted(async () => {
 
           if (changedFields.length > 0) {
             // 调用handleValuesChange回调,传入所有表单值的深拷贝和变更的字段列表
-            forward.value.handleValuesChange(cloneDeep(newVal), changedFields);
+            forward.value.handleValuesChange(
+              cloneDeep(await forward.value.formApi.getValues()),
+              changedFields,
+            );
           }
         }
       }