8
8
ReactNode ,
9
9
useCallback ,
10
10
useMemo ,
11
+ useRef ,
11
12
} from 'react' ;
12
13
import { styled , SxProps } from '@mui/material' ;
13
14
import clsx from 'clsx' ;
@@ -49,6 +50,7 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
49
50
} = props ;
50
51
const { append, fields, move, remove } = useArrayInput ( props ) ;
51
52
const record = useRecordContext ( props ) ;
53
+ const initialDefaultValue = useRef ( { } ) ;
52
54
53
55
const removeField = useCallback (
54
56
( index : number ) => {
@@ -57,10 +59,18 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
57
59
[ remove ]
58
60
) ;
59
61
62
+ if ( fields . length > 0 ) {
63
+ const { id, ...rest } = fields [ 0 ] ;
64
+ initialDefaultValue . current = rest ;
65
+ for ( const k in initialDefaultValue . current )
66
+ initialDefaultValue . current [ k ] = '' ;
67
+ }
68
+
60
69
const addField = useCallback (
61
70
( item : any = undefined ) => {
62
71
let defaultValue = item ;
63
72
if ( item == null ) {
73
+ defaultValue = initialDefaultValue . current ;
64
74
if (
65
75
Children . count ( children ) === 1 &&
66
76
React . isValidElement ( Children . only ( children ) ) &&
@@ -73,11 +83,13 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
73
83
} else {
74
84
// ArrayInput used for an array of objects
75
85
// (e.g. authors: [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Doe' }])
76
- defaultValue = { } as Record < string , unknown > ;
86
+ defaultValue =
87
+ defaultValue || ( { } as Record < string , unknown > ) ;
77
88
Children . forEach ( children , input => {
78
89
if (
79
90
React . isValidElement ( input ) &&
80
- input . type !== FormDataConsumer
91
+ input . type !== FormDataConsumer &&
92
+ input . props . source
81
93
) {
82
94
defaultValue [ input . props . source ] =
83
95
input . props . defaultValue ?? '' ;
0 commit comments