All Projects → afcapel → Stimulus Autocomplete

afcapel / Stimulus Autocomplete

Licence: mit
Stimulus autocomplete component

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Stimulus Autocomplete

React Multi Select
A Multi Select component built with and for React
Stars: ✭ 111 (-32.73%)
Mutual labels:  component, frontend
Github Buttons
Unofficial github:buttons.
Stars: ✭ 821 (+397.58%)
Mutual labels:  component, frontend
San
A fast, portable, flexible JavaScript component framework
Stars: ✭ 4,514 (+2635.76%)
Mutual labels:  component, frontend
Reactpatterns
Patterns for React Developers
Stars: ✭ 1,682 (+919.39%)
Mutual labels:  component, frontend
Devmap
Карта развития веб-разработчика
Stars: ✭ 2,043 (+1138.18%)
Mutual labels:  frontend
React Console Emulator
👨‍💻 A simple, powerful and highly customisable Unix terminal emulator for React.
Stars: ✭ 160 (-3.03%)
Mutual labels:  component
Vue3 Draggable Resizable
[Vue3 组件] 用于拖拽调整位置和大小的的组件,同时支持冲突检测,元素吸附对齐,实时参考线。
Stars: ✭ 159 (-3.64%)
Mutual labels:  component
Userbase
Create secure and private web apps using only static JavaScript, HTML, and CSS.
Stars: ✭ 2,029 (+1129.7%)
Mutual labels:  frontend
Requirejs Demo
《RequreJS学习笔记》
Stars: ✭ 164 (-0.61%)
Mutual labels:  component
Xmpp.js
XMPP for JavaScript
Stars: ✭ 2,006 (+1115.76%)
Mutual labels:  component
React Native Modals
A react native modals library. Swipeable. Highly customizable. Support multi modals & Support custom animation. For IOS & Android.
Stars: ✭ 2,013 (+1120%)
Mutual labels:  component
Crana
A CLI tool to create React + Node apps with just one command
Stars: ✭ 160 (-3.03%)
Mutual labels:  frontend
Learning Pwa
📱some samples and blogs about how to start with your first PWA
Stars: ✭ 162 (-1.82%)
Mutual labels:  frontend
Dcoaboutwindow
👋 A replacement for the standard Mac app About dialog.
Stars: ✭ 159 (-3.64%)
Mutual labels:  component
Fe Interview
宇宙最强的前端面试指南 (https://lucifer.ren/fe-interview)
Stars: ✭ 2,284 (+1284.24%)
Mutual labels:  frontend
Ansible Cmdb
Ansible-cmdb takes the output of Ansible's fact gathering and converts it into a static HTML overview page (and other things) containing system configuration information.
Stars: ✭ 2,003 (+1113.94%)
Mutual labels:  frontend
Hyperdom
A fast, feature rich and simple framework for building dynamic browser applications.
Stars: ✭ 161 (-2.42%)
Mutual labels:  frontend
Meli
Platform for deploying static sites and frontend applications easily. Automatic SSL, deploy previews, reverse proxy, and more.
Stars: ✭ 2,125 (+1187.88%)
Mutual labels:  frontend
Fe
《我的职业是前端工程师》 - Ebook:I'm a FrontEnd Developer
Stars: ✭ 1,970 (+1093.94%)
Mutual labels:  frontend
Vue Notifyjs
Minimalist 1kb Notification component
Stars: ✭ 160 (-3.03%)
Mutual labels:  component

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.

Demo

Installation

yarn add stimulus-autocomplete

Usage

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

import { Application } from 'stimulus'
import { definitionsFromContext } from 'stimulus/webpack-helpers'
import { Autocomplete } from 'stimulus-autocomplete'

const application = Application.start()

const context = require.context('controllers', true, /.js$/)
application.load(definitionsFromContext(context))

application.register('autocomplete', Autocomplete)

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

<div data-controller="autocomplete" data-autocomplete-url-value="/birds/search">
  <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>

Events

Events on the main element that registered the controller:

  • autocomplete.change fires when the users selects a new value from the autocomplete field. The event detail contains the value and textValue properties of the selected result.
  • loadstart fires before the autocomplete fetches the results from the server.
  • load fires when results have been successfully loaded.
  • error fires when there's an error fetching the results.
  • loadend fires when the request for results ends, successfully or not.
  • toggle fires when the results element is shown or hidden.

Events on the optional hidden input:

  • input and change dispatched to it when the users selects a new value from the autocomplete. This allows you to bind subsequent behavior directly to the <input type=hidden> element.

Optional parameters

  • min-length set the minimum number of charaters required to make an autocomplete request.

  • submit-on-enter submit the form after the autocomplete selection via enter keypress.

  • data-autocomplete-label can be used to define the input label upon selection. That way your option elements can have more elaborate markup, i.e.:

    <li class="list-group-item" role="option" data-autocomplete-value="1" data-autocomplete-label="Blackbird">
      <p>Blackbird</p>
      <p class="text-muted"><small>That's also the name of an airplane</small></p>
    </li>
    

Optional HTML configuration

  • If the <input> target has an autofocus attribute then the input will be given focus immediately so the user can start typing. This is useful if the <input> is dynamically added/morphed into the DOM (say by a "edit" button) and the user expects to start typing immediately.

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. Build the package with npm run build

  3. Publish the package to npmjs.com with npm publish

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].