Skip to content

Commit

Permalink
Properly render an example's html output in the Demo HTML tab (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall authored Jan 2, 2020
1 parent 3768e48 commit 9126176
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src-docs/src/services/string/render_to_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import html from 'html';

configure({ adapter: new EnzymeAdapter() });

export function renderToHtml(componentReference, props = {}) {
export function renderToHtml(ComponentReference, props = {}) {
// If there's a failure, just return an empty string. The actual component itself should
// trip an error boundary in the UI when it fails.
try {
// Create the React element, render it and get its HTML, then format it prettily.
const element = React.createElement(componentReference, props);
const htmlString = render(element).html();
// the .html() call below renders the contents of the first node, so wrap everything in a div
const element = (
<div>
<ComponentReference {...props} />
</div>
);
const renderedNodes = render(element);

const htmlString = renderedNodes.html();
return html.prettyPrint(htmlString, {
indent_size: 2,
unformatted: [], // Expand all tags, including spans
Expand Down

0 comments on commit 9126176

Please sign in to comment.