An autocomplete component. Couldn't get others to work how I want it to, so I made my own.
Prior art:
Some philosophicaly differences:
- Mobile first - so touching outside the menu/input closes the menu. Tap support!
- No blur events - alt-tabbing or clicking outside the viewport won't close the menu
- The menu doesn't close when selected - you have to do it yourself
- Queryless - includes no query structure. You must implement querying yourself!
- Progressive enhancement - doesn't create the
<input>
element dynamically
make
open test/index.html
You can also run the tests by:
npm i -g component-test
make test
.container
.Autocomplete-menu(.Autocomplete-hidden)
.Autocomplete-option(.Autocomplete-highlighted)
.Autocomplete-option(.Autocomplete-highlighted)
.Autocomplete-option(.Autocomplete-highlighted)
input[type="text"]
Note that .Autocomplete-menu
has position: absolute;
by default, so you should have a non-statically positioned parent somewhere.
Also note that this structure is completely optional - you can place the menu wherever you'd like.
element
is the <input>
element to enhance. menu
is an optional menu element to use - by default it creates its own and appends it before the input.
You need to set your own query function. Your query function should look something like this:
search.query = function (text) {
// remove all the options
search.clear()
ajax(function (err, options) {
// do your own error handling
if (err) return console.error(err.stack);
// push all the options to the menu
search.push(options)
// highlight the first option
search.highlight(0)
})
}
Each option
should be an {}
with an .id
as well as either a .title
or a .name
. Each option
could also be a string, which will automatically be converted to an object.
Customize the formatting of every option. By default, it is:
search.format = function (option) {
return '<div>' + (option.name || option.title) + '</div>'
}
You may return either an HTML string or a DOM element.
Push options to the menu.
Remove all the options from the menu.
Selection option, if any.
When an option is selected
Currently highlighted option.
Hide the menu.
When the menu is hidden
Show the menu.
When the menu is shown
When an option is highlighted
IE9+ - PRs for IE8 welcomed.
The MIT License (MIT)
Copyright (c) 2014 Jonathan Ong [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.