Skip to content

Commit 1cc86c6

Browse files
committed
add optional booleans support #50
1 parent bbf88ce commit 1cc86c6

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

src/js/brutusin-json-forms.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,39 @@ if (typeof brutusin === "undefined") {
318318
renderers["boolean"] = function (container, id, parentObject, propertyProvider, value) {
319319
var schemaId = getSchemaId(id);
320320
var s = getSchema(schemaId);
321-
var input = document.createElement("input");
322-
input.type = "checkbox";
323-
input.schema = schemaId;
321+
var input;
322+
if (s.required) {
323+
input = document.createElement("input");
324+
input.type = "checkbox";
325+
if (value === true) {
326+
input.checked = true;
327+
}
328+
} else {
329+
input = document.createElement("select");
330+
var emptyOption = document.createElement("option");
331+
var textEmpty = document.createTextNode("");
332+
textEmpty.value = "";
333+
appendChild(emptyOption, textEmpty, s);
334+
appendChild(input, emptyOption, s);
335+
336+
var optionTrue = document.createElement("option");
337+
var textTrue = document.createTextNode("true");
338+
textTrue.value = "true";
339+
appendChild(optionTrue, textTrue, s);
340+
appendChild(input, optionTrue, s);
341+
342+
var optionFalse = document.createElement("option");
343+
var textFalse = document.createTextNode("false");
344+
textFalse.value = "false";
345+
appendChild(optionFalse, textFalse, s);
346+
appendChild(input, optionFalse, s);
347+
348+
if (value === true) {
349+
input.selectedIndex = 1;
350+
} else if (value === false) {
351+
input.selectedIndex = 2;
352+
}
353+
}
324354
input.onchange = function () {
325355
if (parentObject) {
326356
parentObject[propertyProvider.getValue()] = getValue(s, input);
@@ -329,9 +359,7 @@ if (typeof brutusin === "undefined") {
329359
}
330360
onDependencyChanged(schemaId, input);
331361
};
332-
if (value === true) {
333-
input.checked = true;
334-
}
362+
input.schema = schemaId;
335363
input.id = getInputId();
336364
inputCounter++;
337365
if (s.description) {
@@ -1093,9 +1121,19 @@ if (typeof brutusin === "undefined") {
10931121
value = null;
10941122
}
10951123
} else if (schema.type === "boolean") {
1096-
value = input.checked;
1097-
if (!value) {
1098-
value = false;
1124+
if (input.tagName.toLowerCase() === "input") {
1125+
value = input.checked;
1126+
if (!value) {
1127+
value = false;
1128+
}
1129+
} else if (input.tagName.toLowerCase() === "select") {
1130+
if (input.value === "true") {
1131+
value = true;
1132+
} else if (input.value === "false") {
1133+
value = false;
1134+
} else {
1135+
value = null;
1136+
}
10991137
}
11001138
} else if (schema.type === "any") {
11011139
if (value) {

0 commit comments

Comments
 (0)