Socket
Socket
Sign inDemoInstall

ember-select-box

Package Overview
Dependencies
187
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ember-select-box

A faux select box for Ember apps


Version published
Weekly downloads
117
increased by112.73%
Maintainers
1
Install size
14.7 MB
Created
Weekly downloads
 

Changelog

Source

3.0.7

  • Add blurInput to API

Readme

Source

ember-select-box

         

Select box solutions are rarely perfect for what you want.

They come with a myriad of options to configure every possible situation, and they make too many assumptions about how your select-box behaves.

This addon does less, and gives you the primitives to easily compose your own.

Installation

ember install ember-select-box

Native select box

Read more

{{#select-box/native as |sb|}}
  {{sb.option value=1 label='One'}}
  {{sb.option value=2 label='Two'}}
  {{sb.option value=3 label='Three'}}
{{/select-box/native}}
Faux Select box
{{#select-box as |sb|}}
  {{sb.option value=1 label='One'}}
  {{sb.option value=2 label='Two'}}
  {{sb.option value=3 label='Three'}}
{{/select-box}}
View attributes
AttributeDescription
valueUsed to determine which option is selected, can be a promise
multipleIf true, `value` should be an array. Also adds an `is-multiple` class
disabledIf true adds an `is-disabled` class
is-openControls the open/closed state
on-open Fired when the select box is opened
on-close Fired when the select box is closed
on-initFired when the select box initialises. Useful opportunity to get access to the select box's API which is passed as a parameter.
on-select Fired when an option is clicked, or enter is pressed regardless as to whether the value changed or not.
Subsequently fired by use of the `select` API.
on-update Fired once upon initialisation of the select box (as its initial value is set).
Subsequently fired by use of the `update` API or if the value attribute changes.
on-searchFired when the select box decides to run a search
on-searchedFired after the last succesful search attempt
search-min-charsPrevents the on-search action from firing until there are enough chars (default 1)
search-delay-timeMilliseconds to debounce the on-search action from firing (default 100)
search-slow-timeMilliseconds considered for a search to be taking too long (default 500)
class-prefixAdds a prefix to the class name of all child select-box components
on-click-outsideUseful for closing the select box
on-focus-inFired when focus enters the select box, normalised if it contains an input
on-focus-outFired when focus leaves the select box
on-press-backspace
on-press-tab
on-press-enterUseful for preventing default action of event
on-press-escapeUseful for closing and/or resetting a select box
on-press-leftUseful for navigating multi-select boxes
on-press-upUseful for navigating up
on-press-rightUseful for navigating multi-select boxes
on-press-downUseful for navigating down
View yielded API
PropertyDescription
sb.isSearchingWhether the promise returned from the `on-search` action is running
sb.isSlowSearchTrue if the promised search results are taking a while
sb.isOpenTrue if the select box is open
sb.openOpens the select box, adding `is-open` class name
sb.closeCloses the select box removing the `is-open` class name
sb.toggleOpens or closes the select box
sb.selectSelects an arbitrary value and fires the `on-select` action
sb.updateUpdates the selected value, but does not fire the `on-select` action
sb.selectActiveOptionSelects the value of whichever option is currently active
sb.searchRuns an arbitrary search using the search function provided by `on-search`
sb.stopSearching'Cancels' searches currently in progress (even though promises are not cancelable)
sb.setInputValueLets you update the input value, useful for when a selection has been made
sb.focusInputFocuses the input associated with the select box
sb.blurInputUnfocuses the input associated with the select box
sb.activateOptionAtIndexAdds an `is-active` class to the option at the index
sb.activateNextOptionActivates the next option (pass in true to scroll if necessary too)
sb.activatePreviousOptionAs above but reverse
sb.deactivateOptionsMakes no option be active
sb.activateSelectedOptionAtIndexActivates the selected option at the index
sb.activateNextSelectedOptionActivates the next selected option
sb.activatePreviousSelectedOptionAs above but reverse
sb.deactivateSelectedOptionsMakes no selected option be active

Option
{{sb.option value=1 label='One'}}

{{#sb.option value=2 label='Two' as |o|}}
  {{o.label}}
{{/sb.option}}
View attributes
AttributeDescription
selectedUsed for manually selecting an option. (Most of the time you won't need to use this because the options automatically know whether or not they are selected based on the value attrbute set on the select box component itself)
name
title
tabindex
disabled
size
tabindex
multiple
aria-label
style
valueCan be anything, including a promise
labelUsed as the display text by default
on-selectUseful for firing one-off actions when an option is selected
on-activateFired when an individual option is activated
View yielded API
PropertyDescription
o.selectedWhether or not the option is currently selected
o.valueThe value of the option
o.labelThe label of the option
o.indexThe index of the option amongst the options
o.disabledWhether or not the option is currently disabled

Group

Self explanitory, just wraps the options in extra markup.

{{#sb.group label='Things'}}
  {{sb.option value=thing label=thing.name}}
{{/sb.group}}

Options

You only need to wrap the options up in with sb.options if you require extra markup for styling, or you want the options to be navigatable.

{{#sb.options}}
  {{sb.option value=1 label='One'}}
  {{sb.option value=2 label='Two'}}
{{/sb.options}}
View attributes
AttributeDescription
styleUseful for customising the style of the options container

Input

Allows you to input text into the select box, usually for running searches/filtering

{{sb.input}}
View attributes
AttributeDescription
typeSets the type of input text/search etc...
value
size
autofocus
placeholder
readonly
disabled
on-inputFired when text is input
on-deleteFired when there is no text present, but backspace is pressed
on-clearFired when text is cleared completely

Selected option

Does not render the user's selected option automatically, but rather just provides a way for you to render the option(s) that have been selected.

{{sb.selected-option value=1 label='One'}}

{{#sb.selected-option value=2 label='Two' as |so|}}
  {{so.label}}
{{/sb.selected-option}}
View attributes
AttributeDescription
title
style
on-activateFired when a selected option is activated

Selected options
{{#sb.selected-options}}
  {{#sb.selected-option}}You chose this{{/sb.selected-option}}
  {{#sb.selected-option}}And this{{/sb.selected-option}}
{{/sb.selected-options}}

Provides a container for options that the user selected. Does not do anything by default, but it is possible to activate selected options using the API, thereby allowing you to create your own navigatable select box.

View attributes
AttributeDescription
styleUseful for one-off styling of selected options

API

The select boxes that come with this addon exposes an API to you as an argument to action handlers like so:

{{select-box on-select=(action 'selectedAnOption')}}
actions: {
  selectedAnOption(value, api) {
    api.close(); // for example.
  }
}

Customising

There are 3 ways to get what you want

  1. compose a new one
  2. OR extend an existing one
  3. OR create a new one from the mixins
Compose

It's recommended that you compose your own select box like so :

{{#select-box value=value on-select=on-select class-prefix='my-select-box' as |sb|}}}
  {{sb.selected-option label=sb.value.name}}
  <button onclick={{action sb.toggle}}>Toggle</button>
  {{yield sb}}
{{/select-box}}

...and then use it like this:

{{#my-select value=thing on-select=(action 'selectedAThing') as |sb|}}
  {{#each things as |thing|}}
    {{sb.option value=thing label=thing.name}}
  {{/each}}
{{/my-select}}

...will render...

<div class="my-select">
  <div class="my-select-box">
    <div class="my-select-box-selected-option">Foo</div>
    <button>Toggle</button>
    <div class="my-select-box-option is-selected">Foo</div>
    <div class="my-select-box-option">Bar</div>
    <div class="my-select-box-option">Baz</div>
  </div>
</div>
Extend

If you need even more flexibility, you can extend the select box:

const MySelectBox = SelectBox.extend({
  click() {
    this.send('toggle');
  }
})
Mixins

if you need even more flexibility you can create your own select box using the mixins

const MySelectBox = Component.extend(BaseSelectBox, Toggleable, Searchable);

Wormhole

ember-select-box works well with ember-wormhole. In most cases, this isn't needed - but it can be useful to render your <options>'s elsewhere in the DOM if you find yourself with overflow:hidden style issues for example.

{{#select-box class-prefix='worm-select' as |sb|}}
  {{#ember-wormhole to='destination'}}
    {{#sb.options}}
      {{yield sb}}
    {{/sb.options}}
  {{/ember-wormhole}}
{{/select-box}}

Native Select Box info

Rendering lots of components in Ember can be slow. If your select box only uses primitive values, you do not need to use {{sb.option}}, instead you can use a plain old <option> element [Example].


Things to note
  • With just a few lines of code you can create an autocompleter using the input component.
  • Thanks to contextual components there is not a truth helper in sight.
  • This project will never come with built-in styles.
  • The select box's primitives are available to you via the yielded API and as an argument to action handlers, so you should never feel held-back when creating your select box

FAQ

Q: Why aren't the native and faux select boxes two addons.
A: Less effort maintaining 1 addon. Splitting out would be trivial though.

Keywords

FAQs

Last updated on 03 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc