@@ -28,20 +28,33 @@ const DemoRender = (props): JSX.Element => {
28
28
const elementType = element . name ;
29
29
const childId = element . childId ;
30
30
const elementStyle = element . style ;
31
- const innerText = element . attributes . compText ; //typeof innertext is an obj if using arr or obj from state
31
+ const innerText = element . attributes . compText ;
32
32
const classRender = element . attributes . cssClasses ;
33
- const activeLink = element . attributes . compLink ; //typeof activelink is an obj if using arr or obj from state
33
+ const activeLink = element . attributes . compLink ;
34
34
let renderedChildren ;
35
35
if ( elementType !== 'input' && elementType !== 'img' && element . children . length > 0 ) {
36
36
renderedChildren = componentBuilder ( element . children ) ;
37
37
}
38
- // check if array was used -> write a forEach / for
39
- if ( Array . isArray ( innerText ) ) {
40
- innerText . forEach ( el => {
38
+
39
+ // check if innerText or activeLink is an array
40
+ const innerTextIsArray = Array . isArray ( innerText ) ;
41
+ const activeLinkIsArray = Array . isArray ( activeLink ) ;
42
+
43
+ if ( innerTextIsArray || activeLinkIsArray ) {
44
+ // determine how many elements to iterate over
45
+ let n ;
46
+ if ( innerTextIsArray && activeLinkIsArray ) {
47
+ n = Math . min ( innerText . length , activeLink . length ) ;
48
+ } else {
49
+ n = innerTextIsArray ? innerText . length : activeLink . length ;
50
+ }
51
+ // make a new element for each value of the array
52
+ for ( let i = 0 ; i < n ; i ++ ) {
41
53
const elementCopy = JSON . parse ( JSON . stringify ( element ) ) ;
42
- elementCopy . attributes . compText = el ;
54
+ elementCopy . attributes . compText = innerTextIsArray ? innerText [ i ] : innerText ;
55
+ elementCopy . attributes . compLink = activeLinkIsArray ? activeLink [ i ] : activeLink ;
43
56
componentsToRender . push ( componentBuilder ( [ elementCopy ] , key ++ ) ) ;
44
- } ) ;
57
+ }
45
58
continue ;
46
59
}
47
60
0 commit comments