Vue Suggestion List Input

2.1.0 · active · verified Sun Apr 19

vue-suggestion is a Vue 2 component library designed to provide a flexible and customizable suggestion list input, commonly known as an autocomplete field. It simplifies the process of displaying a filtered list of items to users as they type, abstracting away the UI logic for suggestion display and selection. The current stable version is 2.1.0. Releases appear to follow an ad-hoc cadence, with significant rewrites occurring in major versions like v2.0.0. Its key differentiators include support for customizable item templates using Vue components, allowing developers to define exactly how each suggestion in the list is rendered. It also provides a clear API for handling input changes and item selections through events and props.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `vue-suggestion` with global plugin registration, local data filtering, a custom label function, and a custom item template. It shows how to handle input changes and item selections to update the component's state.

import Vue from 'vue';
import VueSuggestion from 'vue-suggestion';
import itemTemplate from './item-template.vue';

Vue.use(VueSuggestion);

new Vue({
  el: '#app',
  template: `
    <div id="app">
      <vue-suggestion
        :items="items"
        v-model="selectedItem"
        :setLabel="setLabel"
        :itemTemplate="itemTemplate"
        @changed="inputChange"
        @selected="itemSelected"
      >
      </vue-suggestion>
      <p>Selected Item: {{ selectedItem.name || 'None' }}</p>
    </div>
  `,
  components: {
    // No need to list VueSuggestion here if globally registered via Vue.use()
  },
  data() {
    return {
      selectedItem: {},
      items: [
        { id: 1, name: 'Golden Retriever' },
        { id: 2, name: 'Cat' },
        { id: 3, name: 'Squirrel' },
        { id: 4, name: 'Dolphin' },
        { id: 5, name: 'Elephant' },
      ],
      itemTemplate,
    };
  },
  methods: {
    itemSelected(item) {
      this.selectedItem = item;
      console.log('Selected:', item);
    },
    setLabel(item) {
      return item.name;
    },
    inputChange(text) {
      console.log('Input changed:', text);
      // Simulate asynchronous search or filter local data
      this.items = [
        { id: 1, name: 'Golden Retriever' },
        { id: 2, name: 'Cat' },
        { id: 3, name: 'Squirrel' },
        { id: 4, name: 'Dolphin' },
        { id: 5, name: 'Elephant' },
      ].filter((item) => item.name.toLowerCase().includes(text.toLowerCase()));
    },
  },
});

view raw JSON →