Skip to content

Commit

Permalink
fix(children): ensure the array of children is well handled
Browse files Browse the repository at this point in the history
Add missing indentation + raw-string child
  • Loading branch information
redox committed Oct 16, 2015
1 parent 17c9571 commit 36b462a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 18 additions & 0 deletions index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,22 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
reactElementToJSXString(<div a={{}} />)
).toEqual(`<div a={{}} />`);
});

it(`reactElementToJSXString(<div><span /><span /></div>)`, () => {
expect(
reactElementToJSXString(<div><span /><span /></div>)
).toEqual(`<div>
<span />
<span />
</div>`);
});

it(`reactElementToJSXString(<div>foo<div /></div>)`, () => {
expect(
reactElementToJSXString(<div>foo<div /></div>)
).toEqual(`<div>
foo
<div />
</div>`);
});
});
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export default function reactElementToJSXString(ReactElement) {
}

function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
if (!isElement(ReactElement)) {
throw new Error('react-element-to-jsx-string: Expected a ReactElement');
if (typeof ReactElement === 'string') {
return ReactElement;
} else if (!isElement(ReactElement)) {
throw new Error('react-element-to-jsx-string: Expected a ReactElement, ' +
'got `' + (typeof ReactElement) + '`');
}

let tagName;
Expand Down Expand Up @@ -58,7 +61,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
.toArray(children)
.map(
recurse({lvl, inline})
);
).join('\n' + spacer(lvl));
}
if (!inline) {
out += `\n`;
Expand Down

0 comments on commit 36b462a

Please sign in to comment.