@@ -12,6 +12,33 @@ import SchemaErrorComponent from './schema-error-component';
12
12
13
13
const isFunc = ( fn ) => typeof fn === 'function' ;
14
14
15
+ const renderChildren = ( children , props ) => {
16
+ if ( isFunc ( children ) ) {
17
+ return children ( props ) ;
18
+ }
19
+
20
+ let childElement = children ;
21
+ if ( Array . isArray ( children ) ) {
22
+ /**
23
+ * Only permit one child element
24
+ */
25
+ if ( children . length !== 1 ) {
26
+ throw new Error ( 'FormRenderer expect only one child element!' ) ;
27
+ }
28
+
29
+ childElement = children [ 0 ] ;
30
+ }
31
+
32
+ if ( typeof childElement === 'object' ) {
33
+ /**
34
+ * Clone react element, pass form fields and schema as props, but override them with child props if present
35
+ */
36
+ return cloneElement ( children , { ...props , ...childElement . props } ) ;
37
+ }
38
+
39
+ throw new Error ( `Invalid children prop! Expected one of [null, Function, object], got ${ typeof children } ` ) ;
40
+ } ;
41
+
15
42
const FormRenderer = ( {
16
43
actionMapper,
17
44
children,
@@ -149,8 +176,7 @@ const FormRenderer = ({
149
176
>
150
177
{ FormTemplate && < FormTemplate formFields = { formFields } schema = { schema } { ...FormTemplateProps } /> }
151
178
152
- { isFunc ( children ) && children ( { formFields, schema } ) }
153
- { typeof children === 'object' && cloneElement ( children , { formFields, schema } ) }
179
+ { children && renderChildren ( children , { formFields, schema } ) }
154
180
</ RendererContext . Provider >
155
181
) }
156
182
{ ...props }
0 commit comments