Tag input component
You can get it on GitHub.
npm install https://github.com/Collokia/taggyTaggy demands one thing of you: the input must have no siblings.
<div>
<input id='insigificant' />
</div>Taggy exposes a function to turn an input into a tag list input. Empty spans will be added on both sides of your input element.
A few options may be provided. They are detailed below.
Defaults to true. When this flag is turned off, options can only be picked from the autocomplete list, they can't be edited, but they can still be deleted entirely using backspace.
When true, humans will be able to delete individual tags by clicking on an icon.
The separator between tags. Defaults to ' '. Must be a single character.
This option will prevent tags identified as invalid from being added. By default this is turned off and they just get a tay-invalid CSS class.
A method that validates whether the user input value constitutes a valid tag. Useful to filter out duplicates. Defaults to the method below, that does exactly that. Note that in the code below, toy is the API returned by calling taggy(el).
function validate (value) {
return toy.findItem(value) === null;
}Note that tags is only a copy and modifying it won't affect the list of tags.
A method that's called whenever a tag should be rendered. Defaults to setting getText(item) as the container's text.
When you have complex data items from autocomplete, you need to set parseText to read the value that should be used as a display value.
When you have complex data items from autocomplete, you need to set parseText to read the value that should be used as each tag's value.
Expects an object that defines how the autocomplete list is configured. Autocomplete options are listed below.
Runs when a tag is inserted. The returned string is used to pre-fill the text input. Useful to avoid repetitive user input. The suggestion list can be used to choose a prefix based on the previous list of suggestions.
info.inputcontains the user input at the time a suggestion was selectedinfo.suggestionscontains the list of suggestions at the time a suggestion was selectedinfo.selectioncontains the suggestion selected by the userCan be an object that will be used to store queries and suggestions. You can provide a
cache.durationas well, which defaults to one day and is specified in seconds. Thecache.durationis used to figure out whether cache entries are fresh or stale.You can disable autocomplete caching by setting
cachetofalse.Can be a number that determines the maximum amount of suggestions shown in the autocomplete list.
By default suggestions are filtered using the
fuzzysearchalgorithm. You can change that and use your ownfilteralgorithm instead.Specifies whether the autocomplete should suggest tags that are already selected.
A
suggestions(data)should be set to a function that returns a promise. The promise should fulfill to the result for the provideddata.input.
data.inputis a query for which suggestions should be provideddata.limitis the previously specifiedoptions.limitdata.valuesare the currently selected valuesdata.previousSelectionis the last suggestion selected by the userdata.previousSuggestionsis the last list of suggestions provided to the userThe expected schema for the promise's fulfillment result is outlined below.
[category1, category2, category3]Each category is expected to follow the next schema. The
idis optional, all category objects without anidwill be treated as if theiridwas'default'. Note that categories under the sameidwill be merged together when displaying the autocomplete suggestions.{ id: 'here is some category', list: [item1, item2, item3] }When this option is set to
true, thesuggestions(data)function will be called even when theinputstring is empty.Defaults to
null. Set to a string if you want to display an informational message when no suggestions match the providedinputstring. Note that this message won't be displayed wheninputis empty even ifblankSearchis turned on.The minimum amount of milliseconds that should ellapse between two different calls to
suggestions. Useful to allow users to type text without firing dozens of queries. Defaults to300.If set to
false, autocomplete suggestions won't be highlighted based on user input.If set to
false, autocomplete suggestions won't be highlighted as whole words first. The highlighter will be faster but the UX won't be as close to user expectations.By default, items are rendered using the text for a
suggestion. You can customize this behavior by settingautocomplete.renderItemto a function that receivesli, suggestionparameters. Theliis a DOM element and thesuggestionis its data object.By default, categories are rendered using just their
data.title. You can customize this behavior by settingautocomplete.renderCategoryto a function that receivesdiv, dataparameters. Thedivis a DOM element and thedatais the full category data object, including thelistof suggestions. After you customize thediv, the list of suggestions for the category will be appended todiv.
By default, tags are converted whenever the blur event fires on elements other than input. Set to false to disable.
When you call taggy(input, options), you'll get back a tiny API to interact with the instance. Calling taggy repeatedly on the same DOM element will have no effect, and it will return the same API object.
Adds an item to the input. The data parameter could be a string or a complex object, depending on your instance configuration.
Finds an item by its data string or object.
Return the index of the first item found by its data string or object.
Finds an item by its .tay-tag DOM element.
Removes an item from the input. The item is found using the data string or object.
Removes an item from the input. The item is found using a .tay-tag DOM element.
Returns the list of valid tags as an array.
Returns the list of tags as an array including invalid tags.
Removes all event listeners, CSS classes, and DOM elements created by taggy. The input's value is set to the output of .value(). Once the instance is destroyed it becomes useless, and you'll have to call taggy(input, options) once again if you want to restore the behavior.
The instance API comes with a few events.
| Event | Arguments | Description |
|---|---|---|
add |
data, el |
Emitted whenever a new item is added to the list |
remove |
data |
Emitted whenever an item is removed from the list |
invalid |
data, el |
Emitted whenever an invalid item is added to the list |
autocomplete.beforeUpdate |
none | Emitted before asking the autocomplete for a different list of suggestions, useful to abort AJAX requests that are no longer necessary |
You can listen to these events using the following API.
const toy = taggy(el);
toy.on('add', data => console.log(data)); // listen to an event
toy.once('invalid', data => throw new Error('invalid data')); // listener discarded after one execution
toy.on('foo', bar);
toy.off('foo', bar); // removes listener
function bar () {}