1.5.2 • Published 2 years ago

tokenfield v1.5.2

Weekly downloads
743
License
BSD-3-Clause
Repository
github
Last release
2 years ago

Tokenfield

Input field with tagging/token/chip capabilities written in raw JavaScript. Tokens in OS X or Chips in Android -
small UI elements which are inserted into various input fields that often combine with autocomplete functionality.

Tokens allow designers to display extra information about input. For example, in email applications when typing an
email address of the recipient, input field could display full name of the owner of a given email and a his/her picture.

This Tokenfield implementation is written in raw JavaScript without any extra dependencies like jQuery. it has one
somewhat opinionated behavior - Tokenfield intended use case is work with structured data. More specifically, it expects
autocomplete data to be JSOn formatted array of objects where each object contains token ID and token Name. More on that
below.

Examples

View live examples here.

GIF demo

Usage

Via JavaScript

Tokenfield could be applied to any visible <input /> element that allows users
to input text or number.

// Given that we have following HTML element: <input class="my-input" />
var tf = new Tokenfield({
  el: document.querySelector('.my-input')
});

This action would create Tokenfield wrapped around given input element. Without additional options, this Tokenfield
would allow users to add multiple token items without any specific restrictions. Only unique items are allowed, though,
so it is not possible to add multiple items such as: "foo", "bar", "foo". Only first "foo" would be added and the last
one discarded.

Data

As it was mentioned above - Tokenfield is intended to be used with structured data - array of objects. With default
options it expects that data returned by the autocomplete or filtered from a given set of items would look like that:

[{id: 1, name: 'Red'}, {id: 2, name: 'Blue'}, {id: 3, name: 'Greed'}, ... ]

You can see that each object has two properties - id and a name. With this format when you submit form where
Tokenfield is located, server would receive not an array of string, but an array of IDs.

However, that is a case only with tokens that are added via autocomplete. If Tokenfield accepts new tokens, then form
would send an additional array which would contain an array of strings.

Options

NameTypeDefaultDescription
elstring or DOM nodenullDOM element or string with selector pointing at an element you want to turn into tokenfield.
formbool, string or DOM nodetrueListens to reset event on the specified form. If set to true listens to immediate parent form.
itemsarray[]Array of objects amongst which autocomplete will try to find a match. Default format might look like this: [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
setItemsarray[]Array of objects which would be displayed as selected after Tokenfield has been created.
newItemsbooltrueOption to allow user to add custom tokens instead of using preset list of tokens or tokens retrieved from the server.
multiplebooltrueOption to allow multiple tokens in the field.
maxItemsinteger0Option to limit number of items. Set to 0 to remove the limit.
matchRegexstring'{value}'Regex string that would be used for matching - when regex is compiled {value} would be replaced with escaped user input.
matchFlagsstring'i'Regex flags used in matching. Default is i - case insensitive matching.
matchStartboolfalseOption to do matching only from the beginning of the string - it compiles match regex to basicall this format: /^{value}/i.
matchEndboolfalseOption to do matching only from the end of the string - it compiles match regex to basicall this format: /{value}$/i.
remoteobjectDetails on that - below in Autocomplete section.
addItemOnBlurboolfalseIf set to true, will add new item to the tokenfield on input blur. Either sets new item or first match from suggested list.
delimitersarray[]Option to specify certain characters/sets of characters to be used as delimiters during tokenization or input events on tokenfield.
addItemsOnPasteboolfalseIf set to true, will add new item to the tokenfield on paste. Tokenization happens using delimiters options listed above.
placeholdernull or stringnullSet a placeholder that will be shown in the input. If set to null, will try to use placeholder attribute from the original element set in el
inputTypestring'text'Specifies HTML type attribute for the input element.
minCharsinteger2Specifies how many characters user has to input before autocomplete suggester is shown.
maxSuggestinteger10Specifies how many suggestions should be shown.
filterSetItemsbooltrueSpecifies whether already set items should not be shown in suggested list.
filterMatchCaseboolfalseUsed to match set items and new item by case-sensitivity. By default is set to false and does not take case of item value into the account.
itemLabelstring'name'Property of an item object which is used to display text in tokens.
itemNamestring'items'Each token item will have its own hidden input which will contain an ID of a given item and a name attribute in an array format. This option sets a name. By default it is set to "items" which means that when user will submit a form server would receive an array of IDs under the name "items".
newItemNamestring'items_new'Same as the above except it is only related to new items which were not added via autocomplete.
itemValuestring'id'Specifies which property from the autocomplete data to use as a primary identifying value.
itemDatastring'name'Which property should be used when you do autocomplete on a given array of items.
validateNewItemclosurenullIf set, closure will run on every attempt to add new non-existing item to validate it. Return true to allow an item, return any falsy value and it will prevent new item from being added.

Remote Options

Below you will find list of options which are related to remote autocomplete requests. Options are set as properties
of an object assigned to remote property of the parent options object:

new Tokenfield({
  remote: {
    url: "http://example.com",
    ...
  }
});
NameTypeDefaultDescription
typestring'GET'Sets AJAX request type. Usually GET or POST
urlstringnullSpecifies which URL will be used to retrieve autocomplete data. If set to null - remote autocomplete won't be performed.
queryParamstring'q'Sets name of the parameter which would contain value that user typed in the input field.
delayinteger300Sets delay in milliseconds after which remote request is performed.
timestampParamstring't'Sets parameter for the timestamp when remote call was requested.
paramsobject{}Sets any additional AJAX params
headersobject{}Sets AJAX headers. Could be simple key:value items, or key:function items if you want to add headers dynamically

Events

Tokenfield uses standard node.js EventEmitter and therefore supports such event as: 'on', 'once', 'removeAllListeners', 'removeListener'.

For more details on EventEmitter, please check official documentation page.

Available events are:

Event TypeDescription
changeFired after any change in tokens list - after adding or removing tokens, setting multiple tokens manually etc.
showSuggestionsFired before Tokenfield would show suggestions box.
shownSuggestionsFired after Tokenfield has shown suggestions box.
hideSuggestionsFired before Tokenfield would hide suggestions box.
hiddenSuggestionsFired after Tokenfield has hidden suggestions box.
addTokenFired before token has been added to the tokenfield. Second argument contains token data.
addedTokenFired after token has been added to internal token list.
removeTokenFired before token has been removed from the tokenfield. Second argument contains token data.
removedTokenFired after token has been removed from the tokenfield. Second argument contains removed token data.

Helper Methods

Tokenfield has several overridable methods which allow user to remap given token data or change how some elements are
rendered.

Available methods are:

Method nameDescription
remapDataFired on every data request. Override it if you want to change structure of an available data - change props names, sanitize property values, remove props. Just make sure to return array of objects which would be consumed by the tokenfield instance.
renderSetItemLabelFired on token item render. Override this method in order to change how label for each token is rendered
onInputFired when you type something in the input field. Accepts value of the input field and event object.
showSuggestionsShows list of suggested items if there are any.
hideSuggestionsTemporarily hides list of suggested items.
getItemsReturns an array containing objects for currently set tokens.
setItemsOverride current set tokens with your own array of tokens. Input could be array or a single object and must conform to the format described at the beginning of the readme.
addItemsAdd tokens. Input could be an array or a single object. Object or array of objects must conform to the correct format described at the beinning of readme.
sortItemsUpdates internal state of the set tokens based on the HTML state of the tokens - useful when you apply a "sortable" or "draggable" library on tokenfield items.
removeItem(value)Remove an item based on the given input. Input could be an item object, item name for new item or item id for existing item.
emptyItemsRemove all currently set token items.
getSuggestedItemsReturns an array of suggested items.
focusFocus the tokenfield.
blurRemove focus from the tokenfield.
removeDestroy tokenfield and display original element it was attached to.
1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.11.0

6 years ago

0.10.0

6 years ago

0.9.10

6 years ago

0.9.9

6 years ago

0.9.8

6 years ago

0.9.6

6 years ago

0.9.5

6 years ago

0.9.4

6 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.6

7 years ago

0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.6.12

7 years ago

0.6.11

7 years ago

0.6.10

7 years ago

0.6.9

7 years ago

0.6.8

7 years ago

0.6.7

7 years ago

0.6.6

7 years ago

0.6.5

7 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.14

8 years ago

0.3.13

8 years ago

0.3.12

8 years ago

0.3.11

8 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago