readable-demo / kit /components /samples.js
akagabi's picture
Upload folder using huggingface_hub
f5d2194 verified
Raw
History Blame Contribute Delete
824 Bytes
export function mountSamples({ container, textarea, samples, autofill = true }) {
const chips = [];
const clear = () => {
for (const chip of chips) chip.setAttribute("aria-pressed", "false");
};
for (const [index, sample] of samples.entries()) {
const chip = document.createElement("button");
chip.type = "button";
chip.className = "chip";
chip.textContent = sample.label;
chip.setAttribute("aria-pressed", autofill && index === 0 ? "true" : "false");
chip.addEventListener("click", () => {
textarea.value = sample.text;
clear();
chip.setAttribute("aria-pressed", "true");
});
chips.push(chip);
container.append(chip);
}
if (autofill && samples.length > 0) textarea.value = samples[0].text;
textarea.addEventListener("input", clear);
return chips;
}