Skip to content

Commit a760bb0

Browse files
committed
generate code for switch. import reactrouter. spacing issues still in code preview.
1 parent 9410714 commit a760bb0

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

app/src/helperFunctions/generateCode.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ const generateUnformattedCode = (
5959
referencedHTML.tag === 'ul' ||
6060
referencedHTML.tag === 'ol' ||
6161
referencedHTML.tag === 'menu' ||
62-
referencedHTML.tag === 'li'
62+
referencedHTML.tag === 'li' ||
63+
referencedHTML.tag === 'Switch' ||
64+
referencedHTML.tag === 'Route'
6365
) {
6466
child.children = getEnrichedChildren(child);
6567
}
68+
// when we see a Switch, import React Router
69+
if (referencedHTML.tag === 'Switch') importReactRouter = true;
6670
return child;
6771
} else if (child.type === 'Route Link') {
6872
links = true;
@@ -89,7 +93,7 @@ const generateUnformattedCode = (
8993
// function to dynamically add classes, ids, and styles to an element if it exists.
9094
const elementTagDetails = (childElement: object) => {
9195
let customizationDetails = "";
92-
if (childElement.childId) customizationDetails += (' ' + `id="${+childElement.childId}"`);
96+
if (childElement.childId && childElement.tag !== 'Route') customizationDetails += (' ' + `id="${+childElement.childId}"`);
9397
if (childElement.attributes && childElement.attributes.cssClasses) customizationDetails += (' ' + `className="${childElement.attributes.cssClasses}"`);
9498
if (childElement.style && Object.keys(childElement.style).length > 0) customizationDetails +=(' ' + formatStyles(childElement));
9599
return customizationDetails;
@@ -120,8 +124,9 @@ const generateUnformattedCode = (
120124
childElement.tag === 'ol' ||
121125
childElement.tag === 'ul' ||
122126
childElement.tag === 'menu' ||
123-
childElement.tag === 'li';
124-
// childElement.tag === 'Switch';
127+
childElement.tag === 'li' ||
128+
childElement.tag === 'Switch' ||
129+
childElement.tag === 'Route';
125130

126131
if (childElement.tag === 'img') {
127132
return `${levelSpacer(level, 5)}<${childElement.tag} src="${activeLink}" ${elementTagDetails(childElement)}/>${levelSpacer(2, (3 + level))}`;
@@ -130,7 +135,9 @@ const generateUnformattedCode = (
130135
} else if (childElement.tag === 'input') {
131136
return `${levelSpacer(level, 5)}<${childElement.tag}${elementTagDetails(childElement)}></${childElement.tag}>${levelSpacer(2, (3 + level))}`;
132137
} 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}
134141
${tabSpacer(level)}${writeNestedElements(childElement.children, level + 1)}
135142
${tabSpacer(level - 1)}</${childElement.tag}>${levelSpacer(2, (3 + level))}`;
136143
} else if (childElement.tag !== 'separator'){
@@ -195,12 +202,14 @@ const generateUnformattedCode = (
195202

196203
const stateful = true;
197204
const classBased = false;
205+
let importReactRouter;
198206

199207
// create final component code. component code differs between classic react, next.js, gatsby.js
200208
// classic react code
201209
if (projectType === 'Classic React') {
202210
return `
203211
${stateful && !classBased ? `import React, {useState} from 'react';` : ''}
212+
${importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';`: ``}
204213
${classBased ? `import React, {Component} from 'react';` : ''}
205214
${!stateful && !classBased ? `import React from 'react';` : ''}
206215
${importsMapped}
@@ -209,7 +218,6 @@ const generateUnformattedCode = (
209218
? `class ${currentComponent.name} extends Component {`
210219
: `const ${currentComponent.name} = (props: any): JSX.Element => {`
211220
}
212-
213221
${
214222
stateful && !classBased
215223
? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE")${writeStateProps(currentComponent.useStateCodes)};
@@ -225,13 +233,18 @@ const generateUnformattedCode = (
225233
: ``
226234
}
227235
${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>
230243
<div className="${currentComponent.name}" style={props.style}>
231244
${writeNestedElements(enrichedChildren)}
232245
</div>
233-
);
234-
}
246+
</Router>
247+
);`}
235248
${classBased ? `}` : ``}
236249
export default ${currentComponent.name};
237250
`;

app/src/helperFunctions/renderChildren.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const renderChildren = (children: ChildElement[]) => {
3434
}
3535
// ommitted orderedlists, unorderedlists, and menus, ommitted li items as non-nestable types because they can be nested within.
3636
// child is a non-nestable type of HTML element (everything except for divs and forms)
37-
else if (type === 'HTML Element' && typeId !== 11 && typeId !== 1000 && typeId !== 2 && typeId !== 3 && typeId !== 14 && typeId !== 15 && typeId !== 16 && typeId != 17 && typeId != -1) {
37+
else if (type === 'HTML Element' && typeId !== 11 && typeId !== 1000 && typeId !== 2 && typeId !== 3 && typeId !== 14 && typeId !== 15 && typeId !== 16 && typeId !== 17 && typeId !== -1) {
3838
return (
3939
<DirectChildHTML
4040
childId={childId}

0 commit comments

Comments
 (0)