Published
Edited
Nov 22, 2018
Fork of Inputs
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function select(config = {}) {
let {
value: formValue,
title,
description,
submit,
multiple,
size,
className="custom-select",
options
} = config;
if (Array.isArray(config)) options = config;
options = options.map(
o => (typeof o === "object" ? o : { value: o, label: o })
);
const form = input({
type: "select",
title,
description,
submit,
getValue: input => {
const selected = Array.prototype.filter
.call(input.options, i => i.selected)
.map(i => i.value);
return multiple ? selected : selected[0];
},
form: html`
<form>
<select name="input" ${
multiple ? `multiple size="${size || options.length}"` : ""
} class="${className}">
${options.map(
({ value, label }) => `
<option value="${value}" ${
value === formValue ? "selected" : ""
}>${label}</option>
`
)}
</select>
</form>
`
});
form.output.remove();
return form;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function checkbox(config = {}) {
let {
value: formValue,
title,
description,
submit,
options,
className="custom-control-input",
} = config;
if (Array.isArray(config)) options = config;
options = options.map(
o => (typeof o === "string" ? { value: o, label: o } : o)
);
options.forEach(o => console.log(o));
let random = Math.round(Math.random()*1008)

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>
<div class="custom-control custom-checkbox">
${options.map(
({ value, label }) => `
<input type=checkbox name="input" value="${value || "on"}" ${
(formValue || []).indexOf(value) > -1 ? "checked" : ""
} style="vertical-align: baseline;" class="${className}" id="customCheck_${value}"/>
<label class="custom-control-label" for="customCheck_${value}">${label}</label>
`
)}
</div>
</form>
`
});
form.output.remove();
return form;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function input(config) {
let {form, type = "text", attributes = {}, action, getValue, title, description, format, submit, options, css_class=""} = config;
if (!form) form = html`<form>
<input name=input type=${type}/>
</form>`;
const input = form.input;
Object.keys(attributes).forEach(key => {
const val = attributes[key];
if (val != null) input.setAttribute(key, val);
});
if (submit) form.append(html`<input name=submit type=submit style="margin: 0 0.75em" value="${typeof submit == 'string' ? submit : 'Submit'}" />`);
form.append(html`<output name=output style="font: 14px Menlo, Consolas, monospace; margin-left: 0.5em;"></output>`);
if (title) form.prepend(html`<div style="font: 700 0.9rem sans-serif;">${title}</div>`);
if (description) form.append(html`<div style="font-size: 0.85rem; font-style: italic;">${description}</div>`);
if (format) format = format;//d3format.format(format);
if (action) {
action(form);
} else {
const verb = submit ? "onsubmit" : type == "button" ? "onclick" : type == "checkbox" || type == "radio" ? "onchange" : "oninput";
form[verb] = (e) => {
e && e.preventDefault();
const value = getValue ? getValue(input) : input.value;
if (form.output) form.output.value = format ? format(value) : value;
form.value = value;
if (verb !== "oninput") form.dispatchEvent(new CustomEvent("input"));
};
if (verb !== "oninput") input.oninput = e => e && e.stopPropagation() && e.preventDefault();
if (verb !== "onsubmit") form.onsubmit = (e) => e && e.preventDefault();
form[verb]();
}
return form;
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more