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.
ember install ember-select-box
View attributes
Attribute | Description |
---|---|
value | Used to determine which option is selected |
multiple | If true, `value` should be an array |
disabled | If true adds an `is-disabled` class |
is-open | Controls the open/closed state |
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-search | Fired when the select box decides to run a search |
on-searched | Fired after the last succesful search attempt |
search-min-chars | Prevents the on-search action from firing until there are enough chars (default 1) |
search-delay-time | Milliseconds to debounce the on-search action from firing (default 100) |
search-slow-time | Milliseconds considered for a search to be taking too long (default 500) |
class-prefix | Adds a prefix to the class name of all child select-box components |
on-click-outside | Useful for closing the select box |
on-focus-in | Fired when focus enters the select box, normalised if it contains an input |
on-focus-out | Fired when focus leaves the select box |
on-press-backspace | |
on-press-tab | |
on-press-enter | Useful for preventing default action of event |
on-press-escape | Useful for closing and/or resetting a select box |
on-press-left | Useful for navigating multi-select boxes |
on-press-up | Useful for navigating up |
on-press-right | Useful for navigating multi-select boxes |
on-press-down | Useful for navigating down |
View yielded API
Property | Description |
---|---|
sb.isSearching | Whether the promise returned from the `on-search` action is running |
sb.isSlowSearch | True if the promised search results are taking a while |
sb.open | Opens the select box, adding `is-open` class name |
sb.close | Closes the select box removing the `is-open` class name |
sb.toggle | Opens or closes the select box |
sb.select | Selects an arbitrary value and fires the `on-select` action |
sb.update | Updates the selected value, but does not fire the `on-select` action |
sb.selectActiveOption | Selects the value of whichever option is currently active |
sb.search | Runs 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.setInputValue | Lets you update the input value, useful for when a selection has been made |
sb.focusInput | Focuses the input associated with the select box |
sb.activateOptionAtIndex | Adds an `is-active` class to the option at the index |
sb.activateNextOption | Activates the next option (pass in true to scroll if necessary too) |
sb.activatePreviousOption | As above but reverse |
sb.deactivateOptions | Makes no option be active |
sb.activateSelectedOptionAtIndex | Activates the selected option at the index |
sb.activateNextSelectedOption | Activates the next selected option |
sb.activatePreviousSelectedOption | As above but reverse |
sb.deactivateSelectedOptions | Makes no selected option be active |
View attributes
Attribute | Description |
---|---|
selected | Used 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 | |
value | Can be anything |
label | Used as the display text by default |
on-select | Useful for firing one-off actions when an option is selected |
on-activate | Fired when an individual option is activated |
View yielded API
Property | Description |
---|---|
o.selected | Whether or not the option is currently selected |
o.value | The value of the option |
o.label | The label of the option |
o.index | The index of the option amongst the options |
Self explanitory, just wraps the options in extra markup.
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.
View attributes
Attribute | Description |
---|---|
style | Useful for customising the style of the options container |
Allows you to input text into the select box, usually for running searches/filtering
View attributes
Attribute | Description |
---|---|
type | Sets the type of input text/search etc... |
value | |
size | |
autofocus | |
placeholder | |
readonly | |
disabled | |
on-input | Fired when text is input |
on-delete | Fired when there is no text present, but backspace is pressed |
on-clear | Fired when text is cleared completely |
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.
View attributes
Attribute | Description |
---|---|
title | |
style | |
on-activate | Fired when a selected option is activated |
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
Attribute | Description |
---|---|
style | Useful for one-off styling of selected options |
The select boxes that come with this addon exposes an API to you as an argument to action handlers like so:
actions: {
selectedAnOption(value, api) {
api.close(); // for example.
}
}
There are 3 ways to get what you want
It's recommended that you compose your own select box like so :
...and then use it like this:
...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>
If you need even more flexibility, you can extend the select box:
let MySelectBox = SelectBox.extend({
click() {
this.send('toggle');
}
})
if you need even more flexibility you can create your own select box using the mixins
let MySelectBox = Component.extend(BaseSelectBox, Toggleable, Searchable);
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.
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].
- 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
Q: Why aren't the native and faux select boxes two addons.
A: Less effort maintaining 1 addon. Splitting out would be trivial though.