• Stars
    star
    200
  • Rank 195,325 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Select component for Ember based on the native html select element.

emberx-select

npm version Ember Observer Score CircleCI

A select component based on the native html select.

We've tried other select components, and were missing the reliability, maintainability, and accessbility of the native html <select>. <XSelect> is a drop-in component to let you use any object for your selectable options. You can use it out of the box, or as a building block of something more ambitious.

The goal of <XSelect> is to let you see how it works and style it right in your template, rather than passing in a ball of configuration or wrapping a hard-coded, inaccessible jQuery plugin.

Installation

ember install emberx-select

Usage

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.

Ember >= 3.4:

<XSelect @value={{bob}} @onChange={{action "selectPerson"}} as |xs|>
  <xs.option @value={{fred}}>Fred Flintstone</xs.option>
  <xs.option @value={{bob}}>Bob Newhart</xs.option>
</XSelect>

Ember < 3.4:

{{#x-select value=bob on-change=(action "selectPerson") as |xs|}}
  {{#xs.option value=fred}}Fred Flintstone{{/xs.option}}
  {{#xs.option value=bob}}Bob Newhart{{/xs.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 onChange action. This is the default action that is fired but not the only event that's available.

Contextual Components

As of version 3.0.0, emberx-select will only support contextual components. This means you will have to use Ember 2.3 or higher. Using contextual components allows emberx-select to skip some potentially expensive DOM traversals. Now the options can register through data rather than through the DOM.

<XSelect @value={{model.status}} as |xs|>
  <xs.option @value=1>Active</xs.option>
  <xs.option @value=2>Inactive</xs.option>
</XSelect>

Multiselect

<XSelect> supports the multiple option. This means you can pass an array as its value, and it will set its selections directly on that array.

<XSelect @value=selections @multiple=true @onChange={{action "selectionsChanged"}} as |xs|>
 <xs.option @value={{fred}}>Fred Flintstone</xs.option>
 <xs.option @value={{bob}}>Bob Newhart</xs.option>
 <xs.option @value={{andrew}}>Andrew WK</xs.option>
</XSelect>

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

Actions and Action Arguments

All of <XSelect>s actions are closure actions. This means you must use the action helper (i.e. @onClick={{action "onClick"}}). The function that is dispatched by <XSelect> whenever the event fires has a function signature of:

/**
* @param {Object} value - the value selected by the user.
* @param {Object} event - the DOM event of the action
*/
function (value, event) {
  // 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 pass any arguments you need from the template. For example:

<XSelect @onClick={{action "didMakeSelection" isXSelectRequired}} @required={{isXSelectRequired}} as |xs|>
  <option>Nothing</option>
  <xs.option @value={{something}}>Something</xs.option>
</XSelect>

then, inside your action handler:

import Controller from '@ember/controller';

export default Controller.extend({
  actions: {
    didMakeSelection(value, event, isXSelectRequired) {
      if (!value & isXSelectRequired) {
        this.set('error', 'You must fill out this field');
      } else {
        this.set('selection', value);
      }
    }
  }
});

<XSelect> provides other actions that fire on different event types. These actions follow the HTML input event naming convention.

onBlur

onBlur fires anytime the blur event is triggered on the <XSelect> component. When the action fires it sends two arguments: the value, the DOM event.

onFocusOut

onFocusOut fires anytime the focusOut event is triggered on the <XSelect> component. When the action fires it sends two arguments: the value, the DOM event.

onClick

onClick fires when <XSelect> is clicked. When the action fires it sends two arguments: the value, the DOM event.

onDisable (x-option)

onDisable fires when x-option detects a change to its disabled attribute. When the action fires it sends two arguments: the value and if it is disabled (boolean).

Test Helper

<XSelect> 4.0 ships with an entirely new test helper that goes beyond just allowing you to select an option. It allows you to interact with your <select> element in all different ways. For example, if you need to assert your first option is disabled or not:

expect(xselect.options(0).isDisabled).to.equal(true);

Under the hood this new test helper is using a BigTest Interactor. Interactors allow you to think about how you're going to interact with the DOM and abstract that into composable & immutable containers. Interactors are similar to page objects, but for components.

Using the test helper

Import the select interactor:

// you can name the import whatever you want
import XSelectInteractor from 'emberx-select/test-support/interactor';

At the top of your test file you need to initialize the interactor. This should go at the top most part of your test so it's available to all tests in the file. Here's an example in Qunit:

module("Acceptance | Your Test", function(hooks) {
  let xselect = new XSelectInteractor('.selector-for-select');
  setupApplicationTest(hooks);
  // ...
});

Once you have initialized the interactor, you're ready to start selecting!

module("Acceptance | Your Test", function(hooks) {
  let xselect = new XSelectInteractor('.selector-for-select');
  // ...

  test('Selecting an option', async (assert) => {
    await xselect
      .select('Fred Flintstone')
      .when(() => assert.equal(xselect.options(0).isSelected, true));

    // for a multiselect pass an array
    // await xselect
    //   .select(['Fred Flintstone', 'Bob Newhart'])
    //   .when(() => assert.equal(xselect.options(0).isSelected, true));;
  });
});

You can do more than just select options with this helper.

module('Acceptance | Your Test', function(hooks) {
  let xselect = new XSelectInteractor('.selector-for-select');
  // ...

  test('Selecting an option', async (assert) => {
    await xselect.select('Fred Flintstone')
      // assert the change is has happened. It's important to make the
      // assertion inside of `when`, so tests are not flakey.
      .when(() => assert.equal(xselect.options(0).isSelected, true));
  });
});

In this example we're using @bigtest/convergence#when to assert. The TL;DR of convergence is it basically converges on the state of the DOM. It checks every 10ms until the assertion is truthy. Once it's truthy the test passes. You can read more about convergences here

You don't need to include @bigtest/convergence in your project, it's already a dependency of @bigtest/interactor and interactor provides all of the convergence methods to you (like when and do).

This is the full interactor which has all of the attributes or interactions for an HTMLSelectElement.

const xSelectInteractor = interactor({
  hasFocus: is(':focus'),
  name: attribute('name'),
  form: attribute('form'),
  title: attribute('title'),
  size: attribute('size'),
  tabindex: attribute('tabindex'),
  isDisabled: property('disabled'),
  isRequired: property('required'),
  isAutofocus: property('autofocus'),

  options: collection('option', {
    name: attribute('name'),
    value: property('value'),
    title: attribute('title'),
    isSelected: property('selected'),
    isDisabled: property('disabled'),
    hasSelectedClass: hasClass('is-selected')
  })
});

Example usage might be:

<select name="World" class="x-select">
  <option value="hello world">Hello world!</option>
</select>
let xselect = new XSelectInteractor('.x-select');

xselect.options(0).value; //=> "hello world"
xselect.options(0).text; //=> "Hello World!"
xselect.name; //=> "World"
xselect.form; //=> null
xselect.hasFocus; //=> false
xselect.tabIndex; //=> 0

If you want to see this test helper used in many different ways look no further than this addons test suite!

Extending the XSelect interactor

If you want to add custom interactions to your <XSelect> interactor, you can do so by importing it into the custom interactor you want to create, and extend it:

import XSelectInteractor from 'emberx-select/test-support/interactor';
import { clickable } from '@bigtest/interactor';

@XSelectInteractor.extend
class NewInteractor {
  submitForm = clickable('[data-test-form-submit]');

  fillAndSubmit(value) {
    return this.select(value).submitForm();
  }
}

EmberX

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

Other Resources

Running Tests

  • ember test
  • ember test --server

Release Process

Every commit to master results in a build and push to the demo application at http://emberx-select.netlify.com

Npm releases use semver and happen at the project owner's discretion.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms, which can be found in the CODE_OF_CONDUCT.md file in this repository.

More Repositories

1

ember-electron

⚑ Build, test, compile and package desktop apps with Ember and Electron
JavaScript
806
star
2

ember-cp-validations

Ember computed property based validations
JavaScript
443
star
3

ember-changeset

Ember.js flavored changesets, inspired by Ecto
JavaScript
431
star
4

ember-moment

JavaScript
399
star
5

ember-infinity

⚑ Simple, flexible Infinite Scroll for Ember CLI Apps.
JavaScript
377
star
6

ember-data-model-fragments

Ember Data addon to support nested JSON documents
JavaScript
371
star
7

ember-metrics

Send data to multiple analytics integrations without re-implementing new API
JavaScript
367
star
8

ember-cli-flash

Simple, highly configurable flash messages for ember-cli
JavaScript
355
star
9

ember-light-table

Lightweight, contextual component based table for Ember
JavaScript
312
star
10

ember-data-factory-guy

Factories and helper functions for (unit, integration, acceptance) testing + development scenarios with Ember Data
JavaScript
302
star
11

ember-sortable

Sortable UI primitives for Ember.js
JavaScript
295
star
12

ember-burger-menu

An off-canvas sidebar component with a collection of animations and styles using CSS transitions
JavaScript
279
star
13

ember-cli-sass

Use node-sass to preprocess your ember-cli app's files, with support for sourceMaps and include paths
JavaScript
276
star
14

ember-notify

Notification messages for your Ember.js app
JavaScript
264
star
15

ember-collection

An efficient incremental rendering component for Ember.js with support for custom layouts and large lists
JavaScript
236
star
16

ember-changeset-validations

Validations for ember-changeset
JavaScript
219
star
17

ember-file-upload

File uploads for Ember apps
TypeScript
201
star
18

ember-keyboard

An Ember.js addon for the painless support of keyboard events
JavaScript
177
star
19

ember-pikaday

A datepicker component for Ember CLI projects.
JavaScript
159
star
20

ember-impagination

An Ember Addon that puts the fun back in asynchronous, paginated datasets
JavaScript
122
star
21

active-model-adapter

Adapters and Serializers for Rails's ActiveModel::Serializers
TypeScript
102
star
22

ember-cli-hot-loader

An early look at what hot reloading might be like in the ember ecosystem
JavaScript
99
star
23

ember-autoresize

Autoresize for Ember Components
JavaScript
88
star
24

ember-cli-windows

🚀 Improve Ember-Cli Performance on Windows
JavaScript
79
star
25

ember-set-helper

A better `mut` helper
JavaScript
68
star
26

ember-inputmask

Ember wrapper around Inputmask.js
JavaScript
68
star
27

ember-collapsible-panel

An unopinionated, zero-dependency panel and accordion
JavaScript
57
star
28

ember-qunit-nice-errors

Because expected true, result false is not enough!
JavaScript
56
star
29

ember-cli-ifa

Ember CLI addon for injecting fingerprinted asset map file into Ember app
JavaScript
54
star
30

emberx-file-input

A tiny Ember component which does one thing and only: select files beautifully.
JavaScript
52
star
31

ember-cognito

AWS Amplify/Cognito and ember-simple-auth integration
JavaScript
32
star
32

ember-validators

A collection of EmberJS validators
JavaScript
24
star
33

ember-cli-bugsnag

Integrates Bugsnag reporting service into your Ember CLI app.
JavaScript
20
star
34

ember-stripe-elements

A simple Ember wrapper for Stripe Elements
JavaScript
18
star
35

ember-popper-modifier

An Ember modifier for working with Popper.js.
TypeScript
14
star
36

ember-launch-darkly

A modern Ember addon to wrap the Launch Darkly service
JavaScript
13
star
37

ember-require-module

Dynamically require modules
JavaScript
7
star
38

ember-indexeddb-adapter

Ember Data adapter for IndexedDB
JavaScript
4
star
39

program-guidelines

Checklist and guidelines for the Adopted Ember Addons org.
JavaScript
3
star
40

ember-cli-deploy-new-relic-sourcemap

Ember CLI Deploy plugin to deploy source maps to new relic
JavaScript
2
star