Skip to content

Commit 853cb6a

Browse files
committed
added more components and now MUI components nest one level with other MUI components
1 parent 8324c0a commit 853cb6a

File tree

4 files changed

+264
-51
lines changed

4 files changed

+264
-51
lines changed

app/src/components/main/DemoRender.tsx

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ const DemoRender = (): JSX.Element => {
202202
<style id="mui-styles"></style>
203203
</head>
204204
<body>
205-
<div id="app"></div>
205+
<div id="app";"></div>
206206
<script>
207207
const top100Films = [
208208
{ label: 'The Shawshank Redemption', year: 1994 },
@@ -266,53 +266,68 @@ const DemoRender = (): JSX.Element => {
266266
FormControlLabel: MaterialUI?.FormControlLabel,
267267
FormControl: MaterialUI?.FormControl,
268268
FormLabel: MaterialUI?.FormLabel,
269-
Rating: MaterialUI?.Rating
269+
Rating: MaterialUI?.Rating,
270+
MenuItem: MaterialUI?.MenuItem,
271+
Select: MaterialUI?.Select,
272+
InputLabel: MaterialUI?.InputLabel,
273+
Slider: MaterialUI?.Slider,
270274
};
271275
272276
const specialComponents = {
273-
'br': React.createElement('br', {})
277+
'br': () => React.createElement('br', {})
278+
};
279+
280+
const isJsonString = (str) => {
281+
try {
282+
JSON.parse(str);
283+
return true;
284+
} catch (e) {
285+
return false;
286+
}
274287
};
275288
276289
const createComponentFromData = (data) => {
277-
console.log('data', data);
278290
const { type, props, children } = data;
279-
const Component = componentMap[type] || 'div';
291+
const Component = componentMap[type] || 'div'; // Default to div if component is not found
292+
280293
const processChildren = (child) => {
281294
if (typeof child === 'string') {
282-
if (specialComponents[child]) {
283-
return specialComponents[child];
284-
} else {
285-
return child;
295+
if (isJsonString(child)) {
296+
return createComponentFromData(JSON.parse(child));
286297
}
298+
return specialComponents[child] ? specialComponents[child]() : child;
287299
} else if (typeof child === 'object' && child !== null) {
288300
return createComponentFromData(child);
289-
} else {
290-
return null;
291301
}
302+
return null;
292303
};
293-
294-
if (typeof children === 'string' || children === null) {
295-
return React.createElement(Component, props, children);
296-
} else if (Array.isArray(children)) {
297-
const processedChildren = children.map(processChildren);
298-
return React.createElement(Component, props, ...processedChildren);
299-
} else if (typeof children === 'object') {
300-
return React.createElement(Component, props, createComponentFromData(children));
301-
}
302-
return React.createElement(Component, props);
303-
};
304-
304+
305+
// Handling single and multiple children uniformly
306+
const processedChildren = Array.isArray(children) ?
307+
children.map(processChildren) :
308+
[processChildren(children)]; // Wrap non-array children in an array
309+
310+
// Spreading children into createElement function
311+
return React.createElement(Component, props, ...processedChildren);
312+
};
313+
305314
window.addEventListener('message', (event) => {
306315
console.log('event', event);
307316
const dataArr = event.data.replace(/}{/g, '},,{').replace(/}</g, '},,<').replace(/>{/g, '>,,{').split(',,');
308317
console.log('dataArr', dataArr);
309318
const container = document.getElementById('app');
310319
container.innerHTML = '';
311320
dataArr.forEach(segment => {
321+
console.log('segment', segment)
312322
if(segment.trim().startsWith('{') && segment.trim().endsWith('}')) {
313323
try {
314324
const jsonData = JSON.parse(segment);
315325
console.log('jsonData', jsonData);
326+
if (jsonData.props.children) {
327+
console.log('jsonData.props.children', jsonData.props.children);
328+
jsonData.children = [...jsonData.children, ...jsonData.props.children];
329+
}
330+
316331
const componentContainer = document.createElement('div');
317332
container.appendChild(componentContainer);
318333
const component = createComponentFromData(jsonData);
@@ -401,18 +416,22 @@ const DemoRender = (): JSX.Element => {
401416
const innerText = element.attributes.compText;
402417
const classRender = element.attributes.cssClasses;
403418
const activeLink = element.attributes.compLink;
419+
420+
let allChildren = [];
421+
422+
if (element.componentData && element.componentData.children) {
423+
allChildren = [...element.componentData.children];
424+
}
425+
if (element.children && element.children.length > 0) {
426+
allChildren = [...allChildren, ...element.children];
427+
}
428+
404429
let renderedChildren =
405-
element.children && element.children.length > 0
406-
? componentBuilder2(element.children, ++key)
430+
allChildren.length > 0
431+
? componentBuilder2(allChildren, ++key)
407432
: undefined;
408-
const shouldSerialize = element.type === 'MUI Component' ? true : false;
409-
if (shouldSerialize) {
410-
renderedChildren =
411-
element.componentData && element.componentData.children
412-
? componentBuilder2([element.componentData], key + 1) // If children are in componentData
413-
: element.children && element.children.length > 0
414-
? componentBuilder2(element.children, key + 1) // Standard children handling
415-
: undefined; // No children to render
433+
434+
if (element.type === 'MUI Component') {
416435
const baseData = MUITypes.find(
417436
(m) => m.tag === elementType
418437
).componentData;
@@ -421,8 +440,9 @@ const DemoRender = (): JSX.Element => {
421440
const componentData = {
422441
...baseData,
423442
props: {
424-
...(baseData.props || {}),
425-
key: ++key
443+
...baseData.props,
444+
key: ++key,
445+
children: renderedChildren
426446
}
427447
};
428448
componentsToRender.push(JSON.stringify(componentData));
@@ -480,6 +500,7 @@ const DemoRender = (): JSX.Element => {
480500
}
481501
key++;
482502
}
503+
console.log('componentsToRender', componentsToRender);
483504
return componentsToRender;
484505
};
485506
//initializes our 'code' which will be whats actually in the iframe in the demo render
@@ -506,9 +527,9 @@ const DemoRender = (): JSX.Element => {
506527
}
507528
});
508529

509-
//writes our stylesheet from state to the code
510-
// code += `<style>${stylesheet}</style>`;
511-
//adds the code into the iframe
530+
// writes our stylesheet from state to the code
531+
code += `<style>${stylesheet}</style>`;
532+
// adds the code into the iframe
512533
useEffect(() => {
513534
//load the current state code when the iframe is loaded and when code changes
514535
iframe.current.addEventListener('load', () => {

app/src/helperFunctions/generateCode.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const generateUnformattedCode = (
6060
const isRoot = rootComponents.includes(componentId);
6161
let importReactRouter = false;
6262

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

@@ -135,7 +135,9 @@ const generateUnformattedCode = (
135135
'checkbox',
136136
'fab',
137137
'radio group',
138-
'rating'
138+
'rating',
139+
'select',
140+
'slider'
139141
].includes(muiComponent.tag)
140142
) {
141143
newChild.children = getEnrichedChildren({

app/src/helperFunctions/renderChildren.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ const renderChildren = (children: ChildElement[]) => {
124124
);
125125
} else if (
126126
type === 'MUI Component' &&
127-
(typeId === 31 || typeId === 61 || typeId === 81 || typeId === 111)
127+
(typeId === 31 ||
128+
typeId === 61 ||
129+
typeId === 81 ||
130+
typeId === 111 ||
131+
typeId === 121)
128132
) {
129133
return (
130134
<DirectChildMUI

0 commit comments

Comments
 (0)