Skip to content

colorfulfool/suggestive-text-field

Repository files navigation

Suggestive Text Field

The most basic autocomplete input on the web. Comes as a single JS file that doesn't depend on anything.

  textInput = document.querySelector('input[name=tags]')
  new SuggestiveTextField(textInput, ['monster', 'monstrosity', 'trap'])

demonstration

Okay, but my autocomplete needs to fetch suggestions dynamically

new SuggestiveTextField(textInput, null,
  suggestionsForToken: (token) =>
    new Promise((resolve, reject) => 
      $.getJSON(remoteUrlFor(token), (response) => resolve(response.results))
)

Options

SuggestiveTextField(textInput, suggestions, {

  tokenSeparator: ', ', // pass false to disallow multiple selections whatsoever
  
  suggestionsForToken: function (token) {
    // return suggestions that start with `token`
  },
  onConfirmHook: function (selectedSuggestion) {
    // do something extra when a suggestion is selected
  }
})

Development

blade runner to run the tests in the browser
blade build to re-compile the dist/

About

Most basic autocomplete field on the web

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published