Vue Autosuggest Component

2.2.0 · active · verified Tue Apr 21

Vue-autosuggest is a Vue.js component designed for building accessible autosuggest, autocomplete, and typeahead input fields. It is currently stable at version 2.2.0 and receives regular updates, often focusing on WAI-ARIA compliance and bug fixes, with new features introduced periodically. Key differentiators include its WAI-ARIA completeness, offering full control over rendering through custom components or built-in defaults, supporting multiple sections for suggestions, and being CSS-agnostic, giving developers complete styling freedom. It also facilitates easy integration with AJAX data fetching for dynamic suggestion lists. The component is rigorously tested and aims for a resilient architecture, providing a robust solution for interactive search inputs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a Vue app with the VueAutosuggest component, demonstrating basic setup with static suggestions, input handling, and selection. It showcases both global registration and local component usage patterns.

import Vue from 'vue';
import { VueAutosuggest } from 'vue-autosuggest';

Vue.use(VueAutosuggest); // Register globally, or import locally into a component

new Vue({
  el: '#app',
  components: {
    VueAutosuggest
  },
  data() {
    return {
      query: '',
      selectedItem: null,
      suggestionsData: [{
        data: ['Frodo', 'Samwise', 'Gandalf', 'Galadriel', 'Faramir', 'Éowyn']
      }]
    };
  },
  methods: {
    onInputChange(input) {
      this.query = input;
      console.log('Input changed:', input);
      // In a real application, you would typically fetch suggestions from an API here
      // and update this.suggestionsData based on the input.
    },
    selectHandler(selected) {
      this.selectedItem = selected.item;
      console.log('Item selected:', selected.item);
    },
    clickHandler(event) {
      console.log('Input clicked:', event);
    },
  },
  template: `
    <div class="app-container">
      <h3>Vue Autosuggest Example</h3>
      <vue-autosuggest
          :suggestions="suggestionsData"
          :input-props="{id:'autosuggest__input', placeholder:'Search characters...'}"
          @input="onInputChange"
          @selected="selectHandler"
          @click="clickHandler"
      >
        <template slot-scope="{suggestion}">
          <span class="my-suggestion-item">{{suggestion.item}}</span>
        </template>
      </vue-autosuggest>
      <p v-if="selectedItem" style="margin-top: 15px;">Selected: <strong>{{ selectedItem }}</strong></p>
      <p style="margin-top: 10px;">Current input: <em>{{ query }}</em></p>
    </div>
  `
});

view raw JSON →