Skip to content

Commit 5b18191

Browse files
author
vvo
committed
feat: handle ref="manual-ref"
1 parent 3fab843 commit 5b18191

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

index-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,37 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
295295
.toEqual(
296296
`<div>
297297
Hello John
298+
</div>`);
299+
});
300+
301+
it(`reactElementToJSXString(<div a={[<div><span /></div>]} />`, () => {
302+
expect(
303+
reactElementToJSXString(<div a={[<div><span /></div>]} />)
304+
).toEqual(`<div a={[<div><span /></div>]} />`);
305+
});
306+
307+
it(`reactElementToJSXString(<div aprop="test" ref="yes" />`, () => {
308+
expect(
309+
reactElementToJSXString(<div aprop="test" ref="yes" />)
310+
).toEqual(
311+
`<div
312+
ref="yes"
313+
aprop="test"
314+
/>`);
315+
});
316+
317+
it(`reactElementToJSXString(<div aprop="a" ref="yes"><span ref="wee" zprop="z"/></div>`, () => {
318+
expect(
319+
reactElementToJSXString(<div aprop="a" ref="yes"><span ref="wee" zprop="z"/></div>)
320+
).toEqual(
321+
`<div
322+
ref="yes"
323+
aprop="a"
324+
>
325+
<span
326+
ref="wee"
327+
zprop="z"
328+
/>
298329
</div>`);
299330
});
300331
});

index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,26 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
3131

3232
let out = `<${tagName}`;
3333
let props = formatProps(ReactElement.props);
34+
let attributes = [];
3435
let children = ReactElement.props.children;
3536

36-
props.forEach(prop => {
37-
if (props.length === 1 || inline) {
37+
if (ReactElement.ref !== null) {
38+
attributes.push(getJSXAttribute('ref', ReactElement.ref));
39+
}
40+
41+
attributes = attributes.concat(props);
42+
43+
attributes.forEach(attribute => {
44+
if (attributes.length === 1 || inline) {
3845
out += ` `;
3946
} else {
4047
out += `\n${spacer(lvl + 1)}`;
4148
}
4249

43-
out += `${prop.name}=${prop.value}`;
50+
out += `${attribute.name}=${attribute.value}`;
4451
});
4552

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

@@ -71,7 +78,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
7178
}
7279
out += `</${tagName}>`;
7380
} else {
74-
if (props.length <= 1) {
81+
if (attributes.length <= 1) {
7582
out += ` `;
7683
}
7784

@@ -99,16 +106,20 @@ function formatProps(props) {
99106
.filter(noChildren)
100107
.sort()
101108
.map(propName => {
102-
return {
103-
name: propName,
104-
value: formatPropValue(props[propName])
105-
.replace(/'?<__reactElementToJSXString__Wrapper__>/g, '')
106-
.replace(/<\/__reactElementToJSXString__Wrapper__>'?/g, '')
107-
};
109+
return getJSXAttribute(propName, props[propName]);
108110
});
109111
}
110112

111-
function formatPropValue(propValue) {
113+
function getJSXAttribute(name, value) {
114+
return {
115+
name,
116+
value: formatJSXAttribute(value)
117+
.replace(/'?<__reactElementToJSXString__Wrapper__>/g, '')
118+
.replace(/<\/__reactElementToJSXString__Wrapper__>'?/g, '')
119+
};
120+
}
121+
122+
function formatJSXAttribute(propValue) {
112123
if (typeof propValue === 'string') {
113124
return `"${propValue}"`;
114125
}

0 commit comments

Comments
 (0)