Skip to content

Commit 748077d

Browse files
committed
feat(Template): allow template functions to return a React element
1 parent 0f9296d commit 748077d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/components/Template.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,22 @@ function Template(props) {
2727
}
2828

2929
const otherProps = omit(props, keys(Template.propTypes));
30-
const isElement = React.isValidElement(content);
30+
31+
if (React.isValidElement(content)) {
32+
return (
33+
<div
34+
{...otherProps}
35+
className={props.cssClass}
36+
>{content}</div>
37+
);
38+
}
3139

3240
return (
3341
<div
3442
{...otherProps}
3543
className={props.cssClass}
36-
dangerouslySetInnerHTML={isElement ? null : {__html: content}}
37-
>
38-
{isElement ? content : null}
39-
</div>
44+
dangerouslySetInnerHTML={{__html: content}}
45+
/>
4046
);
4147
}
4248

src/components/__tests__/Template-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Template', () => {
5454
const out = renderer.getRenderOutput();
5555

5656
const content = 'it also works with functions';
57-
expect(out).toEqualJSX(<div className={undefined} dangerouslySetInnerHTML={null}><p>{content}</p></div>);
57+
expect(out).toEqualJSX(<div className={undefined}><p>{content}</p></div>);
5858
});
5959

6060
it('can configure compilation options', () => {

0 commit comments

Comments
 (0)