11import * as React from 'react' ;
2- import { Children , type ReactNode , useMemo , useRef } from 'react' ;
2+ import { type ReactNode , useMemo } from 'react' ;
33import { type UseFieldArrayReturn , useFormContext } from 'react-hook-form' ;
4- import { FormDataConsumer } from '../../form/FormDataConsumer' ;
54import { useWrappedSource } from '../../core/useWrappedSource' ;
65import type { RaRecord } from '../../types' ;
76import { useEvent } from '../../util' ;
87import { useArrayInput } from './useArrayInput' ;
98import { SimpleFormIteratorContext } from './SimpleFormIteratorContext' ;
109
10+ const DefaultOnAddItem = item => item ;
11+
1112export const SimpleFormIteratorBase = ( props : SimpleFormIteratorBaseProps ) => {
12- const { children, inputs } = props ;
13+ const { children, onAddItem : onAddItemProp = DefaultOnAddItem } = props ;
14+ const onAddItem = useEvent ( onAddItemProp ) ;
1315
1416 const finalSource = useWrappedSource ( '' ) ;
1517 if ( ! finalSource ) {
@@ -20,7 +22,6 @@ export const SimpleFormIteratorBase = (props: SimpleFormIteratorBaseProps) => {
2022
2123 const { append, fields, move, remove, replace } = useArrayInput ( props ) ;
2224 const { trigger, getValues } = useFormContext ( ) ;
23- const initialDefaultValue = useRef ( { } ) ;
2425
2526 const removeField = useEvent ( ( index : number ) => {
2627 remove ( index ) ;
@@ -34,46 +35,8 @@ export const SimpleFormIteratorBase = (props: SimpleFormIteratorBaseProps) => {
3435 }
3536 } ) ;
3637
37- if ( fields . length > 0 ) {
38- const { id, ...rest } = fields [ 0 ] ;
39- initialDefaultValue . current = rest ;
40- for ( const k in initialDefaultValue . current )
41- initialDefaultValue . current [ k ] = null ;
42- }
43-
4438 const addField = useEvent ( ( item : any = undefined ) => {
45- let defaultValue = item ;
46- if ( item == null ) {
47- defaultValue = initialDefaultValue . current ;
48- if (
49- Children . count ( inputs ) === 1 &&
50- React . isValidElement ( Children . only ( inputs ) ) &&
51- // @ts -ignore
52- ! Children . only ( inputs ) . props . source &&
53- // Make sure it's not a FormDataConsumer
54- // @ts -ignore
55- Children . only ( inputs ) . type !== FormDataConsumer
56- ) {
57- // ArrayInput used for an array of scalar values
58- // (e.g. tags: ['foo', 'bar'])
59- defaultValue = '' ;
60- } else {
61- // ArrayInput used for an array of objects
62- // (e.g. authors: [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Doe' }])
63- defaultValue = defaultValue || ( { } as Record < string , unknown > ) ;
64- Children . forEach ( inputs , input => {
65- if (
66- React . isValidElement ( input ) &&
67- input . type !== FormDataConsumer &&
68- input . props . source
69- ) {
70- defaultValue [ input . props . source ] =
71- input . props . defaultValue ?? null ;
72- }
73- } ) ;
74- }
75- }
76- append ( defaultValue ) ;
39+ append ( onAddItem ( item ) ) ;
7740 } ) ;
7841
7942 const handleReorder = useEvent ( ( origin : number , destination : number ) => {
@@ -117,12 +80,12 @@ export interface SimpleFormIteratorBaseProps
11780 extends Partial < UseFieldArrayReturn > {
11881 children : ReactNode ;
11982 inline ?: boolean ;
120- inputs : ReactNode ;
12183 meta ?: {
12284 // the type defined in FieldArrayRenderProps says error is boolean, which is wrong.
12385 error ?: any ;
12486 submitFailed ?: boolean ;
12587 } ;
88+ onAddItem ?: ( item : any ) => any ;
12689 record ?: RaRecord ;
12790 resource ?: string ;
12891 source ?: string ;
0 commit comments