Giter Site home page Giter Site logo

sitback / ember-cli-auto-complete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from toranb/ember-cli-auto-complete

0.0 15.0 0.0 170 KB

ember-cli addon that provides type-ahead selection for text inputs

License: MIT License

JavaScript 90.56% HTML 9.44%

ember-cli-auto-complete's Introduction

ember-cli-auto-complete

Build Status

Description

ember-cli addon that provides type-ahead selection for text inputs (requires ember.js 1.11+)

Demo

http://emberjs.jsbin.com/hohegogizi/1/

Installation

npm install ember-cli-auto-complete --save-dev

How to use this component

Add a custom component that extends AutoComplete, e.g. with ember-cli:

ember generate component my-auto-complete.

Note: if you use use ember-cli to generate the component, it will create a template file e.g. my-auto-complete.hbs. Delete this if you don't intend to use it.

In this component you need to set the valueProperty property and implement suggestions:

  1. valueProperty this string should be the value property for the options passed in (think selectbox value/label)
  2. determineSuggestions this function will determine how the list of options is filtered as the user enters text (it gets passed the available options and the users input). This function should return an array.

e.g.

import AutoComplete from "ember-cli-auto-complete/components/auto-complete";

export default AutoComplete.extend({
  valueProperty: "code",
  determineSuggestions: function(options, input) {
      var list = options.filter(function(item) {
          return item.get("code").toLowerCase().indexOf(input.toLowerCase()) > -1;
      });
      return Ember.A(list);
  }
});

Next add the component to your template including a block with html for the options (requires ember 1.11)

{{#my-auto-complete options=codes inputClass="foobar" selectedValue=model.code placeHolderText="Find a thing" noMesssagePlaceHolderText="No things are found" as |result|}}
  <p><b>{{result.code}}</b>{{result.text}}</p>
{{/my-auto-complete}}

Finally prepare a list of options for the component in the route or controller

var Foo = Ember.Object.extend({});
var Bar = Ember.Object.extend({code: ""});

export default Ember.Route.extend({
    model: function() {
        var codes = [];
        codes.pushObject(Foo.create({code: "ABC", text: "SOMETHING 1"}));
        codes.pushObject(Foo.create({code: "ABCD", text: "SOMETHING 2"}));
        codes.pushObject(Foo.create({code: "ABCDE", text: "SOMETHING 3"}));
        return Ember.RSVP.hash({
            model: Bar.create(),
            codes: codes
        });
    },
    setupController: function(controller, hash) {
        controller.set("model", hash.model);
        controller.set("codes", hash.codes);
    }
});

Handling item selected

If you would like to call an action every time an elements is selected just bind the action through the attribute selectItem.

{{#my-auto-complete
   options=codes
   selectedValue=model.code
   placeHolderText="Find a thing"
   inputClass="my-fun-input-thing andTwo"
   noMesssagePlaceHolderText="No things are found"
   selectItem="itemSelected" as |result|}}
   <p><b>{{result.code}}</b>{{result.text}}</p>
{{/my-auto-complete}}
<p class="selection">{{controller.selection}}</p>

In the example above the action itemSelected will be called with the selected item, bubbling through your routes and controllers.

  actions: {
    itemSelected: function(item) {
      this.get('controller').set('selection', item.get('code'));
    }
  }

Styling Attributes

If you would like to hide the dropdown when there are no suggestions, set the hideWhenNoSuggestions attribute.

{{#my-auto-complete
   ...
   hideWhenNoSuggestions=true
   ...             }}

When you set this options, you will generally not want to set the noMesssagePlaceHolderText attribute.

Running the unit tests

npm install
bower install
ember test

Example project built in

1) npm install
2) bower install
3) ember server
4) localhost:4200

Credits

First I'd like to thank Nick Christus for the design work that made this great component happen to begin with. Next I'd like to thank Charlie for his amazing project ember-cli-suggest as this project truly represents a fork of his work.

License

Copyright © 2015 Toran Billups http://toranbillups.com

Licensed under the MIT License

ember-cli-auto-complete's People

Contributors

abuiles avatar benkiefer avatar ember-tomster avatar gte451f avatar joe-l-bright avatar kagemusha avatar stravid avatar toranb avatar williamsbdev avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.