Public
Edited
Jun 9, 2023
2 forks
Importers
8 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof wiki = SearchForm({
placeholder: "Type-in to search...",
description: "Searching en.wikipedia.org entities",
suggestion: async (text) => {
const res = await d3.json(
`https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=${text}&namespace=0&limit=10&origin=*`
);
return res[1];
}
})
Insert cell
wiki
Insert cell
Insert cell
Insert cell
Insert cell
viewof wikiObject = SearchForm({
placeholder: "Type-in to search...",
description: "Searching en.wikipedia.org entities with their URLs",
format: (d) => d.entry, // what you want to search and display
suggestion: async (text) => {
const res = await d3.json(
`https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=${text}&namespace=0&limit=10&origin=*`
);
return res[1].map((d, i) => ({
entry: d,
url: res[3][i]
}));
}
})
Insert cell
wikiObject
Insert cell
Insert cell
Insert cell
function SearchForm({
uid = DOM.uid("autosuggestion").id,
placeholder = "",
description = "",
value = "",
format = (d) => d,
suggestion = () => []
} = {}) {
const input = htl.html`<input
type="text"
placeholder="${placeholder}"
list="${uid}"
autocomplete="off"
value=${format(value)}
>`;

const tag = htl.html`<div style="font-size: 0.85rem; font-style: italic; margin-top: 3px;">${description}</div>`;

const datalist = htl.html`<datalist id="${uid}">`;

const form = htl.html`<div>${input}${tag}${datalist}`;

let results = [];

form.value = value; // starting value
form.onsubmit = (event) => event.preventDefault();

form.onchange = (event) => {
const value = event.target.value;
form.value = results.find((d) => format(d) == value) || "";
input.blur(); // removes keyboard focus from the element upon submission
form.dispatchEvent(new CustomEvent("input"));
};

// "cache"
const options = new Map();

const getOption = async (text) =>
options.get(text) ||
(text ? options.set(text, await suggestion(text)) : options.set(text, []),
options.get(text));

input.oninput = async (event) => {
let value = event.target.value;

results = await getOption(value);

d3.select(`#${uid}`)
.selectAll("option")
.data(results)
.join("option")
.attr("value", format);
};

return form;
}
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