Published
Edited
Jul 6, 2018
Fork of Inputs
Importers
4 stars
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
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 input(config) {
let {form, type = "text", attributes = {}, action, getValue, title, description, format, submit, options} = 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 = 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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more