1.1.26 • Published 17 days ago

bootstrap5-autocomplete v1.1.26

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

Autocomplete for Bootstrap 4/5

NPM Downloads

How to use

An ES6 autocomplete for your input using standards Bootstrap 5 (and 4) styles.

No additional CSS needed!

import Autocomplete from "./autocomplete.js";
Autocomplete.init();

Server side support

You can also use options provided by the server. This script expects a JSON response with the following structure:

{
    "optionValue1":"optionLabel1",
    "optionValue2":"optionLabel2",
    ...
}

or

[
  {
    "value": "server1",
    "label": "Server 1"
  },
  ...
]

Simply set data-server where your endpoint is located. The suggestions will be populated upon init except if data-live-server is set, in which case, it will be populated on type. A ?query= parameter is passed along with the current value of the searchInput.

Data can be nested in the response under the data key (configurable with serverDataKey).

Options

Options can be either passed to the constructor (eg: optionName) or in data-option-name format.

NameTypeDescription
showAllSuggestionsBooleanShow all suggestions even if they don't match
suggestionsThresholdNumberNumber of chars required to show suggestions
maximumItemsNumberMaximum number of items to display
autoselectFirstBooleanAlways select the first item
ignoreEnterBooleanIgnore enter if no items are selected (play nicely with autoselectFirst=0)
updateOnSelectBooleanUpdate input value on selection (doesn't play nice with autoselectFirst)
highlightTypedBooleanHighlight matched part of the label
highlightClassStringClass added to the mark label
fullWidthBooleanMatch the width on the input field
fixedBooleanUse fixed positioning (solve overflow issues)
fuzzyBooleanFuzzy search
startsWithBooleanMust start with the string. Defaults to false (it matches any position).
fillInBooleanShow fill in icon.
preventBrowserAutocompleteBooleanAdditional measures to prevent browser autocomplete
itemClassStringApplied to the dropdown item. Accepts space separated classes.
activeClassesArrayBy default: "bg-primary", "text-white"
labelFieldStringKey for the label
valueFieldStringKey for the value
searchFieldsArrayKey for the search
queryParamStringKey for the query parameter for server
itemsArray | ObjectAn array of label/value objects or an object with key/values
sourcefunctionA function that provides the list of items
hiddenInputBooleanCreate an hidden input which stores the valueField
hiddenValueStringPopulate the initial hidden value. Mostly useful with liveServer.
clearControlStringSelector that will clear the input on click.
datalistStringThe id of the source datalist
serverStringEndpoint for data provider
serverMethodStringHTTP request method for data provider, default is GET
serverParamsString | ObjectParameters to pass along to the server. You can specify a "related" key with the id of a related field.
serverDataKeyStringBy default: data
fetchOptionsObjectAny other fetch options (https://developer.mozilla.org/en-US/docs/Web/API/fetch#syntax)
liveServerBooleanShould the endpoint be called each time on input
noCacheBooleanPrevent caching by appending a timestamp
debounceTimeNumberDebounce time for live server
notFoundMessageStringDisplay a no suggestions found message. Leave empty to disable
onRenderItemRenderCallbackCallback function that returns the label
onSelectItemItemCallbackCallback function to call on selection
onServerResponseServerCallbackCallback function to process server response. Must return a Promise
onServerErrorErrorCallbackCallback function to process server errors.
onChangeItemCallbackCallback function to call on change-event. Returns currently selected item if any
onBeforeFetchFetchCallbackCallback function before fetch
onAfterFetchFetchCallbackCallback function after fetch

Callbacks

RenderCallback ⇒ string

ParamType
itemObject
labelString
instAutocomplete

ItemCallback ⇒ void

ParamType
itemObject
instAutocomplete

ServerCallback ⇒ Promise

ParamType
responseResponse
instAutocomplete

ErrorCallback ⇒ void

ParamType
eError
signalAbortSignal
instAutocomplete

Tips

  • Use arrow down to show dropdown (and arrow up to hide it)
  • If you have a really long list of options, a scrollbar will be used
  • Access instance on a given element with Autocomplete.getInstance(myEl)
  • Use type="search" for your inputs to get a clear icon

Groups

You can have your items grouped by using the following syntax:

const src = [
  {
    group: "My Group Name",
    items: [
      {
        value: "...",
        label: "...",
      },
    ],
  },
];

Not using Bootstrap ?

This class does NOT depends on Bootstrap JS. If you are not using Bootstrap, you can simply implement the css the way you like it :-)

Demo

https://codepen.io/lekoalabe/pen/MWXydNQ or see demo.html

Custom element

You can now use this as a custom element as part of my Formidable Elements collection.

Browser supports

Modern browsers (edge, chrome, firefox, safari... not IE11). Add a warning if necessary.

Also check out

1.1.26

17 days ago

1.1.19

10 months ago

1.1.18

10 months ago

1.1.17

10 months ago

1.1.23

6 months ago

1.1.22

8 months ago

1.1.20

9 months ago

1.1.25

5 months ago

1.1.24

5 months ago

1.1.16

10 months ago

1.1.15

10 months ago

1.1.14

11 months ago

1.1.9

12 months ago

1.1.8

12 months ago

1.1.12

11 months ago

1.1.10

11 months ago

1.1.13

11 months ago

1.1.7

12 months ago

1.1.6

12 months ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago