BulmaTagsInput is available as a JavaScript object, so you can use it directly.
// Instantiate on one element
new BulmaTagsInput(selector, options);
// Instantiate on multiple elements
BulmaTagsInput.attach(selector, options);
Arguments
String
returning a single Node
or directly a Node
attach
:
String
returning a single Node
or a NodeList
, directly a Node
or a NodeList
Whatever the way you use to instantiate BulmaTagsInput, all element on which BulmaTagsInput is applied will now contains a new BulmaTagsInput
function which will let you access to the instance at any time.
var inputTags = document.getElementById('input-tags');
new BulmaTagsInput(inputTags);
// Access to the BulmaTagsInput instance
var btiInstance = inputTags.BulmaTagsInput();
// Call BulmaTagsInput method
btiInstance.add('new Tag');
You can easily manage the component behavior by passing an object of options to the constructor.
Options can be set either through JavaScript or directly on the DOM as dataset of the element.
Provide options when instanciating plugin. Options will be applied to all element provided (or found by the given selector).
BulmaTagsInput.attach('input[data-type="tags"], input[type="tags"], select[data-type="tags"], select[type="tags"]', {
allowDuplicates: false,
caseSensitive: true,
clearSelectionOnTyping: false,
closeDropdownOnItemSelect: true,
delimiter: ',',
freeInput: true,
highlightDuplicate: true,
highlightMatchesString: true,
itemValue: undefined,
itemText: undefined,
maxTags: undefined,
maxChars: undefined,
minChars: 1,
noResultsLabel: 'No results found',
placeholder: '',
removable: true,
searchMinChars: 1,
searchOn: 'text',
selectable: true,
source: undefined,
tagClass: 'is-rounded',
trim: true
});
Add option directly to the element by using dataset attributes. This way, options can be specific to each element. These declaration will override any default option or option passed by the JavaScript instantiation.
All you have to do is to use the kebab-case notation of option and declare it as a data attribute
of the HTML Element.
<input type="tags" data-allow-duplicates="true" data-max-chars="10" data-min-chars="3" data-selectable="false">
All available option are described in the folowing table:
Option name | Description | Type | Default value |
---|---|---|---|
allowDuplicates | When true, the same tag can be added multiple times. | Boolean | false |
caseSensitive | When true, duplicate tags value check is case sensitive. | Boolean | true |
clearSelectionOnTyping | When true, tags will be unselected when new tag is entered. | Boolean | false |
closeDropdownOnItemSelect | When true, datalist will close automatically after an item have been selected. | Boolean | true |
delimiter | Multiple tags can be added at once. Delimiter is used to separate all tags. | String | , |
freeInput | When true, tags can be entered manually. This option is useful with select Tags inputs. Set to false automatically when using on select element. | Boolean | true |
highlightDuplicate | When true, if allowDuplicates option if false then the already existing tag will be temporarly and visually identified as duplicate |
Boolean | true |
highlightMatchesString | When true, identified matches strings when searching is highlighted. | Boolean | true |
itemText | When adding objects as tags, you can set itemText to the name of the property of item to use for a its tag’s text. When this options is not set, the value of itemValue will be used. | String | undefined |
itemValue | When adding objects as tags, itemValue must be set to the name of the property containing the item’s value. | String | undefined |
maxTags | When set, no more than the given number of tags are allowed to add. | Integer | undefined |
maxChars | Defines the maximum length of a single tag. | Integer | undefined |
minChars | Defines the minimum length of a single tag. | Integer | 1 |
noResultsLabel | Empty dropdown label. | String | No results found |
placeholder | TagsInput placeholder text if original input doesn’t have one. | String | undefined |
removable | When true, tags are removable either using the associted delete button or backspace and delete keys. | Boolean | true |
searchMinChars | Defines the minimum length of input value before loading auto-complete. | Integer | 1 |
searchOn | Defines on what dropdown item data do we search the entered value. Possible value: ‘value’ or ‘text’. | String | text |
selectable | When true, tags can be selected either by mouse click or using left or right arrow keys. | Boolean | true |
source | Source of data proposed in dropdown (used for auto-complete). | Can be one of the following types:
|
undefined |
tagClass | Classname applied to each tag. | String | is-rounded |
trim | When true, automatically removes all whitespace around tags. | Boolean | true |
Instanciate the component based on the element provided and register the instance into BulmaTagsInput
function of the element.
const bulmaTagsInputElement = new BulmaTagsInput('#input-tags');
// Access to the BulmaTagsInput instance from DOM
const inputTagsElement = document.getElementById('input-tags');
if (inputTagsElement) {
const bulmaTagsInputInstance = inputTagsElement.BulmaTagsInput();
}
Constructor shortcut with possibility to pass a String, a Node or a NodeList as a selector so we can instantciate multiple element at once. Return an array of all instances. Note that each instance is link to the related DOM node under BulmaTagsInput
function.
// Return an array of BulmaTagsInput instances (empty if no DOM node found)
const bulmaTagsInputInstances = BulmaTagsInput.attach();
// Loop into instances
bulmaTagsInputInstances.forEach(bulmaTagsInputInstance => {
// Work with the instance
});
Add given item to the component
Item to add. It can be one of the following type: String, Array.
You can provide multiple items at once by passing and Array of item or a string with multiple value delimited by delimiter
option (default: comma).
var inputTags = document.getElementById('myInputTag');
// Add one string item
inputTags.BulmaTagsInput().add('john');
// Add multiple string items in once by passing multiple values as a string delimited by comma
inputTags.BulmaTagsInput().add('john,jane');
// Add multiple string items as array
inputTags.BulmaTagsInput().add(['john', 'jane']);
// Add one object item
inputTags.BulmaTagsInput().add({
"value": "1",
"text": "John"
});
// Add multiple object items at once
inputTags.BulmaTagsInput().add([{
"value": "1",
"text": "John"
}, {
"value": "2",
"text": "Jane"
}]);
Unselect the current selected tag
var inputTags = document.getElementById('myInputTag');
// Clear selection
inputTags.BulmaTagsInput().clearSelection();
Shortcut to removeAll method
var inputTags = document.getElementById('myInputTag');
// Remove all tags focus
inputTags.BulmaTagsInput().flush();
Sets focus on the input
var inputTags = document.getElementById('myInputTag');
// Set focus
inputTags.BulmaTagsInput().focus();
Check if given item is present
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().has('john'); // return True
inputTags.BulmaTagsInput().has('jane'); // return False
Check if given value is present
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add({
"value": "john",
"text": "John Doe"
});
inputTags.BulmaTagsInput().hasValue('john'); // return True
inputTags.BulmaTagsInput().hasValue('jane'); // return False
Check if given text is present
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add({
"value": "john",
"text": "John Doe"
});
inputTags.BulmaTagsInput().hasText('John Doe'); // return True
inputTags.BulmaTagsInput().hasValue('Jane Doe'); // return False
Get index of given item
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().indexOf('john'); // return 0
inputTags.BulmaTagsInput().has('jane'); // return -1
Get the internal input element
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().input();
Get all added items
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().item(); // return ['john']
Remove given items
Item to add. It can be one of the following type: String, Array.
You can provide multiple items at once by passing and Array of item or a string with multiple value delimited by delimiter
option (default: comma).
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().remove('john');
Remove all tags at once
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
inputTags.BulmaTagsInput().items(); // return ['john', 'jane']
inputTags.BulmaTagsInput().removeAll();
inputTags.BulmaTagsInput().items(); // return [] (empty array)
Remove item at given index.
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().items(); // return ['john']
inputTags.BulmaTagsInput().removeAtIndex(0);
inputTags.BulmaTagsInput().items(); // return [] (empty array)
Select given item
Item to add. It can be one of the following type: String, Array.
You can provide multiple items at once by passing and Array of item or a string with multiple value delimited by delimiter
option (default: comma). If a list of items is passed then each item will be selected one by one and at the end only the last existing item from the list will be selected at the end.
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
inputTags.BulmaTagsInput().select('john'); // Tag containing 'john' value now have is-selected class
Select tag at given index
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
inputTags.BulmaTagsInput().selectAtIndex(0); // Tag containing 'john' value now have is-selected class
Get the current selected item
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
inputTags.BulmaTagsInput().selectAtIndex(0);
var selectedItem = inputTags.BulmaTagsInput().selected;
// selectedItem = 'john'
Get the current selected item index
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
inputTags.BulmaTagsInput().selectAtIndex(0);
var selectedItem = inputTags.BulmaTagsInput().selectedIndex;
// selectedItem = 0
Get component value
var inputTags = document.getElementById('myInputTag');
inputTags.BulmaTagsInput().add('john');
inputTags.BulmaTagsInput().add('jane');
var value = inputTags.BulmaTagsInput().value;
// value = "john,jane"
// If component has been instantiated on a multiple select element then a real array is returned
// value = ["john", "jane"]
Build TagsInput DOM elements
Bind all events listener
Determine if input caret it at the start of the input or not.
Check value length constraint if option activated
Create a new dropdown item based on given item data
Create a new tag and add it to the DOM
Remove all dropdown items except the empty title
Filter Dropdown items to be compliant with already selected items and current input value Filtering is made on Text by default (can be changed with option)
Find needle into a string and wrap it with HTML tag
Initialize component, define internal variables and elements.
Listen document click events to manage dropdown visibility. It prevents dropdown to be hidden when the click occurs inside the TagsInput component.
Listener handling dropdown item click and call add
method.
Listen to internal input change event and filter dropdonw items.
Listen to internal input click event and display dropdown if it is relevant.
Add is-focused
class to the TagsInput container when the internal input get focus.
Remove is-focused
class from the TagsInput container when the internal input lost focus.
Listen to original input change
event to automatically update tags.
Listen to tags click events to handle tag selection.
Listen to tag delete button click event to handle tag deletion.
Open dropdown element
Propagate internal input changes to the original input
Trim item is trim
option is set to true
. This function handle both string and object items.
Update dropdown items based on context.
Update original select option based on given item.
This component provides some events you can listen to to react on state changes.
Event name | Description | Example |
---|---|---|
before.add | Trigerred before adding new tag. The concerned item is passed as parameter. You can modify the item before its treatment by returning the new item data or prevent tag to be added by returning false. |
|
after.add | Triggered once a tag has been added. The added item and the related tag are passed in an object as parameter. |
|
before.remove | Triggered before removing a tag. The concerned item is passed as parameter. You can prevent deletion by returning false . |
|
after.remove | Triggered once a tag has been removed. The removed item is passed as parameter. |
|
before.flush | Triggered before flushing items. Items array is passed as parameter. |
|
after.flush | Triggered after flushing items. |
|
before.select | Triggered before selecting an item. The concerned item and related tag are passed in an Object as parameter. |
|
after.select | Triggered once an item has been selected. The concerned item and related tag are passed in an Object as parameter. |
|
before.unselect | Triggered before unselect an item. The concerned item and related tag are passed in an Object as parameter. |
|
after.unselect | Triggered once an item has been unselected. The concerned item and related tag are passed in an Object as parameter. |
|