@@ -59,10 +59,14 @@ const generateUnformattedCode = (
59
59
referencedHTML . tag === 'ul' ||
60
60
referencedHTML . tag === 'ol' ||
61
61
referencedHTML . tag === 'menu' ||
62
- referencedHTML . tag === 'li'
62
+ referencedHTML . tag === 'li' ||
63
+ referencedHTML . tag === 'Switch' ||
64
+ referencedHTML . tag === 'Route'
63
65
) {
64
66
child . children = getEnrichedChildren ( child ) ;
65
67
}
68
+ // when we see a Switch, import React Router
69
+ if ( referencedHTML . tag === 'Switch' ) importReactRouter = true ;
66
70
return child ;
67
71
} else if ( child . type === 'Route Link' ) {
68
72
links = true ;
@@ -89,7 +93,7 @@ const generateUnformattedCode = (
89
93
// function to dynamically add classes, ids, and styles to an element if it exists.
90
94
const elementTagDetails = ( childElement : object ) => {
91
95
let customizationDetails = "" ;
92
- if ( childElement . childId ) customizationDetails += ( ' ' + `id="${ + childElement . childId } "` ) ;
96
+ if ( childElement . childId && childElement . tag !== 'Route' ) customizationDetails += ( ' ' + `id="${ + childElement . childId } "` ) ;
93
97
if ( childElement . attributes && childElement . attributes . cssClasses ) customizationDetails += ( ' ' + `className="${ childElement . attributes . cssClasses } "` ) ;
94
98
if ( childElement . style && Object . keys ( childElement . style ) . length > 0 ) customizationDetails += ( ' ' + formatStyles ( childElement ) ) ;
95
99
return customizationDetails ;
@@ -120,8 +124,9 @@ const generateUnformattedCode = (
120
124
childElement . tag === 'ol' ||
121
125
childElement . tag === 'ul' ||
122
126
childElement . tag === 'menu' ||
123
- childElement . tag === 'li' ;
124
- // childElement.tag === 'Switch';
127
+ childElement . tag === 'li' ||
128
+ childElement . tag === 'Switch' ||
129
+ childElement . tag === 'Route' ;
125
130
126
131
if ( childElement . tag === 'img' ) {
127
132
return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } src="${ activeLink } " ${ elementTagDetails ( childElement ) } />${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
@@ -130,7 +135,9 @@ const generateUnformattedCode = (
130
135
} else if ( childElement . tag === 'input' ) {
131
136
return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } ></${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
132
137
} else if ( nestable ) {
133
- return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText }
138
+ // if Route -> 'exact path=' + activeLink
139
+ const routePath = ( childElement . tag === 'Route' ) ? ( ' ' + 'exact path="' + activeLink + '"' ) : '' ;
140
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } ${ routePath } >${ innerText }
134
141
${ tabSpacer ( level ) } ${ writeNestedElements ( childElement . children , level + 1 ) }
135
142
${ tabSpacer ( level - 1 ) } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
136
143
} else if ( childElement . tag !== 'separator' ) {
@@ -195,12 +202,14 @@ const generateUnformattedCode = (
195
202
196
203
const stateful = true ;
197
204
const classBased = false ;
205
+ let importReactRouter ;
198
206
199
207
// create final component code. component code differs between classic react, next.js, gatsby.js
200
208
// classic react code
201
209
if ( projectType === 'Classic React' ) {
202
210
return `
203
211
${ stateful && ! classBased ? `import React, {useState} from 'react';` : '' }
212
+ ${ importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';` : `` }
204
213
${ classBased ? `import React, {Component} from 'react';` : '' }
205
214
${ ! stateful && ! classBased ? `import React from 'react';` : '' }
206
215
${ importsMapped }
@@ -209,7 +218,6 @@ const generateUnformattedCode = (
209
218
? `class ${ currentComponent . name } extends Component {`
210
219
: `const ${ currentComponent . name } = (props: any): JSX.Element => {`
211
220
}
212
-
213
221
${
214
222
stateful && ! classBased
215
223
? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE")${ writeStateProps ( currentComponent . useStateCodes ) } ;
@@ -225,13 +233,18 @@ const generateUnformattedCode = (
225
233
: ``
226
234
}
227
235
${ classBased ? `render(): JSX.Element {` : `` }
228
-
229
- return (
236
+ ${ ! importReactRouter ?
237
+ `return (
238
+ <div className="${ currentComponent . name } " style={props.style}>
239
+ ${ writeNestedElements ( enrichedChildren ) }
240
+ </div>
241
+ );` : `return (
242
+ <Router>
230
243
<div className="${ currentComponent . name } " style={props.style}>
231
244
${ writeNestedElements ( enrichedChildren ) }
232
245
</div>
233
- );
234
- }
246
+ </Router>
247
+ );` }
235
248
${ classBased ? `}` : `` }
236
249
export default ${ currentComponent . name } ;
237
250
` ;
0 commit comments