File size: 824 Bytes
f5d2194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}