@@ -31,19 +31,26 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
31
31
32
32
let out = `<${ tagName } ` ;
33
33
let props = formatProps ( ReactElement . props ) ;
34
+ let attributes = [ ] ;
34
35
let children = ReactElement . props . children ;
35
36
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 ) {
38
45
out += ` ` ;
39
46
} else {
40
47
out += `\n${ spacer ( lvl + 1 ) } ` ;
41
48
}
42
49
43
- out += `${ prop . name } =${ prop . value } ` ;
50
+ out += `${ attribute . name } =${ attribute . value } ` ;
44
51
} ) ;
45
52
46
- if ( props . length > 1 && ! inline ) {
53
+ if ( attributes . length > 1 && ! inline ) {
47
54
out += `\n${ spacer ( lvl ) } ` ;
48
55
}
49
56
@@ -71,7 +78,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
71
78
}
72
79
out += `</${ tagName } >` ;
73
80
} else {
74
- if ( props . length <= 1 ) {
81
+ if ( attributes . length <= 1 ) {
75
82
out += ` ` ;
76
83
}
77
84
@@ -99,16 +106,20 @@ function formatProps(props) {
99
106
. filter ( noChildren )
100
107
. sort ( )
101
108
. map ( propName => {
102
- return {
103
- name : propName ,
104
- value : formatPropValue ( props [ propName ] )
105
- . replace ( / ' ? < _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ W r a p p e r _ _ > / g, '' )
106
- . replace ( / < \/ _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ W r a p p e r _ _ > ' ? / g, '' )
107
- } ;
109
+ return getJSXAttribute ( propName , props [ propName ] ) ;
108
110
} ) ;
109
111
}
110
112
111
- function formatPropValue ( propValue ) {
113
+ function getJSXAttribute ( name , value ) {
114
+ return {
115
+ name,
116
+ value : formatJSXAttribute ( value )
117
+ . replace ( / ' ? < _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ W r a p p e r _ _ > / g, '' )
118
+ . replace ( / < \/ _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ W r a p p e r _ _ > ' ? / g, '' )
119
+ } ;
120
+ }
121
+
122
+ function formatJSXAttribute ( propValue ) {
112
123
if ( typeof propValue === 'string' ) {
113
124
return `"${ propValue } "` ;
114
125
}
0 commit comments