Skip to content

Commit

Permalink
feat: handle ref="manual-ref"
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Oct 24, 2015
1 parent 3fab843 commit 5b18191
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
31 changes: 31 additions & 0 deletions index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,37 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
.toEqual(
`<div>
Hello John
</div>`);
});

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

it(`reactElementToJSXString(<div aprop="test" ref="yes" />`, () => {
expect(
reactElementToJSXString(<div aprop="test" ref="yes" />)
).toEqual(
`<div
ref="yes"
aprop="test"
/>`);
});

it(`reactElementToJSXString(<div aprop="a" ref="yes"><span ref="wee" zprop="z"/></div>`, () => {
expect(
reactElementToJSXString(<div aprop="a" ref="yes"><span ref="wee" zprop="z"/></div>)
).toEqual(
`<div
ref="yes"
aprop="a"
>
<span
ref="wee"
zprop="z"
/>
</div>`);
});
});
35 changes: 23 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {

let out = `<${tagName}`;
let props = formatProps(ReactElement.props);
let attributes = [];
let children = ReactElement.props.children;

props.forEach(prop => {
if (props.length === 1 || inline) {
if (ReactElement.ref !== null) {
attributes.push(getJSXAttribute('ref', ReactElement.ref));
}

attributes = attributes.concat(props);

attributes.forEach(attribute => {
if (attributes.length === 1 || inline) {
out += ` `;
} else {
out += `\n${spacer(lvl + 1)}`;
}

out += `${prop.name}=${prop.value}`;
out += `${attribute.name}=${attribute.value}`;
});

if (props.length > 1 && !inline) {
if (attributes.length > 1 && !inline) {
out += `\n${spacer(lvl)}`;
}

Expand Down Expand Up @@ -71,7 +78,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
}
out += `</${tagName}>`;
} else {
if (props.length <= 1) {
if (attributes.length <= 1) {
out += ` `;
}

Expand Down Expand Up @@ -99,16 +106,20 @@ function formatProps(props) {
.filter(noChildren)
.sort()
.map(propName => {
return {
name: propName,
value: formatPropValue(props[propName])
.replace(/'?<__reactElementToJSXString__Wrapper__>/g, '')
.replace(/<\/__reactElementToJSXString__Wrapper__>'?/g, '')
};
return getJSXAttribute(propName, props[propName]);
});
}

function formatPropValue(propValue) {
function getJSXAttribute(name, value) {
return {
name,
value: formatJSXAttribute(value)
.replace(/'?<__reactElementToJSXString__Wrapper__>/g, '')
.replace(/<\/__reactElementToJSXString__Wrapper__>'?/g, '')
};
}

function formatJSXAttribute(propValue) {
if (typeof propValue === 'string') {
return `"${propValue}"`;
}
Expand Down

0 comments on commit 5b18191

Please sign in to comment.