WlAutocomplete

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

Change props to see the result: the example and code use the same settings. “Reset” restores the initial values.

Settings

size

Field size.

density

Density.

placeholder

Placeholder.

dropdownLabel

Accessible list expansion button name; falls back to WlConfig.locale.showOptions (Show options in English).

minLength

Minimum characters to start a search.

debounce

Delay in milliseconds before requesting completion for changed text.

Error state.

Disables the field.

List expansion button.

Suggestion animation; defaults to WlConfig.motion (true).

Show a named clear button; clears a single model to null.

Indicate pending work supplied by the application.

Props suggestions, optionLabel, multiple, pt, optionDisabled, optionKey, models, handlers and slots can be edited in the SFC. See their descriptions in API.

Multiple selection is shown in the example below.

Example

Focus: outside the example · pointer: outside the example

Explore the live example and copy its Vue SFC into your application. Settings on the left can be combined.

Code for your application

Vue SFC · current settings
<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.

Vue SFC · additional 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];
}
</script>

<template>
  <div class="wl-stack" data-space="xl" data-input-example="WlAutocomplete">
    <section class="wl-stack" data-space="md">
      <h3 class="wl-text-title">{{ "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-title">{{ "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>
  </div>
</template>