afcapel / stimulus-autocomplete

Stimulus autocomplete component
MIT License
482 stars 62 forks source link
autocomplete autocomplete-search component frontend javascript stimulus stimulusjs

Stimulus Autocomplete controller

This is a tiny stimulus controller (1.5kB gzipped) to make a selection from a list of results fetched from the server. See it in action.

Demo

Installation

If you are using a js bundler with node_modules support (such as esbuild, rollup.js or Webpack) install the package from npm:

yarn add stimulus-autocomplete

If you're using importmap-rails, you'll need to pin stimulus-autocomplete:

./bin/importmap pin stimulus-autocomplete

Usage

Load your stimulus application as usual and the register the autocomplete controller with it:

import { Application } from '@hotwired/stimulus'
import { Autocomplete } from 'stimulus-autocomplete'

const application = Application.start()
application.register('autocomplete', Autocomplete)

To use the autocomplete, you need some markup as this:

<div data-controller="autocomplete" data-autocomplete-url-value="/birds/search" role="combobox">
  <input type="text" data-autocomplete-target="input"/>
  <input type="hidden" name="bird_id" data-autocomplete-target="hidden"/>
  <ul class="list-group" data-autocomplete-target="results"></ul>
</div>

The component makes a request to the data-autocomplete-url to fetch results for the contents of the input field. The server must answer with an html fragment:

<li class="list-group-item" role="option" data-autocomplete-value="1">Blackbird</li>
<li class="list-group-item" role="option" data-autocomplete-value="2">Bluebird</li>
<li class="list-group-item" role="option" data-autocomplete-value="3">Mockingbird</li>

Note: class list-group on <ul> and list-group-item on <li> is required to apply the same css as displayed in the gif above.

Items can be included that are not selectable, such as help text or delimiters using aria-disabled attribute:

<li role="option" aria-disabled="true">Start typing to search...</li>

If the controller has a hidden target, that field will be updated with the value of the selected option. Otherwise, the search text field will be updated.

The height of the result list can be limited with CSS, e.g.:

<ul class="list-group" data-autocomplete-target="results" style="max-height: 10rem; overflow-y: scroll;"></ul>

If you want a custom query parameter name, use the data-autocomplete-query-param-value attribute.

<div data-controller="autocomplete" data-autocomplete-url-value="/birds/search" data-autocomplete-query-param-value="name" ...>

The above will setup will fetch the results from /bird/search?name=SEARCH_TEXT.

Events

Events on the main element that registered the controller:

Events on the optional hidden input:

Optional parameters

Optional HTML configuration

Extension points

URL building

The autcomplete default behaviour is to add a q querystring parameter to the the base data-autocomplete-url. If you need a different format, you can override the controllers buildURL method.

import { Application } from '@hotwired/stimulus'
import { Autocomplete } from 'stimulus-autocomplete'

const application = Application.start()

class CustomAutocomplete extends Autocomplete {
  buildURL(query) {
    return `${new URL(this.urlValue, window.location.href).toString()}/${query}`
  }
}

application.register('autocomplete', CustomAutocomplete)

Examples

Credits

Heavily inspired on github's autocomplete element.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/afcapel/stimulus-autocomplete. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Release a new version

To release a new version follow these steps:

  1. Update the version number in package.json. Try to follow semantic versioning guidelines as much as possible.

  2. Publish the package to npmjs.com with yarn run release

License

This package is available as open source under the terms of the MIT License.