fix: default value for nested fields (#5763)

pull/62/head
Netfan 2025-03-21 16:06:14 +08:00 committed by YunaiV
parent 482227003c
commit 17f156de89
5 changed files with 36 additions and 3 deletions

View File

@ -88,6 +88,7 @@
"lodash.clonedeep": "catalog:",
"lodash.get": "catalog:",
"lodash.isequal": "catalog:",
"lodash.set": "catalog:",
"nprogress": "catalog:",
"tailwind-merge": "catalog:",
"theme-colors": "catalog:"
@ -96,6 +97,7 @@
"@types/lodash.clonedeep": "catalog:",
"@types/lodash.get": "catalog:",
"@types/lodash.isequal": "catalog:",
"@types/lodash.set": "catalog:",
"@types/nprogress": "catalog:"
}
}

View File

@ -17,3 +17,4 @@ export * from './window';
export { default as cloneDeep } from 'lodash.clonedeep';
export { default as get } from 'lodash.get';
export { default as isEqual } from 'lodash.isequal';
export { default as set } from 'lodash.set';

View File

@ -7,7 +7,7 @@ import type { ExtendedFormApi, FormActions, VbenFormProps } from './types';
import { computed, unref, useSlots } from 'vue';
import { createContext } from '@vben-core/shadcn-ui';
import { isString } from '@vben-core/shared/utils';
import { isString, mergeWithArrayOverride, set } from '@vben-core/shared/utils';
import { useForm } from 'vee-validate';
import { object } from 'zod';
@ -50,7 +50,7 @@ export function useFormInitial(
const zodObject: ZodRawShape = {};
(unref(props).schema || []).forEach((item) => {
if (Reflect.has(item, 'defaultValue')) {
initialValues[item.fieldName] = item.defaultValue;
set(initialValues, item.fieldName, item.defaultValue);
} else if (item.rules && !isString(item.rules)) {
zodObject[item.fieldName] = item.rules;
}
@ -58,7 +58,11 @@ export function useFormInitial(
const schemaInitialValues = getDefaultsForSchema(object(zodObject));
return { ...initialValues, ...schemaInitialValues };
const zodDefaults: Record<string, any> = {};
for (const key in schemaInitialValues) {
set(zodDefaults, key, schemaInitialValues[key]);
}
return mergeWithArrayOverride(initialValues, zodDefaults);
}
return {

View File

@ -99,6 +99,9 @@ catalogs:
'@types/lodash.isequal':
specifier: ^4.5.8
version: 4.5.8
'@types/lodash.set':
specifier: ^4.3.2
version: 4.3.9
'@types/node':
specifier: ^22.13.10
version: 22.13.10
@ -315,6 +318,9 @@ catalogs:
lodash.isequal:
specifier: ^4.5.0
version: 4.5.0
lodash.set:
specifier: ^4.3.2
version: 4.3.2
lucide-vue-next:
specifier: ^0.469.0
version: 0.469.0
@ -1252,6 +1258,9 @@ importers:
lodash.isequal:
specifier: 'catalog:'
version: 4.5.0
lodash.set:
specifier: 'catalog:'
version: 4.3.2
nprogress:
specifier: 'catalog:'
version: 0.2.0
@ -1271,6 +1280,9 @@ importers:
'@types/lodash.isequal':
specifier: 'catalog:'
version: 4.5.8
'@types/lodash.set':
specifier: 'catalog:'
version: 4.3.9
'@types/nprogress':
specifier: 'catalog:'
version: 0.2.3
@ -4304,6 +4316,9 @@ packages:
'@types/lodash.isequal@4.5.8':
resolution: {integrity: sha512-uput6pg4E/tj2LGxCZo9+y27JNyB2OZuuI/T5F+ylVDYuqICLG2/ktjxx0v6GvVntAf8TvEzeQLcV0ffRirXuA==}
'@types/lodash.set@4.3.9':
resolution: {integrity: sha512-KOxyNkZpbaggVmqbpr82N2tDVTx05/3/j0f50Es1prxrWB0XYf9p3QNxqcbWb7P1Q9wlvsUSlCFnwlPCIJ46PQ==}
'@types/lodash@4.17.16':
resolution: {integrity: sha512-HX7Em5NYQAXKW+1T+FiuG27NGwzJfCX3s1GjOa7ujxZa52kjJLOr4FUxT+giF6Tgxv1e+/czV/iTtBw27WTU9g==}
@ -7351,6 +7366,9 @@ packages:
lodash.once@4.1.1:
resolution: {integrity: sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==}
lodash.set@4.3.2:
resolution: {integrity: sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg==}
lodash.snakecase@4.1.1:
resolution: {integrity: sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==}
@ -13142,6 +13160,10 @@ snapshots:
dependencies:
'@types/lodash': 4.17.16
'@types/lodash.set@4.3.9':
dependencies:
'@types/lodash': 4.17.16
'@types/lodash@4.17.16': {}
'@types/markdown-it@14.1.2':
@ -16601,6 +16623,8 @@ snapshots:
lodash.once@4.1.1: {}
lodash.set@4.3.2: {}
lodash.snakecase@4.1.1: {}
lodash.sortby@4.7.0: {}

View File

@ -45,6 +45,7 @@ catalog:
'@types/lodash.clonedeep': ^4.5.9
'@types/lodash.get': ^4.4.9
'@types/lodash.isequal': ^4.5.8
'@types/lodash.set': ^4.3.2
'@types/node': ^22.13.10
'@types/nprogress': ^0.2.3
'@types/postcss-import': ^14.0.3
@ -119,6 +120,7 @@ catalog:
lint-staged: ^15.4.3
lodash.clonedeep: ^4.5.0
lodash.get: ^4.4.2
lodash.set: ^4.3.2
lodash.isequal: ^4.5.0
lucide-vue-next: ^0.469.0
medium-zoom: ^1.1.0