Published
Edited
Jan 8, 2019
Importers
Insert cell
Insert cell
Insert cell
viewof options = createControls({
id: 'toggleControl',
type: 'toggle',
defaultValue: true
}, {
id: 'selectControl',
type: 'select',
selections: [
{ value: 'firstValue' },
{ value: 'secondValue', title: 'Second Value' }
],
defaultValue: 'firstValue'
})
Insert cell
function createControls (...configs) {
const form = html`<form style="display: flex;"></form>`
configs.forEach(config => {
const createControl = createControlByType[config.type]
const control = createControl(config)
form.append(control)
})
form.oninput = () => {
form.value = _.reduce(form.elements, (memo, input) => {
memo[input.name] = input.type === 'checkbox' ? input.checked : input.value
return memo
}, {})
}
form.oninput()
return form
}
Insert cell
function createToggle ({ id, title, defaultValue = false}) {
title = title || id
const toggle = html`
<div style="margin: 10px;">
<label>${title}</label>
<input name="${id}" type="checkbox" ${defaultValue ? 'checked' : ''}>
</div>
`
return toggle
}
Insert cell
function createSelect ({ id, title, selections = [], defaultValue }) {
title = title || id
const select = html`
<div style="margin: 10px;">
<label>${title}</label>
<select name="${id}">
${selections.map(createSelection)}
</select>
</div>
`
return select
}
Insert cell
function createSelection ({ value, title }) {
title = title || value
return html`<option value="${value}">${title}</option>`
}
Insert cell
createControlByType = ({
toggle: createToggle,
select: createSelect
})
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