Giter Club home page Giter Club logo

emberx-select's Introduction

emberx-select

npm version Ember Observer Score Build Status

A Select component based on the native html select.

Why a select component? Because Ember.Select is hard to use, and really, all you want to do is just use a <select> tag dammit. The only difference is that you want value of the options to be any object and not just a string.

By allowing arbitrary html to appear in the template of the select element, you can use it just like you would normally. This means things like having <optgroup> tags inside your select, or even plain old <option> elements to represent things like empty values.

XSelect thinly wraps a native <select> element so that it can be object and binding aware. It is used in conjuction with the x-option component to construct select boxes. E.g.

{{#x-select value=bob action="selectPerson"}}
  {{#x-option value=fred}}Fred Flintstone{{/x-option}}
  {{#x-option value=bob}}Bob Newhart{{/x-option}}
{{/x-select}}

the options are always up to date, so that when the object bound to value changes, the corresponding option becomes selected.

Whenever the select tag receives a change event, it will fire action

Multiselect

As of version 1.1.0, emberx-select supports the multiple option. This means you can pass an array as its value, and it will set its selections directly on that array.

{{#x-select value=selections multiple=true action="selectionsChanged"}}
 {{#x-option value=fred}}Fred Flintstone{{/x-option}}
 {{#x-option value=bob}}Bob Newhart{{/x-option}}
 {{#x-option value=andrew}}Andrew WK{{/x-option}}
{{/x-select}}

The selections array will be initialized to an empty array if not present.

Heads Up! This will mutate the contents of your value array as the user changes their selections. This can lead to strange behavior and inconsistencies if you are using computed arrays and/or ember-data hasMany relationships. Just remember, you can't go wrong if you use just a simple array.

Test Helpers

Since emberx-select uses internal identifiers as the value attribute, it doesn't integrate with the fillIn test helper. But don't fret, we've automatically injected the test helper into your app.

Using the test helper

As of version 1.1.3 we support both multiselects and regular selects. To use you need to specify the class on the on the select as the first argument and the rest of the arguments are the options you'd like to select. For example:

//... Single select
  select('.my-drop-down', 'My Option');
//...

Multiselect

//... Multiselect
  select('.my-drop-down', 'My Option', 'My Option Two', 'My Option Three');
//...

Why am I getting a JSHint error?

You need to run the generator: ember g emberx-select

Why am I getting a "Can't find variable: select" error?

You need to either run the generator (ember g emberx-select) or import the test helper into your test-helper.js file:

import registerSelectHelper from './helpers/register-select-helper';
registerSelectHelper();

Blockless Form

As of version 1.1.2, x-select can be invoked in a blockless form which is API compatible with Ember.SelectView. While most of the time you want to use it in block-form, there are some cases where it makes more sense to specify your select on a single line. Also, it makes a more incremental approach to migrating from SelectView possible.

{{x-select action="tagYouAreIt" disabled=isDisabled
  multiple=true
  content=folks
  selection=it
  optionValuePath="content.id"
  optionLabelPath="content.name"}}

Action and Action Arguments

The action that is dispatched by x-select whenever the selected value or values change has a function signature of:

/**
* @param value {Object} the value selected by the user.
* @param component {Ember.Component} the x-select component itself
*/
function (value, component) {
  // action body...
}

Most of the time all you need is the value that has been selected, but sometimes your action requires more context than just that. In those cases, you can associate arbitrary attributes with the component itself and use them later inside your action handler. For example:

{{#x-select action="didMakeSelection" default=anything}}
  <option>Nothing</option>
  {{#x-option value=something}}Something{{/x-option}}
{{/x-select}}

then, inside your action handler:

export default Ember.Route.extend({
  actions: {
    didMakeSelection: function(selection, component) {
      if (selection) {
        this.set('selection', selection)
      } else {
        this.set('selection', component.get('default'))
      }
    }
  }
});

EmberX

emberx-select is part of the "missing components of ember" collectively known as emberx. See also:

Installation

ember install emberx-select

Running Tests

  • ember test
  • ember test --server

emberx-select's People

Contributors

abulrim avatar cowboyd avatar danshapir avatar ember-tomster avatar enspandi avatar fivetanley avatar kategengler avatar miguelcobain avatar mitchlloyd avatar odoe avatar robdel12 avatar seanpdoyle avatar stefanpenner avatar

Watchers

 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.