@@ -53,14 +53,15 @@ const generateUnformattedCode = (
53
53
// find the unique components that we need to import into this component file
54
54
let imports : any = [ ] ;
55
55
let muiImports = new Set ( ) ;
56
+ let muiStateAndEventHandlers = new Set ( ) ;
56
57
let providers : string = '' ;
57
58
let context : string = '' ;
58
59
let links : boolean = false ;
59
60
let images : boolean = false ;
60
61
const isRoot = rootComponents . includes ( componentId ) ;
61
62
let importReactRouter = false ;
62
63
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
64
65
const getEnrichedChildren = ( currentComponent ) => {
65
66
const enrichedChildren = [ ] ;
66
67
@@ -125,14 +126,66 @@ const generateUnformattedCode = (
125
126
if ( muiComponent ) {
126
127
// Recursively process MUI components that can have children
127
128
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 )
131
179
) {
132
180
newChild . children = getEnrichedChildren ( {
133
181
children : child . children
134
182
} ) ;
135
183
collectMUIImports ( child , MUITypes , muiImports ) ;
184
+ collectStateAndEventHandlers (
185
+ child ,
186
+ MUITypes ,
187
+ muiStateAndEventHandlers
188
+ ) ;
136
189
}
137
190
}
138
191
break ;
@@ -347,7 +400,7 @@ const generateUnformattedCode = (
347
400
] . join ( '\n' ) ;
348
401
}
349
402
350
- function modifyAndIndentJsx ( jsxAry , newProps , childId , name ) {
403
+ function modifyAndIndentJsx ( jsxAry , newProps , childId , name , key ) {
351
404
// Define a regular expression to match the start tag of the specified child element
352
405
const tagRegExp = new RegExp ( `^<${ name } (\\s|>)` ) ;
353
406
@@ -370,8 +423,15 @@ const generateUnformattedCode = (
370
423
) } ${ childId } ${ trimmedLine . substring ( insertIndex ) } `;
371
424
// Adjust insertIndex for the next insertion
372
425
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 ) } `;
373
434
}
374
-
375
435
// Insert newProps at the updated insertion index
376
436
if ( newProps ) {
377
437
line = `${ line . substring (
@@ -413,6 +473,7 @@ const generateUnformattedCode = (
413
473
const muiGenerator = ( child , level = 0 ) => {
414
474
let childId = '' ;
415
475
let passedInPropsString = '' ;
476
+ let key = 0 ;
416
477
417
478
const MUIComp = MUITypes . find ( ( el ) => el . tag === child . name ) ;
418
479
const MUIName = MUIComp . name ;
@@ -427,6 +488,7 @@ const generateUnformattedCode = (
427
488
428
489
if ( child . childId ) {
429
490
childId = `id="${ + child . childId } "` ;
491
+ key = + child . childId ;
430
492
}
431
493
432
494
// Indent the JSX generated for MUI components based on the provided level
@@ -437,7 +499,8 @@ const generateUnformattedCode = (
437
499
indentedJSX ,
438
500
passedInPropsString ,
439
501
childId ,
440
- MUIName
502
+ MUIName ,
503
+ key
441
504
) ;
442
505
443
506
// Handle nested components, if any
@@ -521,6 +584,46 @@ const generateUnformattedCode = (
521
584
return Array . from ( muiImports ) . join ( '\n' ) ;
522
585
}
523
586
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
+
524
627
const writeNestedElements = ( enrichedChildren , level = 0 ) => {
525
628
return enrichedChildren . flatMap ( ( child ) => {
526
629
if ( child . type === 'Component' ) {
@@ -660,6 +763,10 @@ const generateUnformattedCode = (
660
763
return importStr ;
661
764
} ;
662
765
const muiImportStatements = generateMUIImportStatements ( muiImports ) ;
766
+ const stateAndEventHandlers = generateStateAndEventHandlerCode (
767
+ muiStateAndEventHandlers
768
+ ) ;
769
+ console . log ( 'stateAndEventHandlers' , stateAndEventHandlers ) ;
663
770
let generatedCode =
664
771
"import React, { useState, useEffect, useContext} from 'react';\n\n" ;
665
772
generatedCode += currComponent . name === 'App' ? contextImports : '' ;
@@ -671,6 +778,7 @@ const generateUnformattedCode = (
671
778
generatedCode += muiImportStatements ? `${ muiImportStatements } \n\n` : '' ;
672
779
// below is the return statement of the codepreview
673
780
generatedCode += `const ${ currComponent . name } = (props) => {\n` ;
781
+ generatedCode += stateAndEventHandlers ? `${ stateAndEventHandlers } ` : '' ;
674
782
generatedCode += writeStateProps ( currComponent . useStateCodes )
675
783
? `\t${ writeStateProps ( currComponent . useStateCodes ) } \n`
676
784
: '' ;
0 commit comments