diff --git a/packages/@core/base/shared/package.json b/packages/@core/base/shared/package.json index ebf481bf6..70ad17c35 100644 --- a/packages/@core/base/shared/package.json +++ b/packages/@core/base/shared/package.json @@ -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:" } } diff --git a/packages/@core/base/shared/src/utils/index.ts b/packages/@core/base/shared/src/utils/index.ts index 1bf09c713..925af1c12 100644 --- a/packages/@core/base/shared/src/utils/index.ts +++ b/packages/@core/base/shared/src/utils/index.ts @@ -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'; diff --git a/packages/@core/ui-kit/form-ui/src/use-form-context.ts b/packages/@core/ui-kit/form-ui/src/use-form-context.ts index 0374cf326..60148bfba 100644 --- a/packages/@core/ui-kit/form-ui/src/use-form-context.ts +++ b/packages/@core/ui-kit/form-ui/src/use-form-context.ts @@ -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 = {}; + for (const key in schemaInitialValues) { + set(zodDefaults, key, schemaInitialValues[key]); + } + return mergeWithArrayOverride(initialValues, zodDefaults); } return { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5e23a946e..45d791c54 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 2c4118109..b8d844aa0 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -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