Skip to content

Commit 269cc16

Browse files
committed
fix(renderer): allow additional renderer children options
1 parent 633f024 commit 269cc16

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/react-form-renderer/src/form-renderer/form-renderer.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ import SchemaErrorComponent from './schema-error-component';
1212

1313
const 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+
1542
const 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

Comments
 (0)