Feature-rich Tags Input With Autocomplete – Tokenfield

Category: Form , Javascript | June 16, 2023
Author:KaneCohen
Views Total:258 views
Official Page:Go to website
Last Update:June 16, 2023
License:MIT

Preview:

Feature-rich Tags Input With Autocomplete – Tokenfield

Description:

Tokenfield is a dynamic and feature-rich tags input JavaScript plugin that allows users to enter and/or select from a list of options as tags (a.k.a. tokens in OSX and chips in Android) in an input field.

How to use it:

1. Download and import the Tokenfield’s files.

<link rel="stylesheet" href="tokenfield.css" />
<script src="tokenfield.web.js"></script>

2. Prepare an array of items for the autocomplete list.

const myItems = [
      {id: 1, name: 'JavaScript'}, 
      {id: 2, name:'HTML'}, 
      {id: 3, name: 'CSS'}, 
      {id: 4, name: 'Angular'}, 
      {id: 5, name: 'React'}, 
      {id: 6, name: 'Vue'},
      // ...
],

3. Initialize the Tokenfield on your input field and done.

<input class="basic" placeholder="e.g. HTML, JavaScript, CSS" />
const instance = new Tokenfield({
      el: document.querySelector('.basic'),
      items: myItems,
});

4. It also allows you to fetch items from a remote data source.

const instance = new Tokenfield({
      el: document.querySelector('.basic'),
      remote: {
        type: 'GET', // GET or POST
        url: null, // Full url.
        queryParam: 'q', // query parameter
        delay: 300, // delay in ms
        timestampParam: 't',
        params: {},
        headers: {}
      },
});

5. All possible options:

const instance = new Tokenfield({
      form: true, // Listens to reset event
      mode: 'tokenfield', // tokenfield or list.
      addItemOnBlur: false,
      addItemsOnPaste: false,
      keepItemsOrder: true, 
      setItems: [], // array of pre-selected items
      newItems: true,
      multiple: true,
      maxItems: 0, 
      minLength: 0,
      keys: {
        17: 'ctrl',
        16: 'shift',
        91: 'meta',
        8: 'delete', // Backspace
        27: 'esc',
        37: 'left',
        38: 'up',
        39: 'right',
        40: 'down',
        46: 'delete',
        65: 'select', // A
        67: 'copy', // C
        88: 'cut', // X
        9: 'delimiter', // Tab
        13: 'delimiter', // Enter
        108: 'delimiter' // Numpad Enter
      },
      matchRegex: '{value}',
      matchFlags: 'i',
      matchStart: false,
      matchEnd: false,
      delimiters: [], // array of strings
      copyProperty: 'name', 
      copyDelimiter: ', ',
      placeholder: null,
      inputType: 'text',
      minChars: 2, 
      maxSuggest: 10,
      maxSuggestWindow: 10,
      filterSetItems: true,
      filterMatchCase: false, 
      singleInput: false, // true, 'selector', or an element.
      singleInputValue: 'id', 
      singleInputDelimiter: ', ',
      itemLabel: 'name', 
      itemName: 'items', 
      newItemName: 'items_new',
      itemValue: 'id', 
      newItemValue: 'name', 
      itemData: 'name',
      validateNewItem: null
});

5. API methods.

instance.instance.remapData();
instance.renderSetItemLabel();
instance.onInput();
instance.showSuggestions();
instance.hideSuggestions();
instance.getItems();
instance.setItems();
instance.addItems();
instance.sortItems();
instance.removeItem(value)();
instance.emptyItems();
instance.getSuggestedItems();
instance.focus();
instance.blur();
instance.remove();

6. Events.

instance.on('change', function() {
  // do something
});
instance.on('showSuggestions', function() {
  // do something
});
instance.on('shownSuggestions', function() {
  // do something
});
instance.on('hideSuggestions', function() {
  // do something
});
instance.on('hiddenSuggestions', function() {
  // do something
});
instance.on('addToken', function() {
  // do something
});
instance.on('addedToken', function() {
  // do something
});
instance.on('removeToken', function() {
  // do something
});
instance.on('removedToken', function() {
  // do something
});

You Might Be Interested In:


Leave a Reply