11import * as React from 'react' ;
2+ import { useForm , FormProvider , useFieldArray } from 'react-hook-form' ;
23import { CoreAdminContext } from '../core' ;
34import { Form } from './Form' ;
45import { InputProps , useInput } from './useInput' ;
@@ -7,12 +8,15 @@ export default {
78 title : 'ra-core/form/useInput' ,
89} ;
910
10- const Input = ( props : InputProps ) => {
11+ const Input = ( props : InputProps & { log ?: boolean } ) => {
12+ const { label, log } = props ;
1113 const { id, field, fieldState } = useInput ( props ) ;
12-
14+ if ( log ) {
15+ console . log ( `Input ${ id } rendered:` ) ;
16+ }
1317 return (
1418 < label htmlFor = { id } >
15- { id } : < input id = { id } { ...field } />
19+ { label ?? id } : < input id = { id } { ...field } />
1620 { fieldState . error && < span > { fieldState . error . message } </ span > }
1721 </ label >
1822 ) ;
@@ -86,3 +90,86 @@ DefaultValue.argTypes = {
8690 control : { type : 'select' } ,
8791 } ,
8892} ;
93+
94+ export const Large = ( ) => {
95+ const [ submittedData , setSubmittedData ] = React . useState < any > ( ) ;
96+ const fields = Array . from ( { length : 15 } ) . map ( ( _ , index ) => (
97+ < Input
98+ key = { index }
99+ source = { `field${ index + 1 } ` }
100+ label = { `field${ index + 1 } ` }
101+ />
102+ ) ) ;
103+ return (
104+ < CoreAdminContext >
105+ < Form
106+ onSubmit = { data => setSubmittedData ( data ) }
107+ record = { Array . from ( { length : 15 } ) . reduce ( ( acc , _ , index ) => {
108+ acc [ `field${ index + 1 } ` ] = `value${ index + 1 } ` ;
109+ return acc ;
110+ } , { } ) }
111+ >
112+ < div
113+ style = { {
114+ display : 'flex' ,
115+ flexDirection : 'column' ,
116+ gap : '1em' ,
117+ marginBottom : '1em' ,
118+ } }
119+ >
120+ { fields }
121+ </ div >
122+ < button type = "submit" > Submit</ button >
123+ </ Form >
124+ < pre > { JSON . stringify ( submittedData , null , 2 ) } </ pre >
125+ </ CoreAdminContext >
126+ ) ;
127+ } ;
128+
129+ const FieldArray = ( ) => {
130+ const { fields } = useFieldArray ( {
131+ name : 'arrayField' ,
132+ } ) ;
133+ return (
134+ < div style = { { display : 'flex' , flexDirection : 'column' , gap : '1em' } } >
135+ { fields . map ( ( field , index ) => (
136+ < Input key = { field . id } source = { `arrayField.${ index } .name` } log />
137+ ) ) }
138+ </ div >
139+ ) ;
140+ } ;
141+ export const ArrayOfFields = ( ) => {
142+ const formValue = useForm ( {
143+ defaultValues : {
144+ arrayField : Array . from ( { length : 50 } , ( _ , index ) => ( {
145+ id : index + 1 ,
146+ name : `Item ${ index + 1 } ` ,
147+ } ) ) ,
148+ } ,
149+ } ) ;
150+ const [ submittedData , setSubmittedData ] = React . useState < any > ( ) ;
151+ return (
152+ < CoreAdminContext >
153+ < FormProvider { ...formValue } >
154+ < form
155+ onSubmit = { formValue . handleSubmit ( data =>
156+ setSubmittedData ( data )
157+ ) }
158+ >
159+ < div
160+ style = { {
161+ display : 'flex' ,
162+ flexDirection : 'column' ,
163+ gap : '1em' ,
164+ marginBottom : '1em' ,
165+ } }
166+ >
167+ < FieldArray />
168+ </ div >
169+ < button type = "submit" > Submit</ button >
170+ </ form >
171+ </ FormProvider >
172+ < pre > { JSON . stringify ( submittedData , null , 2 ) } </ pre >
173+ </ CoreAdminContext >
174+ ) ;
175+ } ;
0 commit comments