From 16ba3f529e45004516e188f47a720cf66f5c23af Mon Sep 17 00:00:00 2001 From: "Jason.Chuah" Date: Fri, 7 Jul 2023 22:49:03 +0800 Subject: [PATCH] enhancement: add support for checkbox --- src/css/brutusin-json-forms.css | 2 +- src/js/brutusin-json-forms.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/css/brutusin-json-forms.css b/src/css/brutusin-json-forms.css index 7477bc2..b5a53f3 100644 --- a/src/css/brutusin-json-forms.css +++ b/src/css/brutusin-json-forms.css @@ -65,7 +65,7 @@ form.brutusin-form table, form.brutusin-form input, form.brutusin-form select, f min-width: 80px; } -form.brutusin-form input[type=radio] { +form.brutusin-form input[type=radio], form.brutusin-form input[type=checkbox] { margin: 0 15px 0 10px; } diff --git a/src/js/brutusin-json-forms.js b/src/js/brutusin-json-forms.js index 1f99f9c..cf0356a 100644 --- a/src/js/brutusin-json-forms.js +++ b/src/js/brutusin-json-forms.js @@ -386,6 +386,28 @@ if (typeof brutusin === "undefined") { appendChild(input, radioInput, s); } } + else if (s.format === "checkbox") { + input = document.createElement("div"); + for (var i = 0; i < s.enum.length; i++) { + checkbox = document.createElement("input"); + checkbox.type = "checkbox"; + checkbox.name = s.enum[i]; + checkbox.value = s.enum[i]; + + var label = document.createElement("label"); + label.htmlFor = s.enum[i]; + var labelText = document.createTextNode(s.enum[i]); + appendChild(label, labelText); + if (value && s.enum[i] === value) { + checkbox.checked = true; + } + if (s.readOnly) { + checkbox.disabled = true; + } + appendChild(input, label); + appendChild(input, checkbox, s); + } + } else if (s.required) { input = document.createElement("input"); input.type = "checkbox";