1
1
import { Ref , unref , ref , onBeforeUnmount , watch , MaybeRef } from 'vue' ;
2
+ import { klona as deepCopy } from 'klona/full' ;
2
3
import { isNullOrUndefined } from '../../shared' ;
3
4
import { FormContextKey } from './symbols' ;
4
5
import { FieldArrayContext , FieldEntry , PrivateFieldArrayContext , PrivateFormContext } from './types' ;
@@ -24,7 +25,7 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
24
25
25
26
if ( ! form ) {
26
27
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' ,
28
29
) ;
29
30
30
31
return noOpApi ;
@@ -77,7 +78,6 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
77
78
}
78
79
79
80
const key = entryCounter ++ ;
80
-
81
81
const entry : FieldEntry < TValue > = {
82
82
key,
83
83
value : computedDeep < TValue > ( {
@@ -96,7 +96,7 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
96
96
97
97
update ( idx , value ) ;
98
98
} ,
99
- } ) as any , // will be auto unwrapped
99
+ } ) as TValue , // will be auto unwrapped
100
100
isFirst : false ,
101
101
isLast : false ,
102
102
} ;
@@ -127,7 +127,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
127
127
afterMutation ( ) ;
128
128
}
129
129
130
- function push ( value : TValue ) {
130
+ function push ( initialValue : TValue ) {
131
+ const value = deepCopy ( initialValue ) ;
131
132
const pathName = unref ( arrayPath ) ;
132
133
const pathValue = getFromPath < TValue [ ] > ( form ?. values , pathName ) ;
133
134
const normalizedPathValue = isNullOrUndefined ( pathValue ) ? [ ] : pathValue ;
@@ -166,7 +167,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
166
167
updateEntryFlags ( ) ;
167
168
}
168
169
169
- function insert ( idx : number , value : TValue ) {
170
+ function insert ( idx : number , initialValue : TValue ) {
171
+ const value = deepCopy ( initialValue ) ;
170
172
const pathName = unref ( arrayPath ) ;
171
173
const pathValue = getFromPath < TValue [ ] > ( form ?. values , pathName ) ;
172
174
if ( ! Array . isArray ( pathValue ) || pathValue . length < idx ) {
@@ -202,7 +204,8 @@ export function useFieldArray<TValue = unknown>(arrayPath: MaybeRef<string>): Fi
202
204
form ?. validate ( { mode : 'validated-only' } ) ;
203
205
}
204
206
205
- function prepend ( value : TValue ) {
207
+ function prepend ( initialValue : TValue ) {
208
+ const value = deepCopy ( initialValue ) ;
206
209
const pathName = unref ( arrayPath ) ;
207
210
const pathValue = getFromPath < TValue [ ] > ( form ?. values , pathName ) ;
208
211
const normalizedPathValue = isNullOrUndefined ( pathValue ) ? [ ] : pathValue ;
0 commit comments