From 80f28167ab7da96b7e4d9a69580bea714ac9ede4 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 3 Jan 2021 23:46:02 +0300 Subject: initial commit --- README.md | 60 ++++++++++++++++++++++++++ autocomplete.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 README.md create mode 100644 autocomplete.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa67f9b --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# bootstrap-5-autocomplete + +This is a rewrite of https://github.com/Honatas/bootstrap-4-autocomplete for bootstrap v5. + +### Example + +```js +const ac = new Autocomplete(field, { + data: [{label: "I'm a label", value: 42}], + maximumItems: 5, + onSelectItem: ({label, value}) => { + console.log("user selected:", label, value); + } +}); + +// later, when you need to change the dataset + +ac.setData([ + {label: 'New York JFK', value: 'JFK'}, + {label: 'Moscow SVO', value: 'SVO'}, +]); +``` + +### Options + +Options is a JSON object with the following attributes (in alphabetical order): + +**data**: +The data from where autocomplete will lookup items to show. This data has to be an array of JSON objects. The format for every item in the array is: + + {"label": "This is a text", "value": 42} + +**dropdownOptions**: +It's the same options from Bootstrap's Dropdown, documented [here](https://getbootstrap.com/docs/5.0/components/dropdowns/#options). + +**dropdownClass**: +The class of the dropdown-menu element, which is the box that is displayed. Can take a string or an array of strings. + +**highlightClass**: +The class to use when highlighting typed text on items. Only used when highlightTyped is true. Default is text-primary. Can take a string or an array of strings. + +**highlightTyped**: +Wether to highlight (style) typed text on items. Default is true. + +**maximumItems**: +How many items you want to show when the autocomplete is displayed. Default is 5. Set to 0 to display all available items. + +**onInput** + +**onSelectItem**: +A callback that is fired every time an item is selected. It receives an object in following format: + + {label: