11import { Ref , unref , ref , onBeforeUnmount , watch , MaybeRef } from 'vue' ;
2+ import { klona as deepCopy } from 'klona/full' ;
23import { isNullOrUndefined } from '../../shared' ;
34import { FormContextKey } from './symbols' ;
45import { 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