Published
Edited
Jun 4, 2018
Fork of Inputs
1 fork
Importers
3 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
function input(config) {
let {form, type = "text", attributes = {}, action, getValue, title, prefix, postfix, 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 (postfix) form.append(html`<span style="font: 14px Menlo, Consolas, monospace; margin-left: 0.2em;">${postfix}</span>`);
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 (prefix) form.prepend(html`<span style="font: 14px Menlo, Consolas, monospace; margin-right: 0.2em;">${prefix}</span>`);
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();
//AP: hack to use change event for just updating output diplay value
if (verb == "onsubmit" && type == 'range'){
form.onchange = (e) => {
const value = getValue ? getValue(input) : input.value;
if (form.output) form.output.value = format ? format(value) : value;
form.value = value;
}
}
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