HexagonJS
Edit Page
Tag Input
A component for inputting tags.
Examples

HTML

<div id="tag-input"></div>

JavaScript

var tagInput = new hx.TagInput('#tag-input')
tagInput.items(['tag-1', 'tag-2', 'tag-3'])

HTML

<div id="tag-input-2"></div>

JavaScript

var tagInput = new hx.TagInput('#tag-input-2', {
  classifier: function(value) {
    switch(value){
      case 'hello':
        return 'hx-positive'
      case 'goodbye':
        return 'hx-negative'
    }
  }
})
tagInput.add('hello', 'hx-positive')
tagInput.add('goodbye', 'hx-negative')
tagInput.add(['tag', 'tag2'])

HTML

<div id="autocomplete-example"></div>

JavaScript

var items = [
  {
    type: 'A',
    name: 'A Text'
  },
  {
    type: 'B',
    name: 'B Text'
  },
  {
    type: 'D',
    name: 'D Text'
  },
  {
    type: 'C',
    name: 'C Text'
  }
]

var ti = new hx.TagInput('#autocomplete-example', {
  autocompleteOptions: {
    renderer: function (elem, item) {
      hx.select(elem).text(item.name)
    },
    inputMap: function (item) { return item.name }
  },
  autocompleteData: function (term, cb) {
    // Remove duplicates and filter based on search term
    cb(items.filter(function (item) {
      return ti.items().indexOf(item.name) === -1 && item.name.indexOf(term) > -1
    }))
  }
})
Api
Prototypes
hx.TagInputextendsEventEmitter
Initialises a tag input.
Constructors
hx.TagInputselectorString / HTMLElementoptionsObject
Arguments
selectorString / HTMLElement
A css selector that uniquely identifies the collapsible in the DOM.
options
The options to use when creating the tag input.
Properties
autocompleteDataArray / Function
The data to use as suggestions for the tag input. Data can be specified as an array of items or as a function that returns an array of items.
See
By default, no autocompletion is provided.
autocompleteOptions
Special options to be passed to the autocomplete. See the Autocompleteconstructor for the available options.
classifiertagStringString
Provides a way to apply css classes to tags.
Arguments
The tag to classify
Returns
The css class to give a tag.
disabledBoolean
Whether the tag input should be disabled
Default: false
draggableBoolean
Whether the tags can be dragged and re-ordered.
Default: true
excludeTagsBoolean
Determines whether existing tags should be excluded from the autocompletion.
Default: true
The array of tags to initialise the tag input with.
mustMatchAutocompleteBoolean
Determines whether the TagInput should only allow inputs that come from the autocomplete (if defined). Be default true.
placeholder
The placeholder for the input field when it has no text
Default: [hx.userFacingText('tagInput', 'placeholder')]
validatortagStringString
A function to prevent the user from adding certain tags.
// example: only allow non number inputs
new hx.TagInput('#validated-tag-input', {
  validator: function(name) {
    if (!isNaN(Number(name))){
      return "please enter text";
    }
  }
});
Arguments
The tag that is being added
Returns
The error to show to the user. If no error exists, the return value should be an empty string.
Events
add
Emitted when a tag is added.
remove
Emitted when a tag is removed.
Methods
addnameStringclassStringTagInput
Adds a tag.
Arguments
nameString
The tag to add
classString
The CSS class to give the tag.
Returns
TagInput
This TagInput
addtagsArray[String]classStringTagInput
Adds an array of tags.
Arguments
An array of tag names to add.
classString
The CSS class to give the tag.
Returns
TagInput
This TagInput
disabledvalueBooleanTagInput
A method for disabling or enabling a tag input
Arguments
valueBoolean
Whether the tag input should be disabled
Returns
TagInput
This tag input
disabledBoolean
A method for getting the disabled state of a tag input
Returns
Whether the tag input is disabled
itemsitemsArray[String]classStringTagInput
A method for setting the tags
Arguments
The array of tags to set
classString
The CSS class to give the tag.
Returns
TagInput
This tag input for chaining
A method for getting the tags
Returns
The current set of tags.
removenameStringNumber
Removes all tags of a given value. Returns the number of tags removed.
Arguments
nameString
The tag to remove.
Returns
removeArray[String]
Removes all tags from the input. Returns an array of the tags removed.
Returns
Functions
hx.tagInputoptionsObjectSelection
Creates a new TagInput set up on a detached element, wrapped in a selection
Arguments
options
See the options object for constructing TagInput
Returns
Selection
A selection containing an element with an TagInput initialised on it
Classes
hx-tag
The base class given to tags in the tag input.
Extra Classes
hx-action
The additional class to give to action tags.
hx-complement
The additional class to give tags a complementary color to the base theme color.
hx-contrast
The additional class to give tags a contrasting color to the base theme color.
hx-info
The additional class to give to info tags.
hx-negative
The additional class to give to negative tags.
hx-positive
The additional class to give to positive tags.
hx-warning
The additional class to give to warning tags.