GithubHelp home page GithubHelp logo

timaschew / autocomplete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonathanong/autocomplete

0.0 1.0 0.0 187 KB

mobile-first autocomplete

Makefile 1.04% CSS 3.01% JavaScript 74.38% HTML 21.57%

autocomplete's Introduction

Autocomplete

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

Demo

make
open test/index.html

You can also run the tests by:

npm i -g component-test
make test

API

.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.

var search = autocomplete(element, [menu])

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.

search.query=

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.

search.format=

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.

search.push(options...)

Push options to the menu.

search.clear()

Remove all the options from the menu.

search.selected

Selection option, if any.

Event: change

When an option is selected

search.highlighted

Currently highlighted option.

search.hide()

Hide the menu.

Event: hide

When the menu is hidden

search.show()

Show the menu.

Event: show

When the menu is shown

Event: highlight

When an option is highlighted

Browser Compatibility

IE9+ - PRs for IE8 welcomed.

License

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.