fix: form-api.setValues can't resolve nested fields (#4915)

fix #4912
pull/48/MERGE
ryomahan 2024-11-17 21:04:35 +08:00 committed by GitHub
parent 94c68c966e
commit fe58af2e78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 4 deletions

View File

@ -13,13 +13,13 @@ import { toRaw } from 'vue';
import { Store } from '@vben-core/shared/store';
import {
bindMethods,
createMerge,
isFunction,
isObject,
mergeWithArrayOverride,
StateHandler,
} from '@vben-core/shared/utils';
import { objectPick } from '@vueuse/core';
function getDefaultState(): VbenFormProps {
return {
actionWrapperClass: '',
@ -250,8 +250,17 @@ export class FormApi {
form.setValues(fields, shouldValidate);
return;
}
const fieldNames = this.state?.schema?.map((item) => item.fieldName) ?? [];
const filteredFields = objectPick(fields, fieldNames);
const fieldMergeFn = createMerge((obj, key, value) => {
if (key in obj) {
obj[key] =
!Array.isArray(obj[key]) && isObject(obj[key])
? fieldMergeFn(obj[key], value)
: value;
}
return true;
});
const filteredFields = fieldMergeFn(fields, form.values);
form.setValues(filteredFields, shouldValidate);
}