1.0.0 • Published 4 years ago

tokeninput v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

Introducing tokeninput

Dependency-free Javascript UI component providing a token based input element with grouped completion lists and free-text entry

Usage

Create an instance of the TokenInput class with two parameters:

  • an instance of an Input DOM element
  • a configuration object, to tailor the component to your needs
<script src="tokeninput/tokeninput.js" />
<div id="tokeninput">
  <input placeholder="Type something…" />
</div>
new TokenInput( document.getElementById( 'tokeninput' ), {} );

See index.html and demo.js for examples.

There are lots of options, see tokeninput.js

API

MethodDescription
addEventListener(element, type, listener)Adds an event listener to the given element.
getTokens()Returns an array of objects representing each token added to the component.
getSelectedCompletion()Returns the datum of the selected completion
setSelectedCompletion(datum)Sets the selected completion
getSelectedCompletionElement()Returns the element of the selected completion
removeCompletions()Removes all the completions
setTokens(newTokens)Removes all existing tokens from the UI and replaces them with the tokens in the given array.
setCompletionGroups(completionGroups)Configures the completion groups that visually separate suggestions.
removeFloatingElement()Hides the suggestion UI.
removeToken(datum, options)Removes the given token from the component UI and the component's internal list of tokens.
positionFloatingElement()Force the floating element to reposition
setElementAfterCompletions(element)Set the element to place after completions
setElementBeforeCompletions(element)Set the element to place before completions
getScrollingContainer()Get the current scrolling container element
onUp()Trigger the up key handler
onDown()Trigger the down key handler
destroy()Cleans up by removing all registered event listeners.

Completions / suggestions

Use the following options to drive how completions are presented:

OptionDescription
completionsForTextFunction that receives the text, delayedCompletionsId and delayedCompletionsFn. Returns datums (objects) for each completion
completionClassNamesFunction that receives a datum to determine it's class name
completionFormatterFunction that receives a datum and corresponding element for further customisation
completionGroupClassNamesFunction that receives the completion group, returns an array of class names
completionGroupHeadingClassNamesFunction that receives the completion group heading, returns an array of class names
completionGroupsObject of completion group definitions, see demo.js
newCompletionOptionFunction that receives a group and returns the datum for the 'new' completion, defaults to "+ New (group.singular)"

Other options

OptionDescriptionDefault
readOnlyPrevents tokens from being added / removedfalse
tabToAddPressing the tab key submits the current token and allows a new one to be enteredtrue
xHTMLA string which represents the delete button in a token&times;
tokenClassNamesA function which is called for each added token. Should return an array of class names that will be added to the token HTML element.[]
tokenFormatterA function which is called for each added token. Can be used to customise the token element for display when not in inline mode
inlineTokenFormatterA function which is called for each added token. Can be used to customise the token element for display when in inline mode
containerClickTriggersFocusDetermines if a click on the containing element will focus the tokeninputtrue
freeTextEnabledDetermines whether the user is able to enter arbitrary tokens (true) or forced to select a pre-existing datum (false)false
freeTextTokenA function that determines the makeup of a datum created via free-text
freeTextCompletionA function that determines the makeup of an object that represents free-text entry which is passwd to the suggestion handler
willShowFreeTextCompletionA function that determines whether or not to show the completion view for free-text
inlineTokenTriggerAn object which defines logic for determining whether to show completion for typed text when in inline mode
namespaceA string or function to namespace element class namesnull
autoSelectSingleCompletionsSelect the first and only completion automaticallytrue
positionFloatingElementA function that is passed the floating element for manual positioningnull
floatingElementParentAn element to use as the floating elements parent node, defaults to the input elements parent nodenull
removeOnlyCompletionsListElementSet to true to only remove the list element rather than the whole floating elementfalse
beforeEnterPre-keypress event handlernull
beforeCompletionClickPre-click event handlernull
hintElementAn element to use as a hint containernull
scrollingContainerClassNameA selector for the scrolling container, by default the floating element is usednull
hintAfterAddShow the hint element after adding a tokenfalse
disableTokenClickDisable the build-in token click handerfalse
disableFocusOnRemoveDisable the automatic behaviour that focuses the input element after removing a tokenfalse
placeholderLengthSpecify the placeholder length in charactersnull