Skip to main content

SelectBox

SelectBox is a UI component for displaying dropdowns.

Learn more about the SelectBox plugin.

constructor​

constructor(opt?: SelectBox.Options);

The ui.SelectBox constructor accepts several parameters:

width​

[optional] The width of the dropdown in pixels.

options​

[optional] An array of items. Each item is an object with the following properties:

contentThe content of the item. It can be either a string or an HTML.
valueThe value of the item. It can be a string, a number or any other JavaScript object. If the value is not defined, the value is considered to be equal to the content property.
iconA path to an image. The icon is prepended to the item and is given the select-box-option-icon CSS class.
selectedtrue if the item should be selected by default. If non of the items are selected, the first item is selected by default.

selected​

[optional] The index of the item which should be selected by default. If this value is undefined (which it is by default), the ui.SelectBox widget looks up the selected item from the options array (by using the selected boolean property). If nothing is selected, the first item in the options array is selected by default.

keyboardNavigation​

[optional] true (the default) if you want the ui.SelectBox to provide a keyboard navigation (up/down/left/right/enter/escape keys are supported).

selectBoxOptionsClass​

[optional] When the dropdown is opened, the ui.SelectBox generates an HTML container that contains all the items. This container is then appended to the document body (or target if provided). Sometimes, you want to provide custom styling to this dropdown container. selectBoxOptionsClass gives you a chance to define a CSS class that will be set on this container so that you can style it in your CSS.

target​

[optional] ui.SelectBox generates an HTML container that contains all the dropdown items. This container is by default appended to the document body. In some situations (e.g. if you want the dropdown to scroll with some other container), you want to append this container to a different DOM element. The target option is exactly for that. It accepts an HTML element that will be used as a container for the generated dropdown items.

openPolicy​

[optional] openPolicy determines how the dropdown will open when clicked. It can be one of 'auto', 'above', 'coverAbove', 'below', 'coverBelow' and 'selected'.

placeholder​

[optional] In some cases, you want to deselect the dropdown and show a placeholder in place of the selected item. This is when you don't want any of the items to be selected. In this case, set the selected index to -1 and the placeholder to any HTML you want to display in the selected item window. This placeholder has the selected-box-placeholder CSS class that you can use for further styling.

Methods​

render()​

selectBox.render(): this;

Render the dropdown. Note that once you render the dropdown, you can use the el property that points to the container HTML element and append it anywhere in the DOM (e.g. document.body.appendChild(selectBox.el)).

getSelection()​

selectBox.getSelection(): SelectBox.Selection;

Get the current selection. The selection points to one of the items from the options array.

getSelectionValue()​

selectBox.getSelectionValue(selection?: SelectBox.Selection): any;

Get the current selection value.

getSelectionIndex()​

selectBox.getSelectionIndex(): number;

Get the index in the options array of the item that is currently selected.

select()​

selectBox.select(idx: number, opt?: { [key: string]: any }): void;

Select an item. index is the index to the options array. opt is optional and can be an arbitrary object that will be passed to the option:select event handler.

selectByValue()​

selectBox.selectByValue(value: any, opt?: { [key: string]: any }): void;

Select an item by value. The selected item will be chosen based on a strict equality of the value passed an a value (content) of any of the items.

isOpen()​

selectBox.isOpen(): boolean;

Returns true if the dropdown is currently opened. false otherwise.

toggle()​

selectBox.toggle(): void;

Programmatically toggle the dropdown.

open()​

selectBox.open(): void;

Programmatically open the dropdown.

close()​

selectBox.close(): void;

Programmatically close the dropdown.

remove()​

selectBox.remove(): this;

Remove the dropdown from the DOM.

disable()​

selectBox.disable(): void;

Disable the dropdown.

enable()​

selectBox.enable(): void;

Enable the dropdown.

isDisabled()​

selectBox.isDisabled(): boolean;

Return true if the dropdown is disabled.

on()​

selectBox.on(eventName: string, callback: EventHandler, context?: any): this;

Register a callback function that will be called whenever eventName is triggered. The optional context is an object that will be the context of the handler function (the this).

Events​

option:hover​

Triggered when the user hovers over one of the items. The handler is passed the option object and the index of the option in the options array.

option:select​

Triggered when the user selects an item. The handler is passed the option object and the index of the option in the options array. If you selected the item with the select(index, opt) method, the third argument to the event handler will be your opt object.

option:mouseout​

Triggered when the user leaves an item with mouse cursor. The handler is passed an event object as the only argument.

close​

Triggered when the dropdown gets closed.

Types​

OpenPolicy​

type OpenPolicy = 'selected' | 'auto' | 'above' | 'coverAbove' | 'below' | 'coverBelow';

Selection​

interface Selection {
[key: string]: any;
}

Options​

interface Options extends mvc.ViewOptions<undefined> {
icon?: string;
content?: mvc.$HTMLElement;
options?: Array<Selection>;
target?: mvc.$HTMLElement;
width?: number;
openPolicy?: OpenPolicy;
selectBoxOptionsClass?: string | (() => string);
placeholder?: string;
disabled?: boolean;
selected?: number;
keyboardNavigation?: boolean
}