function checkbox(config = {}) {
let { value: formValue, title, description, submit, options } = Array.isArray(
config
)
? { options: config }
: config;
options = options.map(o =>
typeof o === "string" ? { value: o, label: o } : o
);
const form = input({
type: "checkbox",
title,
description,
submit,
getValue: input => {
if (input.length)
return Array.prototype.filter
.call(input, i => i.checked)
.map(i => i.value);
return input.checked ? input.value : false;
},
form: html`
<form>
${options.map(({ value, label, color }) => {
const colorBlock = html`<span style="display: inline-block; background-color: ${color}; width: 1em; height: 1em; border-radius: 1em"></span>`;
const input = html`<input type=checkbox name=input ${
(formValue || []).indexOf(value) > -1 ? "checked" : ""
} style="vertical-align: baseline;" />`;
input.setAttribute("value", value);
const tag = html`<label style="display: inline-block; margin: 5px 10px 3px 0; font-size: 0.85em;">
${input}
${colorBlock}
${label}
</label>`;
return tag;
})}
</form>
`
});
form.output.remove();
return form;
}