Skip to content

Commit 3f77a2a

Browse files
authored
Merge pull request #9 from oslabs-beta/feature/moreComps
Feature/more comps
2 parents 2e4028a + 325e004 commit 3f77a2a

File tree

11 files changed

+5048
-210
lines changed

11 files changed

+5048
-210
lines changed

app/src/components/left/ComponentDrag.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import makeStyles from '@mui/styles/makeStyles';
55
import { useSelector } from 'react-redux';
66
import ComponentPanelItem from '../right/ComponentPanelItem';
77

8-
98
const useStyles = makeStyles({
109
panelWrapper: {
1110
display: 'flex',
@@ -21,7 +20,7 @@ const useStyles = makeStyles({
2120
color: '#fff'
2221
},
2322
darkThemeFontColor: {
24-
color: '#fff'
23+
color: '#00008B,'
2524
}
2625
});
2726

@@ -70,4 +69,3 @@ const ComponentDrag = ({ isVisible, isThemeLight }): JSX.Element | null => {
7069
};
7170

7271
export default ComponentDrag;
73-

app/src/components/main/CanvasContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function CanvasContainer(props: CanvasContainerProps): JSX.Element {
3838
backgroundSize: '8px 8px',
3939
backgroundPosition: '-19px -19px',
4040
borderBottom: 'none',
41-
overflow: 'auto'
41+
overflow: 'auto',
4242
};
4343

4444
//containerRef references the container that will ultimately have the scroll functionality

app/src/components/main/DemoRender.tsx

Lines changed: 489 additions & 139 deletions
Large diffs are not rendered by default.

app/src/containers/CustomizationPanel.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { RootState } from '../redux/store';
4242
import { emitEvent } from '../helperFunctions/socket';
4343
import { Number } from 'mongoose';
4444

45+
4546
// Previously named rightContainer, Renamed to Customizationpanel this now hangs on BottomTabs
4647
// need to pass in props to use the useHistory feature of react router
4748
const CustomizationPanel = ({ isThemeLight }): JSX.Element => {

app/src/helperFunctions/generateCode.ts

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ const generateUnformattedCode = (
5353
// find the unique components that we need to import into this component file
5454
let imports: any = [];
5555
let muiImports = new Set();
56+
let muiStateAndEventHandlers = new Set();
5657
let providers: string = '';
5758
let context: string = '';
5859
let links: boolean = false;
5960
let images: boolean = false;
6061
const isRoot = rootComponents.includes(componentId);
6162
let importReactRouter = false;
6263

63-
// returns an array of objects which may include components, html elements, and/or route links
64+
// returns an array of objects which may include components, html elements, MaterialUI components, and/or route links
6465
const getEnrichedChildren = (currentComponent) => {
6566
const enrichedChildren = [];
6667

@@ -125,14 +126,66 @@ const generateUnformattedCode = (
125126
if (muiComponent) {
126127
// Recursively process MUI components that can have children
127128
if (
128-
['mui button', 'card', 'typography', 'textfield'].includes(
129-
muiComponent.tag
130-
)
129+
[
130+
'autocomplete',
131+
'mui button',
132+
'btn group',
133+
'checkbox',
134+
'fab',
135+
'radio group',
136+
'rating',
137+
'select',
138+
'slider',
139+
'switch',
140+
'textfield',
141+
'transfer-list',
142+
'toggle-button',
143+
'avatar',
144+
'badge',
145+
'chip',
146+
'list',
147+
'table',
148+
'tooltip',
149+
'typography',
150+
'alert',
151+
'backdrop',
152+
'dialog',
153+
'progress',
154+
'skeleton',
155+
'snackbar',
156+
'accordion',
157+
'appbar',
158+
'card',
159+
'paper',
160+
'bottomNavigation',
161+
'breadcrumbs',
162+
'drawer',
163+
'link',
164+
'menu',
165+
'pagination',
166+
'speedDial',
167+
'stepper',
168+
'tabs',
169+
'box',
170+
'container',
171+
'grid',
172+
'stack',
173+
'image-list',
174+
'modal',
175+
'popover',
176+
'popper',
177+
'transition'
178+
].includes(muiComponent.tag)
131179
) {
132180
newChild.children = getEnrichedChildren({
133181
children: child.children
134182
});
135183
collectMUIImports(child, MUITypes, muiImports);
184+
collectStateAndEventHandlers(
185+
child,
186+
MUITypes,
187+
muiStateAndEventHandlers
188+
);
136189
}
137190
}
138191
break;
@@ -347,7 +400,7 @@ const generateUnformattedCode = (
347400
].join('\n');
348401
}
349402

350-
function modifyAndIndentJsx(jsxAry, newProps, childId, name) {
403+
function modifyAndIndentJsx(jsxAry, newProps, childId, name, key) {
351404
// Define a regular expression to match the start tag of the specified child element
352405
const tagRegExp = new RegExp(`^<${name}(\\s|>)`);
353406

@@ -370,8 +423,15 @@ const generateUnformattedCode = (
370423
)} ${childId}${trimmedLine.substring(insertIndex)}`;
371424
// Adjust insertIndex for the next insertion
372425
insertIndex += childId.length + 1;
426+
} else if (trimmedLine.includes(`id=`) && childId) {
427+
// Define the insertion point for "{key} " right after `id="`
428+
let idInsertIndex = trimmedLine.indexOf(`id="`) + 4;
429+
// Insert "{key} " at the identified position within the existing id value
430+
line = `${trimmedLine.substring(
431+
0,
432+
idInsertIndex
433+
)}${key} ${trimmedLine.substring(idInsertIndex)}`;
373434
}
374-
375435
// Insert newProps at the updated insertion index
376436
if (newProps) {
377437
line = `${line.substring(
@@ -413,6 +473,7 @@ const generateUnformattedCode = (
413473
const muiGenerator = (child, level = 0) => {
414474
let childId = '';
415475
let passedInPropsString = '';
476+
let key = 0;
416477

417478
const MUIComp = MUITypes.find((el) => el.tag === child.name);
418479
const MUIName = MUIComp.name;
@@ -427,6 +488,7 @@ const generateUnformattedCode = (
427488

428489
if (child.childId) {
429490
childId = `id="${+child.childId}"`;
491+
key = +child.childId;
430492
}
431493

432494
// Indent the JSX generated for MUI components based on the provided level
@@ -437,7 +499,8 @@ const generateUnformattedCode = (
437499
indentedJSX,
438500
passedInPropsString,
439501
childId,
440-
MUIName
502+
MUIName,
503+
key
441504
);
442505

443506
// Handle nested components, if any
@@ -521,6 +584,46 @@ const generateUnformattedCode = (
521584
return Array.from(muiImports).join('\n');
522585
}
523586

587+
// Function to collect state and event handler snippets from components
588+
function collectStateAndEventHandlers(
589+
component,
590+
MUITypes,
591+
handlersCollection
592+
) {
593+
console.log('collectStateAndEventHandlers invoked');
594+
if (component.type === 'MUI Component') {
595+
console.log('collectStateAndEventHandlers MUI check');
596+
const muiComponent = MUITypes.find((m) => m.id === component.typeId);
597+
598+
console.log('muiComponent found:', JSON.stringify(muiComponent)); // Check what muiComponent is found
599+
console.log(
600+
'StateAndEventHandlers:',
601+
muiComponent?.stateAndEventHandlers
602+
); // Direct check
603+
604+
if (muiComponent && Array.isArray(muiComponent.stateAndEventHandlers)) {
605+
console.log('collectStateAndEventHandlers hasState');
606+
muiComponent.stateAndEventHandlers.forEach((handlerSnippet) => {
607+
handlersCollection.add(handlerSnippet);
608+
});
609+
} else {
610+
console.log('No stateAndEventHandlers found or not an array');
611+
}
612+
}
613+
614+
// Recursively collect handlers from child components if they exist
615+
if (component.children) {
616+
component.children.forEach((child) =>
617+
collectStateAndEventHandlers(child, MUITypes, handlersCollection)
618+
);
619+
}
620+
}
621+
622+
// Function to generate code for state and event handlers
623+
function generateStateAndEventHandlerCode(handlersCollection) {
624+
return Array.from(handlersCollection).join('\n');
625+
}
626+
524627
const writeNestedElements = (enrichedChildren, level = 0) => {
525628
return enrichedChildren.flatMap((child) => {
526629
if (child.type === 'Component') {
@@ -660,6 +763,10 @@ const generateUnformattedCode = (
660763
return importStr;
661764
};
662765
const muiImportStatements = generateMUIImportStatements(muiImports);
766+
const stateAndEventHandlers = generateStateAndEventHandlerCode(
767+
muiStateAndEventHandlers
768+
);
769+
console.log('stateAndEventHandlers', stateAndEventHandlers);
663770
let generatedCode =
664771
"import React, { useState, useEffect, useContext} from 'react';\n\n";
665772
generatedCode += currComponent.name === 'App' ? contextImports : '';
@@ -671,6 +778,7 @@ const generateUnformattedCode = (
671778
generatedCode += muiImportStatements ? `${muiImportStatements}\n\n` : '';
672779
// below is the return statement of the codepreview
673780
generatedCode += `const ${currComponent.name} = (props) => {\n`;
781+
generatedCode += stateAndEventHandlers ? `${stateAndEventHandlers}` : '';
674782
generatedCode += writeStateProps(currComponent.useStateCodes)
675783
? `\t${writeStateProps(currComponent.useStateCodes)}\n`
676784
: '';

app/src/helperFunctions/renderChildren.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,46 @@ const renderChildren = (children: ChildElement[]) => {
122122
passedInProps={[]}
123123
/>
124124
);
125-
} else if (type === 'MUI Component' && typeId === 31) {
125+
} else if (
126+
type === 'MUI Component' &&
127+
(typeId === 21 ||
128+
typeId === 22 ||
129+
typeId === 24 ||
130+
typeId === 26 ||
131+
typeId === 27 ||
132+
typeId === 28 ||
133+
typeId === 29 ||
134+
typeId === 30 ||
135+
typeId === 31 ||
136+
typeId === 33 ||
137+
typeId === 32 ||
138+
typeId === 34 ||
139+
typeId === 35 ||
140+
typeId === 36 ||
141+
typeId === 37 ||
142+
typeId === 40 ||
143+
typeId === 41 ||
144+
typeId === 42 ||
145+
typeId === 44 ||
146+
typeId === 46 ||
147+
typeId === 47 ||
148+
typeId === 51 ||
149+
typeId === 53 ||
150+
typeId === 54 ||
151+
typeId === 55 ||
152+
typeId === 56 ||
153+
typeId === 57 ||
154+
typeId === 58 ||
155+
typeId === 59 ||
156+
typeId === 60 ||
157+
typeId === 61 ||
158+
typeId === 62 ||
159+
typeId === 68 ||
160+
typeId === 69 ||
161+
typeId === 73 ||
162+
typeId === 75 ||
163+
typeId === 76)
164+
) {
126165
return (
127166
<DirectChildMUI
128167
childId={childId}
@@ -141,7 +180,18 @@ const renderChildren = (children: ChildElement[]) => {
141180
// child is a nestable type of HTML element (divs, forms, OrderedLists, UnorderedLists, menus)
142181
else if (
143182
type === 'MUI Component' &&
144-
(typeId === 21 || typeId === 41 || typeId === 51)
183+
(typeId === 23 ||
184+
typeId === 25 ||
185+
typeId === 43 ||
186+
typeId === 45 ||
187+
typeId === 48 ||
188+
typeId === 49 ||
189+
typeId === 50 ||
190+
typeId === 52 ||
191+
typeId === 63 ||
192+
typeId === 64 ||
193+
typeId === 65 ||
194+
typeId === 79)
145195
) {
146196
return (
147197
<DirectChildMUINestable

app/src/interfaces/Interfaces.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ export interface MUIType {
114114
icon?: any;
115115
framework: string;
116116
nestable: boolean;
117-
imports: any[];
118-
propOptions: any[];
119-
defaultProps: any[];
120-
jsx: any[];
117+
stateAndEventHandlers: string[];
118+
imports: string[];
119+
propOptions: string[];
120+
defaultProps: string[];
121+
jsx: string[];
122+
componentData: object;
121123
children: [];
122124
}
123125
export interface DragItem extends DragObjectWithType {

0 commit comments

Comments
 (0)