Skip to content

Commit 9290f5a

Browse files
committedJul 15, 2023
fix: clone values inserted into field arrays closes #4372
1 parent c07627e commit 9290f5a

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed
 

‎.changeset/curvy-eagles-build.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': major
3+
---
4+
5+
fix: clone values inserted into field arrays closes #4372

‎packages/nuxt/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
"dist/*"
3838
],
3939
"dependencies": {
40-
"@nuxt/kit": "^3.6.2",
40+
"@nuxt/kit": "^3.6.3",
4141
"local-pkg": "^0.4.3",
4242
"vee-validate": "^4.10.6"
4343
},
4444
"devDependencies": {
4545
"@nuxt/eslint-config": "^0.1.1",
4646
"@nuxt/module-builder": "^0.4.0",
47-
"@nuxt/schema": "^3.6.2",
48-
"@nuxt/test-utils": "^3.6.2",
49-
"nuxt": "^3.6.2"
47+
"@nuxt/schema": "^3.6.3",
48+
"@nuxt/test-utils": "^3.6.3",
49+
"nuxt": "^3.6.3"
5050
}
5151
}

‎packages/vee-validate/src/useFieldArray.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Ref, unref, ref, onBeforeUnmount, watch, MaybeRef } from 'vue';
2+
import { klona as deepCopy } from 'klona/full';
23
import { isNullOrUndefined } from '../../shared';
34
import { FormContextKey } from './symbols';
45
import { FieldArrayContext, FieldEntry, PrivateFieldArrayContext, PrivateFormContext } from './types';
@@ -24,7 +25,7 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
2425

2526
if (!form) {
2627
warn(
27-
'FieldArray requires being a child of `<Form/>` or `useForm` being called before it. Array fields may not work correctly'
28+
'FieldArray requires being a child of `<Form/>` or `useForm` being called before it. Array fields may not work correctly',
2829
);
2930

3031
return noOpApi;
@@ -77,7 +78,6 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
7778
}
7879

7980
const key = entryCounter++;
80-
8181
const entry: FieldEntry<TValue> = {
8282
key,
8383
value: computedDeep<TValue>({
@@ -96,7 +96,7 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
9696

9797
update(idx, value);
9898
},
99-
}) as any, // will be auto unwrapped
99+
}) as TValue, // will be auto unwrapped
100100
isFirst: false,
101101
isLast: false,
102102
};
@@ -127,7 +127,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
127127
afterMutation();
128128
}
129129

130-
function push(value: TValue) {
130+
function push(initialValue: TValue) {
131+
const value = deepCopy(initialValue);
131132
const pathName = unref(arrayPath);
132133
const pathValue = getFromPath<TValue[]>(form?.values, pathName);
133134
const normalizedPathValue = isNullOrUndefined(pathValue) ? [] : pathValue;
@@ -166,7 +167,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
166167
updateEntryFlags();
167168
}
168169

169-
function insert(idx: number, value: TValue) {
170+
function insert(idx: number, initialValue: TValue) {
171+
const value = deepCopy(initialValue);
170172
const pathName = unref(arrayPath);
171173
const pathValue = getFromPath<TValue[]>(form?.values, pathName);
172174
if (!Array.isArray(pathValue) || pathValue.length < idx) {
@@ -202,7 +204,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
202204
form?.validate({ mode: 'validated-only' });
203205
}
204206

205-
function prepend(value: TValue) {
207+
function prepend(initialValue: TValue) {
208+
const value = deepCopy(initialValue);
206209
const pathName = unref(arrayPath);
207210
const pathValue = getFromPath<TValue[]>(form?.values, pathName);
208211
const normalizedPathValue = isNullOrUndefined(pathValue) ? [] : pathValue;

0 commit comments

Comments
 (0)
Please sign in to comment.