Skip to content

Commit 4b0199a

Browse files
updating composable for deep merging
1 parent eeaab2e commit 4b0199a

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/plugin/VStepperForm.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ import {
174174
import {
175175
useBuildSettings,
176176
useColumnErrorCheck,
177-
useMergeProps,
177+
useDeepMerge,
178178
} from './composables/helpers';
179179
import componentEmits from './utils/emits';
180180
import { AllProps } from './utils/props';
@@ -190,7 +190,7 @@ const injectedOptions = inject(globalOptions, {});
190190
191191
// -------------------------------------------------- Props //
192192
const props = withDefaults(defineProps<Props>(), AllProps);
193-
let stepperProps: Settings = reactive<Settings>(useMergeProps(attrs, injectedOptions, props));
193+
let stepperProps: Settings = reactive<Settings>(useDeepMerge(attrs, injectedOptions, props));
194194
const { direction, title, width } = toRefs(props);
195195
const pages = reactive<Page[]>(props.pages);
196196
const originalPages = JSON.parse(JSON.stringify(pages));
@@ -208,7 +208,7 @@ const stepperSettings = computed(() => useBindingSettings(settings.value as Part
208208
]));
209209
210210
watch(props, () => {
211-
stepperProps = useMergeProps(attrs, injectedOptions, props);
211+
stepperProps = useDeepMerge(attrs, injectedOptions, props);
212212
settings.value = useBuildSettings(stepperProps);
213213
}, { deep: true });
214214

src/plugin/composables/helpers.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
12
import { watchDebounced } from '@vueuse/core';
23
import {
34
UseAutoPage,
45
UseBuildSettings,
56
UseColumnErrorCheck,
6-
UseMergeProps,
7+
UseDeepMerge,
78
} from '@/plugin/types';
89

910

1011
/**
1112
* Merges props from three objects.
1213
*/
13-
export const useMergeProps: UseMergeProps = (A, B, C) => {
14-
const res: Record<string, any> = {};
15-
16-
Object.keys({ ...A, ...B, ...C }).map(key => {
17-
res[key] = (C[key] ?? B[key] ?? A[key]) as any;
18-
});
14+
type AnyObject = Record<string, any>;
15+
16+
export const useDeepMerge: UseDeepMerge = (A, B, C) => {
17+
const deepMerge = (obj1: AnyObject, obj2: AnyObject): AnyObject => {
18+
const result: AnyObject = { ...obj1 };
19+
for (const key in obj2) {
20+
if (
21+
obj2[key] &&
22+
typeof obj2[key] === 'object' &&
23+
!Array.isArray(obj2[key])
24+
) {
25+
result[key] = deepMerge(result[key] ?? {}, obj2[key]);
26+
}
27+
else {
28+
result[key] = obj2[key];
29+
}
30+
}
31+
return result;
32+
};
1933

20-
return res;
34+
// Merge A, B, and C with priority order C > B > A
35+
return deepMerge(deepMerge(A, B), C);
2136
};
2237

2338

src/plugin/types/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ export interface UseBuildSettings {
257257
): Settings;
258258
}
259259
// ------------------------- Helpers //
260-
export interface UseMergeProps {
260+
export interface UseDeepMerge {
261261
(
262262
A: Record<string, any>,
263-
B: PluginOptions,
264-
C: Props
263+
B: Record<string, any>,
264+
C: Record<string, any>
265265
): Record<string, any>;
266266
}
267267

0 commit comments

Comments
 (0)