Skip to content

Commit 59c7361

Browse files
committed
application can now handle arrays for both text and link inputs from state. updates were to componentBuilder in DemoRender.tsx.
co-authored-by: Crys Lim [email protected]
1 parent ae53bac commit 59c7361

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

app/src/components/main/DemoRender.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,33 @@ const DemoRender = (props): JSX.Element => {
2828
const elementType = element.name;
2929
const childId = element.childId;
3030
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;
3232
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;
3434
let renderedChildren;
3535
if (elementType !== 'input' && elementType !== 'img' && element.children.length > 0) {
3636
renderedChildren = componentBuilder(element.children);
3737
}
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++) {
4153
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;
4356
componentsToRender.push(componentBuilder([elementCopy], key++));
44-
});
57+
}
4558
continue;
4659
}
4760

0 commit comments

Comments
 (0)