Published
Edited
Jul 21, 2021
1 fork
Importers
22 stars
Insert cell
Insert cell
data = FileAttachment("flare.json").json()
Insert cell
Insert cell
viewof selection = treeInput(data)
Insert cell
selection
Insert cell
Insert cell
viewof selection2 = treeInput(data, {
children: (d) => d.children,
name: (d) =>
htl.html`<code style=${{
color: `hsl(${Math.random() * 360}, 50%, 50%)`
}}>${d.name}</code>`,
open: 1,
value: data.children[3]
})
Insert cell
selection2
Insert cell
Insert cell
function treeInput(tree, { name = getName, children = getChildren, open = 0, value = tree } = {}) {
const id = DOM.uid().id;
const form = htl.html`<form style=${{
fontFamily: "var(--sans-serif)",
fontSize: "smaller"
}}>
${inner(tree, { open })}
</form>`;
set(value)();
return form;

function set(value) {
return function () {
form.value = value;
form.dispatchEvent(new Event("input", { bubbles: true }));
};
}
function inner(node, { checked = false, open } = {}) {
const item = htl.html`
<label><input type="radio" name="${id}" checked=${node === value} onInput=${set(node)}>
${name(node)}</label>`;
return node.children?.length
? htl.html`<details open=${Boolean(open)}>
<summary>${item}</summary>
<div style=${{ marginLeft: "24px" }}>
${children(node).map(d => inner(d, {open: open - 1}))}
</div>
</details>`
: htl.html`<div>${item}</div>`;
}
}
Insert cell
getName = (d) => d.name
Insert cell
getChildren = (d) => d.children
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