Tag Select
Tag select is a JS alternative to <select> that allows you to create a nice looking multi-select with cross browser styling.
Tag Select offers an API for creating interaction with the component.
Configuration and initialization
By default, Tag select will use the HTML markup to generate the data.
Initialize from script. You may pass any selector as a string, a DOM node or node list.
Note: envision.select will return a Promise. Use the instance method .then() to access individual controls.
Single select
Create a Single select by using a Tag select and set maxItems to 1. Note: Clear and remove buttons will not be available in a single select.
Use allowEmptyOption and sortField to make behaviour similar to native select element.
Options
-
maxItemsnumber- The max number of items the user can select.
- Default value:
null(unlimited) - Set to
1to create a Single select.
-
createboolean- Allow adding new tags
- Default value:
false
-
createFilterRegExp | ‘string’ | function(input)- Specifies a RegExp or a string containing a regular expression that the current search filter must match to be allowed to be created. May also be a predicate function that takes the filter text and returns whether it is allowed.
-
clearButtonboolean- Show clear all button
- Default value:
true - Not available in Single select
-
allowEmptyOptionboolean- Only available in Single select
- Option with no value will be selectable if set to true.
- Default value:
false
-
placeholder‘string’- Use a custom placeholder.
- Default: Will try to use option with empty value or placeholder attribute from HTML first.
-
dropdownParent‘string’- The element the dropdown menu is appended to.
- Default: Will be appended as a child of the control.
-
options[{ value, text }]- Create a Tag select from custom dataset
- By default this is populated from the original element.
-
maxOptionsnumber- Limits the number of visible options
- Default value:
null(unlimited)
-
items[‘value’]- An array of the initial selected values.
- By default this is populated from the original element.
-
i18n‘sv’ | ‘en’ | ‘no’ | { add, no_results, remove_button, clear_button }- Translation of remove button, clear button, add item and no results. Use predefined strings (swedish, english or norwegian) or write your own translation. Default is ‘sv’.
-
loadfunction(query, callback)- Loads options by invoking the provided function. The function should accept two arguments (query, callback) and should invoke the callback with the results once they are available.
-
preloadboolean | ‘string’- If true, the load function will be called upon control initialization with an empty search. Alternatively it can be set to ‘focus’ to call the load function when control receives focus.
-
labelField‘string’- The name of the property to render as an option / item label when loading remote data.
-
valueField‘string’- The name of the property to use as the value when loading remote data.
-
searchField‘string’ | [‘string’]- A string or an array of property names to analyze when filtering options in remote data.
-
sortField‘string’ | [object] | function(a, b)- A string, an array of objects or a function to sort available options.
- By default the the order is based on the current locale.
- To disable sorting entirely and maintain the original order of options, use:
sortField:[{field:'$order'},{field:'$score'}]
-
renderobject- An object specifications for rendering.
- Available options:
itemfunction(data, escape)optionfunction(data, escape)option_createfunction(data, escape)no_resultsfunction(data, escape)loadingfunction(data, escape)
- The
escapeargument is a function that takes a string and escapes all special HTML characters. This is very important to use to prevent XSS vulnerabilities.
-
onEventNamefunction()- See Event handlers and Advanced example
Advanced examples
Options from JavaScript config
This example sets the options from the config. It will allow adding tags from the Tag Select itself, or from a separate input using the API.
Options from remote data API
This example fetches repository names from github. It will preload some popular names on page load. The data does not follow the Tag Select naming standards so value-/label-/search-/sortField must be defined.
States
Disabled
A Tag Select may be disabled by adding the disabled attribute in the HTML. When disabled, the control can not receive focus.
Locked
A Tag Select may be locked by adding the class env-select--locked to the <input> or <select>, or by using functions described in the API section.
Locked disables user input on the control, but the control can still receive focus.
Note: The readonly attribute is not supported or relevant to the Tag Select component. An input with the readonly attribute will be locked.
Event handlers
-
onInitializefunction() { … }- Invoked once the control is completely initialized.
-
onChangefunction(value) { … }- Invoked when the value of the control changes.
-
onItemAddfunction(value, $item) { … }- Invoked when an item is selected.
-
onItemRemovefunction(value) { … }- Invoked when an item is deselected.
-
onClearfunction() { … }- Invoked when the control is manually cleared via the clear() method.
-
onOptionAddfunction(value, data) { … }- Invoked when a new option is added to the available options list.
-
onOptionRemovefunction(value) { … }- Invoked when an option is removed from the available options.
-
onOptionClearfunction() { … }- Invoked when all options are removed from the control.
-
onDropdownOpenfunction(dropdown) { … }- Invoked when the dropdown opens.
-
onDropdownClosefunction(dropdown) { … }- Invoked when the dropdown closes.
-
onTypefunction(str) { … }- Invoked when the user types while filtering options.
-
onFocusfunction() { … }- Invoked when the control gains focus.
-
onBlurfunction() { … }- Invoked when the control loses focus.
-
onLoadfunction() { … }- Invoked when new options have been loaded and added to the control via the load option.
API functions
Instances of Tag Select may be controlled by the methods described below.
Options
-
addOptions(data)- Adds an available option, or array of options. If it already exists, nothing will happen. Note: this does not refresh the options list dropdown (use refreshOptions() for that).
-
getOption(value)- Retrieves the dom element for the option identified by the given value.
-
updateOption(value, data)- Updates an option available for selection. If it is visible in the selected items or options dropdown, it will be re-rendered automatically.
-
removeOption(value)- Removes the option identified by the given value.
-
refreshOptions(triggerDropdown)- Refreshes the list of available options shown in the autocomplete dropdown menu.
-
load(query)- Invoked when new options should be loaded from the server. Called with the current query string.
-
open()- Shows the autocomplete dropdown containing the available options.
-
close()- Closes the autocomplete dropdown menu.
-
clear(silent)- Resets / clears all selected items from the control. If
silentis truthy, no change event will be fired on the original input.
- Resets / clears all selected items from the control. If
-
clearOptions(clearFilter?)- Removes all unselected options from the control. To clear selection options, call clear() before calling clearOptions().
-
clearFilter(option, value)- Callback used by clearOptions() to decide whether or not an option should be removed. Return true to keep an option, false to remove
Items
-
getItem(value)- Returns the dom element of the item matching the given value.
-
addItem(value, silent)- “Selects” an item. Adds it to the list at the current caret position. If silent is truthy, no change event will be fired on the original input.
Other
-
getValue()- Returns the value of the control. If multiple items can be selected with a “select” input tag (e.g.
<select multiple>), this returns an array. Otherwise, returns a string (separated by delimiter if “multiple”).
- Returns the value of the control. If multiple items can be selected with a “select” input tag (e.g.
-
setValue(value, silent)- Resets the selected items to the given value.
-
lock()- Disables user input on the control. The control can still receive focus.
-
unlock()- Re-enables user input on the control.
-
disable()- Disables user input on the control. The control can not receive focus.
-
enable()- Re-enables the control.
-
destroy()- Destroys the control and unbinds event listeners so that it can be garbage collected.
-
focus()- Focuses the control.
-
blur()- Blurs the control.