WlAutocomplete

Field with suggestions; the suggestions list updates through the complete event.
Components / Inputs v0.2.0

Selection

Full example
<script setup lang="ts">
import { WlAutocomplete, WlButton } from "legato-ui";
import { onBeforeUnmount, ref } from "vue";
const value = ref<string | null>("Anna");
const suggestions = ref(["Anna", "Andrey", "Maria"]);
const loading = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
function cancel(): void {
  clearTimeout(timer);
  loading.value = false;
}
function complete(event: { query: string }): void {
  cancel();
  loading.value = true;
  // Simulate application-owned work; typing again replaces this local request.
  timer = setTimeout(() => {
    suggestions.value = ["Anna", "Andrey", "Maria"]
      .filter((name) => name.toLowerCase().includes(event.query.toLowerCase()));
    loading.value = false;
  }, 600);
}
onBeforeUnmount(cancel);
</script>

<template>
  <div class="wl-stack" data-space="md">
    <WlAutocomplete
      v-model="value"
      :suggestions="suggestions"
      :loading="loading"
      :debounce="250"
      clearable
      dropdown
      aria-label="Member"
      
      @complete="complete"
    />
    <WlButton v-if="loading" variant="ghost" @click="cancel">{{ "Cancel" }}</WlButton>
  </div>
</template>

States

Full example
<script setup lang="ts">
import { WlAutocomplete, WlButton } from "legato-ui";
import { onBeforeUnmount, ref } from "vue";
const value = ref<string | null>("Anna");
const suggestions = ref(["Anna", "Andrey", "Maria"]);
const loading = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
function cancel(): void {
  clearTimeout(timer);
  loading.value = false;
}
function complete(event: { query: string }): void {
  cancel();
  loading.value = true;
  // Simulate application-owned work; typing again replaces this local request.
  timer = setTimeout(() => {
    suggestions.value = ["Anna", "Andrey", "Maria"]
      .filter((name) => name.toLowerCase().includes(event.query.toLowerCase()));
    loading.value = false;
  }, 600);
}
onBeforeUnmount(cancel);
</script>

<template>
  <div class="wl-stack" data-space="md">
    <WlAutocomplete
      v-model="value"
      :suggestions="suggestions"
      :loading="loading"
      :debounce="250"
      clearable
      dropdown
      aria-label="Member"
      
      @complete="complete"
    />
    <WlButton v-if="loading" variant="ghost" @click="cancel">{{ "Cancel" }}</WlButton>
  </div>
</template>

Search and multiple participants

Object suggestions through complete, a prefilled dropdown, multiple, sizes and states.

Find an owner and select members

Enter a name. The application updates suggestions on the complete event.
Anna

The button opens the current suggestions; the initial list is prefilled. Selecting a suggestion returns an object; typing in single mode returns a string. The label function supports both.

Sizes, compact density and states

Confirm the owner before saving.

Owner: Anna. Members: Anna.

5,000 options

Set virtual to window a large option list. Options and group headings use the same fixed height, and keyboard navigation continues through the full filtered list.

Full example
<script setup lang="ts">
import { WlAutocomplete, WlButton, WlField } from "legato-ui";
import { computed, ref } from "vue";
const people = [{ id: "anna", label: "Anna" }, { id: "boris", label: "Boris" }, { id: "dmitry", label: "Dmitry" }, { id: "elena", label: "Elena" }];
type Person = (typeof people)[number];
const person = ref<Person | string | null>(people[0]!);
const participants = ref<Person[] | null>([people[0]!]);
const singleSuggestions = ref([...people]);
const multipleSuggestions = ref([...people]);
function filter(query: string) {
  return people.filter((item) => item.label.toLocaleLowerCase().includes(query.toLocaleLowerCase()));
}
function searchSingle(event: { query: string }) {
  singleSuggestions.value = filter(event.query);
}
function searchMultiple(event: { query: string }) {
  multipleSuggestions.value = filter(event.query);
}
function label(value: unknown): string {
  return typeof value === "object" && value !== null && "label" in value ? String(value.label) : String(value ?? "");
}
const selectedNames = computed(() => Array.isArray(participants.value) ? participants.value.map(label).join(", ") : "");
function reset() {
  person.value = "";
  participants.value = [];
  singleSuggestions.value = [...people];
  multipleSuggestions.value = [...people];
}
const largeOptions = computed(() => Array.from({ length: 5000 }, (_, index) =>
  ["Record", index + 1].join(" ")
));
const virtualQuery = ref("");
const results = computed(() => largeOptions.value.filter((option) => option.toLocaleLowerCase().includes(virtualQuery.value.toLocaleLowerCase())));

</script>

<template>
  <div class="wl-stack" data-space="xl" data-input-example="WlAutocomplete">
    <section class="wl-stack" data-space="md">
      <h3 class="wl-text-subheading">{{ "Find an owner and select members" }}</h3>
      <div class="wl-grid" data-space="lg">
        <WlField
          id="docs-auto-single"
          v-slot="{ id, ariaDescribedby }"
          label="Owner"
          hint="Enter a name. The application updates suggestions on the complete event."
        ><WlAutocomplete
          :id="id"
          v-model="person"
          :suggestions="singleSuggestions"
          :option-label="label"
          dropdown
          dropdown-label="Show owners"
          :aria-describedby="ariaDescribedby"
          @complete="searchSingle"
        /></WlField>
        <WlField id="docs-auto-multiple" v-slot="{ id }" label="Members"><WlAutocomplete
          :id="id"
          v-model="participants"
          :suggestions="multipleSuggestions"
          :option-label="label"
          multiple
          dropdown
          dropdown-label="Show members"
          :min-length="0"
          @complete="searchMultiple"
        /></WlField>
      </div>
      <p class="wl-text-small wl-text-muted">{{ "The button opens the current suggestions; the initial list is prefilled. Selecting a suggestion returns an object; typing in single mode returns a string. The label function supports both." }}</p>
    </section>
    <section class="wl-stack" data-space="md">
      <h3 class="wl-text-subheading">{{ "Sizes, compact density and states" }}</h3>
      <div class="wl-grid" data-space="lg">
        <WlField
          v-for="size in ['sm', 'md', 'lg'] as const"
          :id="`docs-auto-${size}`"
          :key="size"
          v-slot="{ id }"
          :label="`${'Owner, '}${size}`"
        ><WlAutocomplete
          :id="id"
          v-model="person"
          :size="size"
          :suggestions="singleSuggestions"
          :option-label="label"
          @complete="searchSingle"
        /></WlField>
        <WlField id="docs-auto-compact" v-slot="{ id }" label="Compact suggestions"><WlAutocomplete
          :id="id"
          v-model="person"
          density="compact"
          :suggestions="singleSuggestions"
          :option-label="label"
          @complete="searchSingle"
        /></WlField>
        <WlField
          id="docs-auto-invalid"
          v-slot="{ id, ariaDescribedby, invalid }"
          label="Suggestions with an error"
          error="Confirm the owner before saving."
        ><WlAutocomplete
          :id="id"
          v-model="person"
          :suggestions="singleSuggestions"
          :option-label="label"
          :invalid="invalid"
          :aria-describedby="ariaDescribedby"
          @complete="searchSingle"
        /></WlField>
        <WlField id="docs-auto-disabled" v-slot="{ id }" label="Unavailable suggestions"><WlAutocomplete
          :id="id"
          :model-value="people[0]"
          :suggestions="people"
          :option-label="label"
          disabled
          dropdown
        /></WlField>
      </div>
      <WlButton size="sm" @click="reset">{{ "Clear selected values" }}</WlButton>
      <p class="wl-text-small" role="status">{{ "Owner:" }} {{
        label(person) || "not selected"
      }}{{ ". Members:" }} {{ selectedNames || "none selected" }}.</p>
    </section>
    <section class="wl-stack" data-space="md">
      <h3 class="wl-text-subheading">{{ "5,000 options" }}</h3>
      <WlAutocomplete
        aria-label="5,000 options"
        :suggestions="results"
        virtual
        dropdown
        @complete="virtualQuery = $event.query"
      />
      <p class="wl-text-small wl-text-muted">{{ "Set virtual to window a large option list. Options and group headings use the same fixed height, and keyboard navigation continues through the full filtered list." }}</p>
    </section>
  </div>
</template>