1.5.0 • Published 5 years ago
vanilla-js-autocomplete v1.5.0
vanilla-js-autocomplete
Autocomplete, which you can configure as accurately as possible to fit your needs. You yourself describe his behavior. You can implement any scenario of the autocomplete.
Advantages:
- a lightweight and powerful on pure JavaScript
- does not set you any framework due to internal code implementation
- fit at billion cases
Install
npm i vanilla-js-autocomplete
or
yarn add vanilla-js-autocomplete
Get started
<link rel="stylesheet" href="dist/autocomplete.css">
<script src="dist/autocomplete.js"></script>
<input type="text">
const data = ['Moscow', 'Hong Kong', 'Monaco', 'Tokyo', 'Minsk'];
const input = document.querySelector('input[type=text]');
JcAutocomplete.create(input, {
minChars: 1,
source: (term, response) => {
const re = new RegExp(term, 'iu');
const result = data.filter(item => item.match(re));
if (result) {
response(result);
}
}
});
For more examples
See example/*.html
- fetch from local data
- fetch from remote data
- in body with position - static
- mentions (also custom items and insert value)
- not found case
Default params
const defaults = {
minChars: 3,
delay: 150,
offsetLeft: 0,
offsetTop: 1,
changeValue: true,
containerClass: 'autocomplete-dropdown',
itemClass: 'autocomplete-item',
createContainer: function () {
const container = document.createElement('div');
container.style.display = 'none';
container.classList.add(this.containerClass);
document.body.appendChild(container);
return container;
},
source: function (term, response) {
},
renderItem: function (item, search) {
search = search.replace(/[^\w\s]/g, '\\$&');
const re = new RegExp(`(${ search.split(' ').join('|') })`, 'gi');
return `<div class="${ this.itemClass }" data-val="${ item }">${ item.replace(re, '<mark>$1</mark>') }</div>`;
},
onSelect: function (event, term, item) {
}
};
Options
name | type | default | note |
---|---|---|---|
minChars | Integer | 3 | Number of characters from which autocomplete is activated. |
delay | Integer | 150 | Delay (ms) after entering a character. |
offsetLeft | Integer | 0 | Indent (px) to the left of the list from the input element. |
offsetTop | Integer | 1 | Indent (px) to the top of the list from the input element. |
changeValue | Boolean | true | Substitute the value of the active list item in the input field. |
containerClass | String | autocomplete-dropdown | Element list class name. |
itemClass | String | autocomplete-item | List item class name. |
createContainer | Function | see note column | The function of creating and adding to the page a container of elements. By default, a simple DIV is created with the this.containerClass class. When you create a container yourself, you must add the this.containerClass class to it. |
source | Function | require | The function in which you implement the search in any way convenient for you. After the search is completed, it is necessary to transfer the iterable list of elements through the response callback. |
renderItem | Function | see note column | The function of implementing the display of a single list item. By default, they simply create DIV. The parent attribute of the element must have the data-val attribute defined, this value will be substituted in the input field, and also this.itemClass class. |
onSelect | Function | void 0 | The callback that is called when the list item is selected. |