@@ -202,7 +202,7 @@ const DemoRender = (): JSX.Element => {
202
202
<style id="mui-styles"></style>
203
203
</head>
204
204
<body>
205
- <div id="app"></div>
205
+ <div id="app";" ></div>
206
206
<script>
207
207
const top100Films = [
208
208
{ label: 'The Shawshank Redemption', year: 1994 },
@@ -266,53 +266,68 @@ const DemoRender = (): JSX.Element => {
266
266
FormControlLabel: MaterialUI?.FormControlLabel,
267
267
FormControl: MaterialUI?.FormControl,
268
268
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,
270
274
};
271
275
272
276
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
+ }
274
287
};
275
288
276
289
const createComponentFromData = (data) => {
277
- console.log('data', data);
278
290
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
+
280
293
const processChildren = (child) => {
281
294
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));
286
297
}
298
+ return specialComponents[child] ? specialComponents[child]() : child;
287
299
} else if (typeof child === 'object' && child !== null) {
288
300
return createComponentFromData(child);
289
- } else {
290
- return null;
291
301
}
302
+ return null;
292
303
};
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
+
305
314
window.addEventListener('message', (event) => {
306
315
console.log('event', event);
307
316
const dataArr = event.data.replace(/}{/g, '},,{').replace(/}</g, '},,<').replace(/>{/g, '>,,{').split(',,');
308
317
console.log('dataArr', dataArr);
309
318
const container = document.getElementById('app');
310
319
container.innerHTML = '';
311
320
dataArr.forEach(segment => {
321
+ console.log('segment', segment)
312
322
if(segment.trim().startsWith('{') && segment.trim().endsWith('}')) {
313
323
try {
314
324
const jsonData = JSON.parse(segment);
315
325
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
+
316
331
const componentContainer = document.createElement('div');
317
332
container.appendChild(componentContainer);
318
333
const component = createComponentFromData(jsonData);
@@ -401,18 +416,22 @@ const DemoRender = (): JSX.Element => {
401
416
const innerText = element . attributes . compText ;
402
417
const classRender = element . attributes . cssClasses ;
403
418
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
+
404
429
let renderedChildren =
405
- element . children && element . children . length > 0
406
- ? componentBuilder2 ( element . children , ++ key )
430
+ allChildren . length > 0
431
+ ? componentBuilder2 ( allChildren , ++ key )
407
432
: 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' ) {
416
435
const baseData = MUITypes . find (
417
436
( m ) => m . tag === elementType
418
437
) . componentData ;
@@ -421,8 +440,9 @@ const DemoRender = (): JSX.Element => {
421
440
const componentData = {
422
441
...baseData ,
423
442
props : {
424
- ...( baseData . props || { } ) ,
425
- key : ++ key
443
+ ...baseData . props ,
444
+ key : ++ key ,
445
+ children : renderedChildren
426
446
}
427
447
} ;
428
448
componentsToRender . push ( JSON . stringify ( componentData ) ) ;
@@ -480,6 +500,7 @@ const DemoRender = (): JSX.Element => {
480
500
}
481
501
key ++ ;
482
502
}
503
+ console . log ( 'componentsToRender' , componentsToRender ) ;
483
504
return componentsToRender ;
484
505
} ;
485
506
//initializes our 'code' which will be whats actually in the iframe in the demo render
@@ -506,9 +527,9 @@ const DemoRender = (): JSX.Element => {
506
527
}
507
528
} ) ;
508
529
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
512
533
useEffect ( ( ) => {
513
534
//load the current state code when the iframe is loaded and when code changes
514
535
iframe . current . addEventListener ( 'load' , ( ) => {
0 commit comments