1.5.0 • Published 5 years ago

vanilla-js-autocomplete v1.5.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

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.

GitHub tag (latest by date) GitHub stars GitHub issues GitHub forks

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

nametypedefaultnote
minCharsInteger3Number of characters from which autocomplete is activated.
delayInteger150Delay (ms) after entering a character.
offsetLeftInteger0Indent (px) to the left of the list from the input element.
offsetTopInteger1Indent (px) to the top of the list from the input element.
changeValueBooleantrueSubstitute the value of the active list item in the input field.
containerClassStringautocomplete-dropdownElement list class name.
itemClassStringautocomplete-itemList item class name.
createContainerFunctionsee note columnThe 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.
sourceFunctionrequireThe 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.
renderItemFunctionsee note columnThe 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.
onSelectFunctionvoid 0The callback that is called when the list item is selected.
1.5.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago