1- import React , { useEffect , useState } from 'react' ;
1+ import React from 'react' ;
22import { useFieldArray } from 'react-hook-form' ;
33
44import { usePydanticFormContext } from '@/core' ;
55import { fieldToComponentMatcher } from '@/core/helper' ;
6- import { PydanticFormElementProps } from '@/types' ;
6+ import {
7+ PydanticFormElementProps ,
8+ PydanticFormField ,
9+ PydanticFormFieldType ,
10+ } from '@/types' ;
711import { itemizeArrayItem } from '@/utils' ;
812
913import { RenderFields } from '../render' ;
1014
1115export const ArrayField = ( { pydanticFormField } : PydanticFormElementProps ) => {
1216 const { rhf, config } = usePydanticFormContext ( ) ;
1317
14- const [ isInitialized , setInitialized ] = useState ( false ) ;
1518 const { control } = rhf ;
16- const { id, arrayItem } = pydanticFormField ;
17- const { minItems, maxItems } = pydanticFormField . validations ;
19+ const { id : arrayName , arrayItem } = pydanticFormField ;
1820 const { fields, append, remove } = useFieldArray ( {
1921 control,
20- name : id ,
22+ name : arrayName ,
2123 } ) ;
24+ const { minItems = 1 , maxItems = undefined } =
25+ pydanticFormField ?. validations ;
2226
23- useEffect ( ( ) => {
24- if ( ! isInitialized ) {
25- const arrayValueObject = {
26- [ id ] : arrayItem ?. default ,
27- } ;
28- const minItemCount = minItems || 1 ;
29- const initialArray = Array . from (
30- { length : minItemCount } ,
31- ( ) => arrayValueObject ,
32- ) ;
33- append ( initialArray ) ;
34- setInitialized ( true ) ;
27+ const getInitialValue = ( arrayItem ?: PydanticFormField ) => {
28+ switch ( arrayItem ?. type ) {
29+ case PydanticFormFieldType . STRING :
30+ return '' ;
31+ case PydanticFormFieldType . INTEGER :
32+ return 0 ;
33+ case PydanticFormFieldType . BOOLEAN :
34+ return false ;
35+ default :
36+ return undefined ;
3537 }
36- } , [
37- append ,
38- arrayItem ?. default ,
39- id ,
40- fields . length ,
41- isInitialized ,
42- minItems ,
43- ] ) ;
38+ } ;
4439
4540 if ( ! arrayItem ) return '' ;
4641
@@ -50,7 +45,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
5045 ) ;
5146
5247 const renderField = ( field : Record < 'id' , string > , index : number ) => {
53- const arrayItemField = itemizeArrayItem ( index , arrayItem , id ) ;
48+ const arrayItemField = itemizeArrayItem ( index , arrayItem , arrayName ) ;
5449
5550 return (
5651 < div
@@ -69,7 +64,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
6964 pydanticFormField : arrayItemField ,
7065 } ,
7166 ] }
72- extraTriggerFields = { [ id ] }
67+ extraTriggerFields = { [ arrayName ] }
7368 />
7469 { ( ! minItems || ( minItems && fields . length > minItems ) ) && (
7570 < span
@@ -96,12 +91,12 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
9691 } }
9792 >
9893 { fields . map ( renderField ) }
99-
10094 { ( ! maxItems || ( maxItems && fields . length < maxItems ) ) && (
10195 < div
10296 onClick = { ( ) => {
10397 append ( {
104- [ id ] : arrayItem . default ?? undefined ,
98+ [ arrayName ] :
99+ arrayItem . default || getInitialValue ( arrayItem ) ,
105100 } ) ;
106101 } }
107102 style = { {
0 commit comments