Vue Suggestion List Input
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
-
[Vue warn]: Unknown custom element: <vue-suggestion> - did you register the component correctly? For recursive components, make sure to provide the 'name' option.
cause The `VueSuggestion` component has not been registered with Vue, either globally via `Vue.use()` or locally within a parent component's `components` option.fixTo register globally: `import VueSuggestion from 'vue-suggestion'; Vue.use(VueSuggestion);`. To register locally: `import { VueSuggestion } from 'vue-suggestion'; export default { components: { VueSuggestion } };` -
TypeError: Cannot read properties of undefined (reading 'name')
cause The `setLabel` prop or the `itemTemplate` expects a specific property (e.g., `name`) on the `item` object, but the `items` array contains objects with a different structure or `item` is undefined.fixVerify that the `items` array elements have the properties expected by `setLabel` and your `itemTemplate`. Adjust `setLabel` and `itemTemplate` to match the actual data structure of your `items`. -
Suggestion list appears empty or doesn't update when typing.
cause The `inputChange` event handler is not correctly updating the component's `items` data property, or the filtering logic within `inputChange` is incorrect, resulting in an empty or unfiltered `items` array.fixEnsure that within your `@changed` event handler, `this.items` is actively updated with the filtered results based on the input `text`. Double-check your filtering logic or data fetching mechanism.
Warnings
- breaking Version 2.0.0 was a complete rewrite of the codebase, changing the build tool from Rollup to Vue CLI and reorganizing component props (e.g., `inputOptions`, `suggestionOptions`). Existing v1.x implementations will require significant refactoring.
- breaking Version 1.0.0 transitioned the build tool from PoiJS to Vue CLI. While less impactful for direct users, internal build processes or highly customized integrations based on pre-1.0.0 build artifacts might be affected.
- gotcha The `vue-suggestion` library is primarily built for Vue 2. Using it in a Vue 3 project will likely lead to incompatibility issues and may require a compatibility layer (`@vue/compat`) or manual adaptation, as Vue 3 introduces breaking changes to the reactivity system and component lifecycle.
- gotcha The component provides basic markup but relies heavily on custom CSS for styling the input, suggestion list, and active item states. Without custom styles, the component's appearance will be minimal and may not integrate well with the application's design.
- gotcha The component does not perform data filtering itself. The responsibility of filtering the `items` array based on user input lies with the consumer, typically within the `@changed` event handler. If the `items` prop is not updated correctly, the suggestion list will not show relevant results.
Install
-
npm install vue-suggestion -
yarn add vue-suggestion -
pnpm add vue-suggestion
Imports
- VueSuggestion
import { VueSuggestion } from 'vue-suggestion'; Vue.use(VueSuggestion);import VueSuggestion from 'vue-suggestion'; Vue.use(VueSuggestion);
- VueSuggestion
import VueSuggestion from 'vue-suggestion';
import { VueSuggestion } from 'vue-suggestion'; - itemTemplate
import { itemTemplate } from './item-template.vue';import itemTemplate from './item-template.vue';
Quickstart
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()));
},
},
});