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:
content | The content of the item. It can be either a string or an HTML. |
---|---|
value | The 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. |
icon | A path to an image. The icon is prepended to the item and is given the select-box-option-icon CSS class. |
selected | true 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
}