0.2.1 • Published 7 years ago

tagselector v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Tag Selector

Lightweight (~2kb gzipped), ES6 Class-based library for turning select fields into customizable and user friendly tag clouds.

Features include:

  • ES6 Class-based (Transpiled to ES5 / CommonJS)
  • ~2kb in size gzipped
  • 100% Dependency free
  • Comes completely unstyled or with an optional base theme (see screenshot)
  • Easy to style with well-named CSS classes

Tag selector demo

See demo here

This library works by simply hiding and changing the hidden select field behind the scenes as user selects tags. The way you handle forms with selects remains the same, however users get a much more user friendly experience.

Installation

Via NPM:

npm install --save tagselector

Or simply download dist/tagselector.js manually if not using NPM.

Then simply include it however you like:

const TagSelector = require('tagselector');

or

<script src="path/to/tagselector.js"></script>

Usage

To use, simply create a select field with a unique ID, as well as some options with text and values:

<select id="someSelect">
  <option value="orange">Orange</option>
  <option value="apple">Apple</option>
</select>

Note: Add multiple to the select in order to get multiselect functionality with the tag cloud.

Next, create a new instance of tagselector and pass in the Javascript Object reference:

var selectField = document.getElementById('someSelect');
var tagSelector = new TagSelector(selectField);

To reload a current instance, simply do the following:

var selectField = document.getElementById('someSelect');
var tagSelector = new TagSelector(selectField);
tagSelector.reload();

You can also destroy the instance as follows:

var selectField = document.getElementById('someSelect');
var tagSelector = new TagSelector(selectField);
tagSelector.destroy();

Customization

Tag selector comes with a number of customizations that can be passed as an optional parameter:

var selectField = document.getElementById('someSelect');
var tagSelector = new TagSelector(selectField, {
  max: 2,
  onInit: function() {
    console.log('inited!')
  },
  onDestroy: function() {
    console.log('destroyed!')
  },
  onSelect: function(value, text) {
    console.log(`Selected item with value: '${value}' and text: '${text}'`)
  },
  onDeselect: function(value, text) {
    console.log(`Deselected item with value: '${value}' and text: '${text}'`)
  }
});
SettingDescriptionDefault
maxSets a maximum number of selects at a given time (only works with multiselect)false
onInitCallback function after new instance of tag selector is createdfalse
onDestroyCallback function after instance of tag selector is destroyedfalse
onSelectCallback function after a new tag is selected. Params received are option value and textfalse
onDeselectCallback function after a tag is deselected. Params received are option value and textfalse

Styles

By default, the wrapper and tags don't come with any styling, leaving the look and feel completely up to you. The classes available to style are as follows:

Class NameDescription
.tagselector-wrapThe wrapper div surrounding all tags
.tagselector-tagEach individiual tag span
.activeClass applied to any actively selected tag

If you would also like some basic styling out of the box (like in the screenshot above), include the CSS: dist/tagselector.css.

License

MIT License. See LICENSE.MD

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago