Feature-rich autocomplete component for Vue.js
npm install vue-simple-suggest> Simple yet feature-rich autocomplete component for Vue.js






``bash`
npm install --save vue-simple-suggest
See installation guide for more options.
- vue-simple-suggest
- Install
- Table of contents
- What is it
- Features
- New features?
- Examples:
- Simple example
- Async example
- Installation
- NPM
- Unpkg
- Importing
- Polyfills
- Usage
- Build Setup
- Default Controls
- Component API
- TLDR
- CSS class structure
- Transitions
- API definitions
- Props
- mode
- Emitted Events
- Ref Methods
- Ref Event Handlers
- Ref Data
- Slots
- Custom input
- Accessibility on custom input
- Custom suggestion item
- Custom miscellaneous item slots
- Contributors
This is a simple yet feature-rich suggestion/autocomplete component for Vue.js.
Actually, it's so feature rich, that it's possible to do crazy stuff with it, like
- Imitating drop-downs and drop-down menus
- Turn suggestions list into an actual suggestions table
- Work with ANY type of custom input passed (like type=button, radio and etc.)
- ... And many more things
And, as a bonus, it is very light.
- v-model support.v-model
- Automatic accessibility attributes (WAI-ARIA complete)
- Switching type (select/input).type
- Custom input element through default slot.
- Custom list items through named scoped slots.
- All HTML5-valid props for default input element are provided (, tabindex and etc...).
- Customizable keyboard controls.
- Rich and simple API.
- CSS classes for quick and easy restyling.
- Many build variants to choose from.
- Flexible and customizable component design.
- Optional polyfills for IE importable from the lib.
All of the props, events and slots are OPTIONAL for this component, so it can be used without any configuration at all.
If you feel that something important is missing (or found a bug) - feel free to create an issue. :)
-----
To use the component just install via NPM:
npm install --save vue-simple-suggest
Then, in your Vue.js component/page do the following...
If you need to suggest things from a static array:
`html
:list="simpleSuggestionList"
:filter-by-query="true">
Chosen element: {{ chosen }}
`
If you're dealing with async data from server (example using https://swapi.co/):
`html
display-attribute="name"
value-attribute="url"
:list="getSuggestionList"
>
Chosen element: {{ chosen }}
`
For a more advanced example (using wikipedia search)
see our example source.
-----
`bash`
npm install --save vue-simple-suggestor
yarn add vue-simple-suggest
If including via this method - the component will automatically install itself.
`html
`
`js
/// ESNext (original code, no pollyfills, single-file .vue component, css included)
import VueSimpleSuggest from 'vue-simple-suggest/lib'
///
/// ES6 (async polyfills)
import VueSimpleSuggest from 'vue-simple-suggest'
// or, if you have problems importing:
import VueSimpleSuggest from 'vue-simple-suggest/dist/es6'
///
/// ES7 and above (no polyfills)
import VueSimpleSuggest from 'vue-simple-suggest/dist/es7'
///
/// CommonJS (async, arrow-functions and promises are polyfilled)
const VueSimpleSuggest = require('vue-simple-suggest')
// or, if you have problems importing:
const VueSimpleSuggest = require('vue-simple-suggest/dist/cjs')
///
// Optional - import css separately with css loaders:
import 'vue-simple-suggest/dist/styles.css'
`
#### Polyfills
> New in v1.8.3
vue-simple-suggest comes with minimal optional polyfills for IE11+ - Object.assign, Element.prototype.closest and Element.prototype.matches.`
You can import them like this:js`
import 'vue-simple-suggest/lib/polyfills';
// or
require('vue-simple-suggest/lib/polyfills');
Globaly:
`js`
// You don't need to do it, if including via
-----
` bashclone the repo
git clone https://github.com/KazanExpress/vue-simple-suggest.git
cd ./vue-simple-suggest
-----
Default Controls
> New in v1.2.0
These are default keyboard shortcuts.
controls prop. All fields in this controls object are optional.Default scheme:
|Key (key code) | Description|
|-|-|
|
Escape (27) | If the suggestions list is shown - hide it. Defined by hideList property. |
|ArrowDown (40) | If the suggestions list is hidden - show it. Defined by showList property. |
|ArrowUp (38) / ArrowDown (40) | Cycle (hover) through suggestions. Defined by selectionUp/selectionDown properties respectfully. |
|Enter (13) | If the list is shown - chooses the highlighted element. Defined by select property.|
|(Ctrl/Shift) + Space (32) | Select the first element in the list. Defined by autocomplete property. Works with Ctrl modifier key or Shift modifier key. |
|(Ctrl/Shift) + Enter (13) | Same as previous, but also hides the suggestions list. |JS object:
`js
{
selectionUp: [38],
selectionDown: [40],
select: [13],
showList: [40],
hideList: [27],
autocomplete: [32, 13]
}
`-----
Component API
$3
Click to expand
`html
value-attribute="id"
display-attribute="title"
mode="input"
placeholder="placeholder!!!"
:list="getListFunction"
:max-suggestions="10"
:min-length="3"
:debounce="100"
:destyled="false"
:remove-list="false"
:filter-by-query="false"
:filter="customFilterFunction"
:value="defaultValue"
:nullable-select="true"
:controls="{
selectionUp: [38, 33],
selectionDown: [40, 34],
select: [13, 36],
showList: [40],
hideList: [27, 35],
autocomplete: [32, 13],
}"
@input="onInputEvent"
@select="onSuggestSelect"
@hover="onSuggestHover"
@focus="onFocus"
@blur="onBlur"
@request-start="onRequestStart"
@request-done="onRequestDone"
@request-failed="onRequestFailed"
@show-list="onShowList"
@hide-list="onHideList"
>
You're searching for {{ query }}.
{{ suggestions.length }} suggestions are shown...
{{ suggestion.title }}
Loading...
`-----
$3
If there's a need to customize the appearance of the component, here's the internal class-structure:
`less
// .designed is applied only if destyled prop is false.
.vue-simple-suggest.designed.focus // .focus is applied whenever the component is focused.
.input-wrapper
.default-input // Replaced with your custom input if default slot is provided.
.suggestions // Also has transition classes, see below.
.suggest-item
`If you wish to use your existing classes, or frameworks like Bootstrap you can inject your own classes using the
'styles' prop:`html
v-model="chosen"
:list="simpleSuggestionList"
:styles="autoCompleteStyle"
:destyled=true
:filter-by-query="true">
``
Scheme:
|Property | Description|
|-|-|
|vueSimpleSuggest | Additional classname for component's root element. |inputWrapper
| | Additional classname for .input-wrapper element. |defaultInput
| | Additional classname for input element if no custom input component is given. |suggestions
| | Additional classname for suggestions list ul element. |miscItemAbove
| | Classname for misc-item-above slot wrapper (li element itself). |suggestItem
| | Additional classname for suggestion item li element. |miscItemBelow
| | Classname for misc-item-below slot wrapper (li element itself). |
-----
#### Transitions
> New in v1.8.0
You can also define custom list transitions by defining css rules for the transition named vue-simple-suggest on the .suggestions div:
`css
.suggestions {
/ It's improtant to have a cpecific pivot point for transition:/
opacity: 1;
}
.vue-simple-suggest-enter-active.suggestions,
.vue-simple-suggest-leave-active.suggestions {
/ Transition length here:/
transition: opacity .2s;
}
.vue-simple-suggest-enter.suggestions,
.vue-simple-suggest-leave-to.suggestions {
/ Transition rule for "disengaged" element:/
opacity: 0;
}
`
-----
#### Props
| Name | Type | Default | Description |
|--------------------------------|----------|----------|--------------------------------------------------------------|
| controls v1.2.0 | Object | See default controls | Determines the keyboard shortcuts in key-codes (for browser-compatibility purposes). Arrays provide the ability to assign multiple keys to one action. Consists of 5 array fields: selectionUp, selectionDown, select, hideList and autocomplete, all of which are optional. |max-suggestions
| | Number | 10 | The maximum amount of suggestions to display. Set to 0 for infinite suggestions. |min-length
| | Number | 1 | The minimum amount of symbols in input to trigger suggestion list. vue-simple-suggest starts behaving as a dropdown menu, if the value is 0. |display-attribute
| | String | 'title' | The property in a suggestion object to display in a list. Supports dotted paths. |value-attribute
| | String | 'id' | The property in a suggestion object to use as a unique key. Supports dotted paths. |list
| | Function or Array | () => [] | The array provider function, must accept a query as its only argument. Can return an array or a promise. Can be async. The component behaves as a simple input without this function. |debounce
| | Number | 0 | Determines the list debounce (a time between the input event and a function execution). |destyled
| | Boolean | false | Whether to cancel the default styling of input and suggestions list. |styles
| v1.8.0 | Object | {} | CSS classes to attach with current component style. |remove-list
| | Boolean | false | If true - the suggestion list will be always hidden. |filter-by-query
| | Boolean | false | Whether to filter the resulting suggestions by input's text query (make it a search component). |filter
| | Function | - | A custom function for filtering the suggestion results that accepts a single item and a query to filter by as its 2 arguments. Used only if filter-by-query is set to true. |mode
| v1.4.0 | String | 'input' | The v-model event. Determines the event, that triggers v-model. Can be one of 'input' (v-model binds a displayed property) or 'select' (v-model binds a selected item). |type
| , value, pattern, etc... | | | All of the HTML5 input attributes with their respected default values. |nullable-select
| v1.9.0 | Boolean | false | Whether the select should accept null or not. |preventHide
| v1.11.0|Boolean|false|Whether to keep the input open or not, allowing the user to select multiple inputs|
##### mode
> New in v1.4.0
Determines the event, that triggers v-model. Can be one of 'input' (default) or 'select'.
For example, if 'input' is chosen - then v-model will update the value each time an input event is fired, setting the input's string.
Same is for 'select' - v-model changes only when something is selected from the list, setting the selected value (string, object or whatever) to its argument.
A proper use-case for it being when one wants to use the component only for selection binding and custom input for text binding:
`html`
-----
#### Emitted Events
| Name | Arguments | Description |
|-----------------|-----------------------------|--------------------------------------------------------------------------------------------------------|
| input | HTML input event | An outward projection of the current input's event. |focus
| | HTML focus event | An outward projection of the current input's event. |blur
| | HTML focus event | An outward projection of the current input's event. |select
| | Selected suggestion | Fires on suggestion selection (via a mouse click or enter keypress). |hover
| | Hovered suggestion, target element | Fires each time a new suggestion is highlighted (via a cursor movement or keyboard arrows). |suggestion-click
| | Selected suggestion, HTML click event | Fires on suggestion element click. |show-list
| | - | Fires each time the suggestion list is toggled to be shown. |hide-list
| | - | Fires each time the suggestion list is being hidden. |request-start
| | Current input value (query) | Fires each time a list function starts executing. |request-done
| | Resulting suggestions list | Fires when a list function successfully returns a result and forwards that result as an argument. |request-failed
| | The interrrupting exception | Fires if an exception occurs during the execution of a list funciton. |
-----
#### Ref Methods
> accessed via $refs.your ref name here
| Name | Arguments | Description |
|------|-----------|-------------|
|showList| - | Shows the suggestion list. Emits the respected event. |hideList
|| - | Hides the suggestion list. Emits the respected event. |getSuggestions
|| query: string | Gets and processes suggestions from the list prop. Returns a promise. Emits the requestStart, requestDone and requestFailed events. |research
|| - | Debounced getSuggestions on the current input value. |clearSuggestions
|| - | Clears the suggestions array. |select
|| suggestion | Selects the passed suggestion. Emits the respected event. |hover
|| suggestion | Hovers over the passed suggestion. Emits the respected event. |displayProperty
|| suggestion | Returns the displayed property of a suggestion. |valueProperty
|| suggestion | Returns the value property of a suggestion. |setText
| v1.9.0 | text | Reliably sets custom text to the input field. |autocompleteText
| v1.10.0 | suggestion | Autocompletes the input text using the suggestion passed as the only argument. |
-----
#### Ref Event Handlers
> accessed via $refs.your ref name here
You can use these to imitate some of the component's behaviours.
| Name | Arguments | Description |
|------|-----------|-------------|
|onShowList|| Invoked when a suggestion list needs to be shown. |showSuggestions
||| Shows suggestion list, refreshes the data if needed. |onInput
|| HTML input event | Invoked whenever the input text is changed. Emits the input event. |onFocus
|| HTML focus event | Invoked whenever the input comes into focus, emits the focus event. |onBlur
|| HTML focus event | Antonym to onFocus. |onAutocomplete
|| - | Invoked when the autocomplete keyboard shortcut is pressed. Selects the first suggestion. |onListKeyUp
|| HTML keyup event | Invoked on component keyup. Internally used for hiding the list. |onKeyDown
|| HTML keydown event | Invoked on component keydown. Internally used for showing the list, updating suggestions and preventing form submit. |moveSelection
||| Invoked when hovered element needs to be changed. |suggestionClick
|| suggestion, HTML click event | Invoked on any suggestion click. Can be used to emulate such a click from ouside of the component. |
-----
#### Ref Data
> accessed via $refs.your ref name here
| Name | Default | Description |
|------|-----------|-------------|
|selected| null | Currently selected element. |hovered
|| null | Currently hovered element. |suggestions
|| [] | Current suggestions list. |textLength
|| 0 | Length of the text in the input. |listShown
|| false | Is suggestion list shown. |inputElement
|| null | Currently used HTMLInputElement. |canSend
|| true | Whether the assigned getListFuncion can be executed. |timeoutInstance
|| null | The timeout until next getListFunction execution. |text
|| $props.value | Current input text. |slotIsComponent
|| false | Whether this current custom input is a vue-component. |listIsRequest
|| - | Whether the list prop is a function. |input
|| - | A ref to the current input (component or vanilla). |hoveredIndex
|| - | The current hovered element index. |controlScheme
|| Default Controls | The current controls scheme. |isPlainSuggestion
|| false | Whether the current suggestions list consists of plain strings (not objects). |isClicking
|| false | true if the user currently clicks. |isOverList
|| false | true if the user currently hovers over suggestions list. |isInFocus
|| false | true if the component is currently in focus. |isTabbed
|| false | true if the user pressed tab, while the component is in focus. |isSelectedUpToDate
|| false | true if the user hasn't done any inputs since the last selection, so the selection is still relevant. |
-----
##### Custom input
> default slot (optional)
Supports nesting. Input props can be passed to a custom input to avoid their processing by vue-simple-suggest.
Defaults to a simple input with props passed to vue-simple-suggest.
Warning: v-model on a custom input IS NOT the same as v-model on vue-simple-suggest!
`html``html`
`html``html`
`html`
`html`
Custom input component caveats:
To work with the vue-simple-suggest your custom input component has to follow certain standard behaviours.
Custom input component must (in order to work properly):
- Emit an input event.focus
- Emit and blur events.value
- Have a prop.
Custom input component should (in order to avoid usage limitations):
- Not stop any event propagations from internal input HTML element.
- Forward an original event argument with focus and blur events.
If vue-simple-suggest with your custom component doesn't seem to react to outside variable changes - bind both components' v-model to the same variable, like so:`html`
##### Accessibility on custom input
> New in v1.9.0
vue-simple-suggest automatically injects 3 necessary ARIA attributes for the default element
and any custom input, as long as your custom input component contains an html element.
These attributes ensure that the autocomplete can be used by users who rely on Screen Readers.
| Name | Value | Description |
|-----------------------|------------------------------------|----------------------------------------|
| aria-autocomplete | "list" | Indicates that the autocomplete behavior of the text input is to suggest a list of possible values in a popup. |suggestions
| aria-controls | IDREF of list | IDREF of the popup element that lists suggested values. |
| aria-activedescendant | IDREF of hovered option | Enables assistive technologies to know which element the application regards as focused while DOM focus remains on the input element. |
##### Custom suggestion item
> suggestion-item slot (optional)
Description
Allows custom html-definitons of the suggestion items in a list.
Defaults to {{ displayAttribute(suggestion) }}
Accepts the suggestion object and a query text as a slot-scope attribute values.
`html`
{{ suggestion.title }} by {{ suggestion.author }}
Custom buttons inside of suggestion items
If you want to add some action buttons to the suggetion items,
just use the .stop directive modifier to prevent the default suggestion-click:
`html`
{{ suggestion.title }} by {{ suggestion.author }}
In this case, the buttons will ONLY execute the bound method and will not select the suggested item.
Manual autocomplete
If there's a need to autocomplete the suggestion in the input instead of selecting it (like in mobile suggestions of google search),
you can use the autocomplete() function in the slot's scope:
`html`
{{ suggestion.title }} by {{ suggestion.author }}
or in the ref methods:
`html
{{ suggestion.title }} by {{ suggestion.author }}
`
Ref Data
In cooperation with ref fields this slot can be used to drastically transform the suggestion list behaviour.
One of the simplest examples - highlighting the query text in the results:
`html`
`js
boldenSuggestion(scope) {
if (!scope) return scope;
const { suggestion, query } = scope;
let result = this.$refs.suggestComponent.displayProperty(suggestion);
if (!query) return result;
const texts = query.split(/[\s-_/\\|\.]/gm).filter(t => !!t) || [''];
return result.replace(new RegExp('(.?)(' + texts.join('|') + ')(.?)','gi'), '$1$2$3');
}
`
Result via Google Books search API:

##### Custom miscellaneous item slots
> misc-item-above and misc-item-below slots (optional)
Allow custom elements to be shown in suggestion list. These elements never dissapear from the list, neither can they be selected nor hovered on.
They can be used for decoration, loaders, error messages and etc.
These slots don't have defaults, so they are not shown until defined.
Accept the suggestions array and a query text as a slot-scope attribute values.
`html
You're searching for {{ query }}.
{{ suggestions.length }} suggestions are shown...
These slots can also be used to handle empty results, like this:
`html
You're searching for '{{ query }}'.
{{ suggestions.length }} suggestions are shown...
No results
``----
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind are welcome!