@@ -4,13 +4,14 @@ import { useFieldArray } from 'react-hook-form';
44import { usePydanticFormContext } from '@/core' ;
55import { fieldToComponentMatcher } from '@/core/helper' ;
66import { PydanticFormElementProps } from '@/types' ;
7- import { itemizeArrayItem } from '@/utils' ;
7+ import { disableField , itemizeArrayItem } from '@/utils' ;
88
99import { RenderFields } from '../render' ;
1010
1111export const ArrayField = ( { pydanticFormField } : PydanticFormElementProps ) => {
1212 const { rhf, config } = usePydanticFormContext ( ) ;
1313
14+ const disabled = pydanticFormField . attributes ?. disabled || false ;
1415 const { control } = rhf ;
1516 const { id : arrayName , arrayItem } = pydanticFormField ;
1617 const { fields, append, remove } = useFieldArray ( {
@@ -28,7 +29,12 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
2829 ) ;
2930
3031 const renderField = ( field : Record < 'id' , string > , index : number ) => {
31- const arrayItemField = itemizeArrayItem ( index , arrayItem , arrayName ) ;
32+ const itemizedField = itemizeArrayItem ( index , arrayItem , arrayName ) ;
33+ // We have decided - for now - on the convention that all descendants of disabled fields will be disabled as well
34+ // so we will not displaying any interactive elements inside a disabled element
35+ const arrayItemField = disabled
36+ ? disableField ( itemizedField )
37+ : itemizedField ;
3238
3339 return (
3440 < div
@@ -49,15 +55,16 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
4955 ] }
5056 extraTriggerFields = { [ arrayName ] }
5157 />
52- { ( ! minItems || ( minItems && fields . length > minItems ) ) && (
53- < span
54- onClick = { ( ) => {
55- remove ( index ) ;
56- } }
57- >
58- -
59- </ span >
60- ) }
58+ { ( ! minItems || ( minItems && fields . length > minItems ) ) &&
59+ ! disabled && (
60+ < span
61+ onClick = { ( ) => {
62+ remove ( index ) ;
63+ } }
64+ >
65+ -
66+ </ span >
67+ ) }
6168 </ div >
6269 ) ;
6370 } ;
@@ -66,7 +73,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
6673 < div
6774 data-testid = { arrayName }
6875 style = { {
69- border : 'thin solid green ' ,
76+ border : '1px solid #ccc ' ,
7077 padding : '1rem' ,
7178 marginTop : '16px' ,
7279 display : 'flex' ,
@@ -75,26 +82,27 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
7582 } }
7683 >
7784 { fields . map ( renderField ) }
78- { ( ! maxItems || ( maxItems && fields . length < maxItems ) ) && (
79- < div
80- onClick = { ( ) => {
81- append ( {
82- [ arrayName ] : arrayItem . default ,
83- } ) ;
84- } }
85- style = { {
86- cursor : 'pointer' ,
87- fontSize : '32px' ,
88- display : 'flex' ,
89- justifyContent : 'end' ,
90- marginTop : '8px' ,
91- marginBottom : '8px' ,
92- padding : '16px' ,
93- } }
94- >
95- +
96- </ div >
97- ) }
85+ { ( ! maxItems || ( maxItems && fields . length < maxItems ) ) &&
86+ ! disabled && (
87+ < div
88+ onClick = { ( ) => {
89+ append ( {
90+ [ arrayName ] : arrayItem . default ,
91+ } ) ;
92+ } }
93+ style = { {
94+ cursor : 'pointer' ,
95+ fontSize : '32px' ,
96+ display : 'flex' ,
97+ justifyContent : 'end' ,
98+ marginTop : '8px' ,
99+ marginBottom : '8px' ,
100+ padding : '16px' ,
101+ } }
102+ >
103+ +
104+ </ div >
105+ ) }
98106 </ div >
99107 ) ;
100108} ;
0 commit comments