Skip to content

Commit 5e0aa61

Browse files
Added container component for field with object type
Signed-off-by: Natalia Luzuriaga <[email protected]>
1 parent c0c7d3b commit 5e0aa61

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

js/generateFormComponents.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ function transformArrayToOptions(arr) {
2323
}
2424

2525
function determineType(field) {
26-
if (field.type === "array") {
26+
if (field.type === "object") {
27+
return "container";
28+
} else if (field.type === "array") {
2729
// Multi-select
2830
if (field.items.hasOwnProperty("enum")) {
2931
return "selectboxes";
@@ -148,7 +150,7 @@ function createComponent(fieldName, fieldObject) {
148150
"maxDate": null
149151
},
150152
description: fieldObject["description"]
151-
}
153+
};
152154
case "select-boolean":
153155
return {
154156
"label": fieldName,
@@ -171,7 +173,17 @@ function createComponent(fieldName, fieldObject) {
171173
"type": "select",
172174
"input": true,
173175
description: fieldObject["description"]
174-
}
176+
};
177+
case "container":
178+
return {
179+
label: fieldName,
180+
tableView: false,
181+
validateWhenHidden: false,
182+
key: fieldName,
183+
type: "container",
184+
input: true,
185+
components: []
186+
};
175187
default:
176188
break;
177189
}
@@ -182,6 +194,28 @@ function createFormHeading(title, description) {
182194
container.innerHTML = `<h1>${title}</h1>\n<h2>${description}</h2>`;
183195
}
184196

197+
function createAllComponents(schema, prefix = ""){
198+
let components = [];
199+
200+
console.log("checking schema", schema);
201+
202+
if (schema.type === "object" && schema.properties) {
203+
for (const [key, value] of Object.entries(schema.properties)) {
204+
205+
const fullKey = prefix ? `${prefix}.${key}` : key;
206+
207+
var fieldComponent = createComponent(key, value);
208+
209+
if (fieldComponent.type === "container") {
210+
fieldComponent.components = createAllComponents(value, fullKey);
211+
}
212+
components.push(fieldComponent);
213+
}
214+
}
215+
216+
return components;
217+
}
218+
185219
// Iterates through each json field and creates component array for Form.io
186220
async function createFormComponents() {
187221
let components = [];
@@ -193,14 +227,7 @@ async function createFormComponents() {
193227

194228
createFormHeading(jsonData["title"], jsonData["description"]);
195229

196-
formFields = jsonData["properties"];
197-
console.log("form Fields:", formFields);
198-
199-
for (const key in formFields) {
200-
console.log(`${key}:`, formFields[key]);
201-
var fieldComponent = createComponent(key, formFields[key]);
202-
components.push(fieldComponent);
203-
}
230+
components = createAllComponents(jsonData);
204231

205232
// Add submit button to form
206233
components.push({

schemas/schema-0.0.0.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
},
183183
"date": {
184184
"type": "object",
185+
"description": "A date object describing the release",
185186
"properties": {
186187
"created": {
187188
"type": "string",
@@ -209,6 +210,7 @@
209210
},
210211
"contact": {
211212
"type": "object",
213+
"description": "Point of contact for the release",
212214
"properties": {
213215
"email": {
214216
"type": "string",

0 commit comments

Comments
 (0)