Published unlisted
Edited
Sep 20, 2022
Insert cell
Insert cell
Insert cell
reactiveMultiControlUI = {
const answer = Inputs.radio(["a", "b"]);
const result = Inputs.text({ label: "result", disabled: true });

const checker = () => {
if (submitted > 0) {
if (answer.value == "b") result.value = "correct!";
if (answer.value == "a") result.value = "wrong!";
}
};
answer.addEventListener("input", checker);
invalidation.then(() => answer.removeEventListener("input", checker)); // For every addXXX their is a removeXXX

return Inputs.form([answer, result]);
}
Insert cell
viewof submitted = Inputs.button("Submit")
Insert cell
submitted
Insert cell
## Fetchp
Insert cell
image1 = FileAttachment("image@1.png").image()
Insert cell
import { fetchp } from "@tomlarkworthy/fetchp"
Insert cell
Insert cell
Insert cell
Insert cell
dynamicRadio = juice(Inputs.radio, {
options: "[0]",
result: "[1].value"
})
Insert cell
viewof control = dynamicRadio(["a", "b", "c"])
Insert cell
Insert cell
control
Insert cell
Insert cell
Insert cell
logic = {
if (filter === "all") {
viewof control.options.value = ["a", "b", "c"];
} else if (filter === "b,c") {
if (viewof control.result.value == "a") viewof control.result.value = null;
viewof control.options.value = ["b", "c"];
} else if (filter === "a,c") {
if (viewof control.result.value == "b") viewof control.result.value = null;
viewof control.options.value = ["a", "c"];
}
viewof control.dispatchEvent(new Event("input", { bubbles: true }));
}
Insert cell
Insert cell
Insert cell
Insert cell
myRequire = d3Require.requireFrom(async (name) => {
return `${registry}/${name}`;
})
Insert cell
Insert cell
(runImport, myRequire("@securityscorecard/design-system@1.0.0-alpha.148"))
Insert cell
Insert cell
viewof selection = Inputs.select(["red", "green", "blue"])
Insert cell
Insert cell
viewof submit1 = Inputs.textarea({ value: selection, submit: true })
Insert cell
submit1
Insert cell
Insert cell
viewof submit2 = Inputs.textarea({ submit: true, minlength: 1 })
Insert cell
submit2
Insert cell
Inputs.bind(viewof selection, viewof submit2)
Insert cell
Insert cell
viewof submit3 = Inputs.button("submit", {
required: true,
reduce: () => selection
})
Insert cell
submit3
Insert cell
Insert cell
Insert cell
import { tweet } from "@mbostock/tweet"
Insert cell
tweet("1442010098482626563")
Insert cell
*I am a mardown cell but I escape to JS and have some smart casting so that if my JS returns a DOM node I can render that too ${html`<input type="range">`}*

Look ${function foo() {
const el = document.createElement("h1")
el.innerHTML = "Programmatic Title in embedded markdown";
return el
}()}
Insert cell
Insert cell
viewof foo = {
// Create a view 'foo' with value 'bar'
return Object.defineProperty(md`foo`, "value", {
value: "bar",
writable: true,
enumerable: true
});
}
Insert cell
viewof foo.value
Insert cell
foo
Insert cell
set_value_to_promise = Inputs.button("set new promise", {
reduce: () => {
viewof foo.value = invalidation;
viewof foo.dispatchEvent(new Event('input', { bubbles: true }));
}
})
Insert cell
set_value_to_undefined = Inputs.button("set undefined", {
reduce: () => {
viewof foo.value = undefined;
viewof foo.dispatchEvent(new Event('input', { bubbles: true }));
}
})
Insert cell
md`If you refresh the 'viewof foo.value' cell it will not resolve`
Insert cell
Insert cell
viewof x = Object.assign(Inputs.range([0, 10], { label: "yo", step: 2 }), {
oninput: event => event.isTrusted && event.stopImmediatePropagation(),
onchange: event => event.currentTarget.dispatchEvent(new Event("input"))
})
Insert cell
x
Insert cell
Promises.delay(Number.NaN)
Insert cell
Insert cell
viewof textAreaWithSubmit = Inputs.textarea({
submit: true
})
Insert cell
textAreaWithSubmit
Insert cell
viewof textWithSubmit = Inputs.text({
submit: true
})
Insert cell
textWithSubmit
Insert cell
Insert cell
viewof start = Inputs.text()
Insert cell
viewof middle = {
const middle = Inputs.text();
Inputs.bind(viewof start, middle);
return middle;
}
Insert cell
viewof end = {
const end = Inputs.text();
Inputs.bind(viewof middle, end);
return end;
}
Insert cell
end
Insert cell
start
Insert cell
md`### Repo multi update`
Insert cell
import { exportString } from "@tomlarkworthy/repo1"
Insert cell
exportString
Insert cell
ganja = md`### Ganja.js`
Insert cell
Algebra = require("ganja.js")
Insert cell
Algebra(2, 0, 1, () => {
// 1e1 is the dimension (Y)
// Change to 1e2 to make this a horizontal line
return this.graph([1e1]);
})
Insert cell
Insert cell
viewof cell_a = Inputs.select(["one", "two"])
Insert cell
cell_a
Insert cell
viewof cell_b = Inputs.text()
Insert cell
Insert cell
updater_with_event = {
// You could declare this updating in cell_b too, but I think its more illastrative taking it into its own cell
viewof cell_a.addEventListener("input", (evt) => {
viewof cell_b.value = viewof cell_a.value; // Updates visual appearance but does not trigger dataflow from cell_b
viewof cell_b.dispatchEvent(new Event("input", { bubbles: true }));
});
}
Insert cell
Insert cell
import { view, variable } from "@tomlarkworthy/view"
Insert cell
import { viewroutine, ask } from "@tomlarkworthy/viewroutine"
Insert cell
Insert cell
viewof cell_c = mutableSelection(["a", "b"])
Insert cell
cell_c
Insert cell
cell_c.selection
Insert cell
updater_first_option_using_cell_b = {
viewof cell_b.addEventListener("input", (evt) => {
viewof cell_c.options.value = [viewof cell_b.value, "other"]; // Assign any array of options you like (does nto support in-place mutation like .push)
viewof cell_c.options.dispatchEvent(new Event("input", { bubbles: true }));
});
}
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