@@ -12,6 +12,33 @@ import SchemaErrorComponent from './schema-error-component';
1212
1313const isFunc = ( fn ) => typeof fn === 'function' ;
1414
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+
1542const FormRenderer = ( {
1643 actionMapper,
1744 children,
@@ -149,8 +176,7 @@ const FormRenderer = ({
149176 >
150177 { FormTemplate && < FormTemplate formFields = { formFields } schema = { schema } { ...FormTemplateProps } /> }
151178
152- { isFunc ( children ) && children ( { formFields, schema } ) }
153- { typeof children === 'object' && cloneElement ( children , { formFields, schema } ) }
179+ { children && renderChildren ( children , { formFields, schema } ) }
154180 </ RendererContext . Provider >
155181 ) }
156182 { ...props }
0 commit comments