Typeahead-standalone.js
![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg?logo=paypal)
A fast fully-featured standalone autocomplete library
🌟 Highlights 🌟
- 🚀 Blazing fast suggestions and autocompletion
- 📦 Has 0 DEPENDENCIES! Written in pure JS (typescript)
- 🎀 Framework agnostic! Usable with any framework (React, Vue, Svelte, etc)
- 💡 Highly customizable and light-weight
![~4kb minzipped](https://badgen.net/bundlephobia/minzip/typeahead-standalone)
- ⚜️ In-built support for multiple data sources - Local, Prefetch and Remote (requests rate-limited by default)
- ⚡️ Suggestions calculated via a very efficient algorithm based on trie data structure
- ♿️ WAI-ARIA compliant design pattern with support for language diacritics
- 🌐 Supports every major browser!
🔥 Demo/Docs
Find here detailed Docs with Live Demos for typeahead-standalone.js.
Preview of a basic example:
![Basic example](https://github.com/digitalfortress-tech/typeahead-standalone/raw/HEAD/./demo/basic-ex.gif)
# you can install typeahead with npm
$ npm install --save typeahead-standalone
# Alternatively you can use Yarn
$ yarn add typeahead-standalone
Then include the library in your App/Page.
As a module,
import typeahead from 'typeahead-standalone';
import 'typeahead-standalone/dist/basic.css';
const typeahead = require('typeahead-standalone');
require('typeahead-standalone/dist/basic.css');
In the browser context,
<link rel="stylesheet" href="./node_modules/typeahead-standalone/dist/basic.css" />
<script src="./node_modules/typeahead-standalone/dist/typeahead-standalone.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeahead-standalone/dist/basic.css" />
<script src="https://cdn.jsdelivr.net/npm/typeahead-standalone"></script>
<link rel="stylesheet" href="https://unpkg.com/typeahead-standalone@4.14.1/dist/basic.css" />
<script src="https://unpkg.com/typeahead-standalone@4.14.1/dist/typeahead-standalone.js"></script>
The library will be available as a global object at window.typeahead
🌱 Usage
Typeahead requires an input
Element to attach itself to, and a Data source
(local/remote) to display suggestions.
Here is a very basic example (See demo for advanced examples)
Html
<script src="..." async></script>
<input type="search" id="searchInput" autocomplete="off" placeholder="Search...">
Javascript
const colors = ['Grey', 'Brown', 'Black', 'Blue'];
const inputElement = document.getElementById("searchInput");
typeahead({
input: inputElement,
source: {
local: colors,
}
});
You can pass the following config options to typeahead-standalone
:
Parameter | Description | Default |
---|
input | DOM input element must be passed with this parameter and typeahead will attach itself to this field. | - (Required) |
source | This is the source of Data from which suggestions will be calculated. The source can be local, prefetched or retrieved from a remote endpoint. Details | - (Required) |
minLength | Specify the minimum length, when suggestions should appear on the screen. | 1 |
limit | Specify the maximum number of suggestions that should be displayed. | 5 |
highlight | If set to true, the matched letters are highlighted in the list of suggestions. A class tt-highlight is added to facilitate styling | undefined |
autoSelect | If set to true, pre-selects the first displayed suggestion | false |
hint | Updates the input placeholder to be equal to the first matched suggestion. A class tt-hint is added to facilitate styling | true |
diacritics | Flag to enable/disable language diacritics supported search (i.e. search by converting accented characters into their non-accented counterparts) | undefined |
className | The typeahead-standalone container will have this class name (in addition to the default class typeahead-standalone ) | undefined |
templates | An object containing templates for header, footer, suggestion, group and notFound state. See templates section for clarification | undefined |
debounceRemote | Delays execution of making Ajax requests (in milliseconds) | 100 |
preventSubmit | If your input element is used inside a form element, this flag allows to prevent the default submit action when the ENTER key is pressed. | false |
onSubmit(event, selectedItem?) | When you want to use typeahead outside a form element, this handler can be used to process/submit the input value. Gets triggered on hitting the ENTER key. First parameter is the keyboard Event and the 2nd parameter is the selected item or undefined if no item was selected | undefined |
display(selectedItem, event?) => string | This callback is executed when the user selects an item from the suggestions. The current suggestion/item is passed as a parameter and it must return a string which is set as the input's value. The 2nd optional parameter event is a Mouse/Keyboard event which can be used to track user interaction or for analytics. It defaults to null . | Returns the string representation of the selected item |
🔌 Source
This is the source of data from which suggestions will be provided. This is the expected format of the source object.
source: {
local: [],
remote: {
url: 'https://remoteapi.com/%QUERY', // OR `url: () => 'https://remoteapi.com/%QUERY',`
wildcard: '%QUERY',
requestOptions: {} // optional, default => undefined
},
prefetch: {
url: 'https://remoteapi.com/load-suggestions', // OR `url: () => string`
when: 'onFocus', // optional, default => 'onInit'
done: false, // optional, default => false
process: (items) => void, // optional, default => undefined
requestOptions: {} // optional, default => undefined
},
identifier: '...', // optional (required when source => Object[])
identity: (item) => string, // optional (determines uniqueness of each suggestion)
dataTokens: ['...'], // optional
groupIdentifier: '...', // optional, default => undefined
transform: function (data) {
// modify received data if needed & return it
return data;
}
}
- Local: The
local
data source is used when you want to provide suggestions from a local source like a variable. - Prefetch: The
prefetch
data source is used when you want to preload suggestions from a remote endpoint in advance. You must provide the url
parameter that points to the endpoint that will return suggestions. You can provide an optional when
parameter which defines when the prefetch request should occur. It defaults to onInit
meaning that suggestions will be preloaded as soon as typeahead gets initialized. You can set it to onFocus
which will cause suggestions to be preloaded only when the user focuses the search input box. The done
flag is optional & can be used to disable the prefetch request programmatically. Its default value is false
. It gets set to true
automatically when data is prefetched for the first time (to prevent multiple network requests). By setting done: true
, the prefetch request will not occur. An example use-case to do this is when you are using localStorage to store suggestions but the localStorage already had stored suggestions previously thereby eliminating the need to prefetch data again. The process(suggestions)
callback is optional. It gets executed after the prefetch request occurs. It receives the transformed suggestions as a parameter & as an example can be used to store the received suggestions in localStorage to be used later. - Remote: The
remote
data source is used when you want to interrogate a remote endpoint to fetch data. - Wildcard: While using the
remote
data source, you must set the url
and the wildcard
options. wildcard
will be replaced with the search string while executing the request. - RequestOptions: The fetch API is used to query remote endpoints. You may provide an object of requestOptions to customize the outgoing request. By default the query type is
GET
. - Transform: You can provide an optional
transform()
function which gets called immediately after the prefetch/remote endpoint returns a response. You can modify the response before it gets processed by typeahead. - Identifier: An
identifier
is required when the data source is an array of objects. An identifier
is used to identify which property of the object should be used as the text for displaying the suggestions. For example, lets say the data source is something like this:
[
{ id: 1, color: "Yellow", colorCode: "YW" },
{ id: 2, color: "Green", colorCode: "GN", shade: "Greenish" },
{ id: 3, color: "Olive", colorCode: "OV", shade: "Greenish" },
...
]
Now if we wish to use the the text defined in the color
property to appear as the suggestions, then the identifier must be set to color. (i.e. identifier: "color"
)
- dataTokens:
dataTokens: string[]
is an optional property. It accepts an array of strings which represent the properties of the source object that should be added to the search index. This can be best understood with an example. Lets take the same example data source as shown above. What if you wanted to search colors by another property(colorCode) and not just by its identifier(color) ? That's exactly where dataTokens comes in. Set dataTokens: ["colorCode"]
. If you now search for "YW", the suggestion "Yellow" pops up as expected. - groupIdentifier: If you wish to group your suggestions, set the groupIdentifier property. This is an optional property. Again, going with the same example data source as above, when you set
groupIdentifier: "shade"
, suggestions will be grouped by the property "shade". In this example, the colors Green and Olive will appear under the group "Greenish" (shade
) whereas the color Yellow will have no group. - identity: The
identity()
function is used to determine uniqueness of each suggestion. It receives the suggestion as a parameter and must return a string unique to the given suggestion. This is an optional property and it defaults to returning the identifier
. However, the default value might not work everytime. For example, consider the following code -
[
{ title: "God is Good", artist: "Don Moen" },
{ title: "God is Good", artist: "Paul Wilbur" },
{ title: "God is Good", artist: "Micheal Smith" },
{ title: "El Shaddai", artist: "Amy Grant" },
...
]
Lets assume the identifier is set to title
. By default the identity()
function uses the identifier property (i.e. the title) to determine uniqueness. So if you search for God
, you will find only 1 suggestion displayed since there are 3 songs with the exact same title
property. In order to show all 3 suggestions with different artists, you need to set the identity
property such that it returns a unique string -
identity(item) => `${item.title}${item.artist}`;
It is highly recommended to use the identity()
property to return a unique string when your data source is an array of Objects.
Checkout the Live Examples for further clarification.
🎨 Styling (css)
Some basic styling is provided with typeahead. The UI is completely upto you and is customizable to the very pixel. You can use the following classes to add/override styles.
- The entire html is wrapped in a container with a class
typeahead-standalone
. - The input element has a
tt-input
class. - The hint element has a
tt-hint
class. - The list of suggestions is wrapped in a container with a
tt-list
class. (A class tt-hide
is added when no suggestions are available) - Each suggestion has a class
tt-suggestion
and if the suggestion is selected, then it has a tt-selected
class additionally. - If the highlight config option is set to true, every highlighted text block has a
tt-highlight
class.
Styling upto version 3.x.x
You can add your own styles by targetting the parent selector .typeahead-standalone
. For example, we can update the background color of every suggestion as seen below -
.typeahead-standalone .tt-list .tt-suggestion {
background-color: green;
}
To override default styling, set the config option className
and use it as a selector. Lets say you set className: "my-typeahead"
, then to override style on hovering/selecting a suggestion, you could use:
.typeahead-standalone.my-typeahead .tt-list .tt-suggestion:hover,
.typeahead-standalone.my-typeahead .tt-list .tt-suggestion.tt-selected {
color: black;
background-color: white;
}
Styling for version 4.x.x and above
Starting with v4.0
, the JS and CSS has been separated allowing greater control over the style. The entire css can be retrieved either from the CDN or from below and be copied directly into your project allowing you to discard/override any styles as necessary.
.typeahead-standalone {
position: relative;
text-align: left;
color: #000;
}
.typeahead-standalone .tt-input {
z-index: 1;
background: transparent;
position: relative;
}
.typeahead-standalone .tt-hint {
position: absolute;
left: 0;
cursor: default;
user-select: none;
background: #fff;
color: silver;
z-index: 0;
}
.typeahead-standalone .tt-list {
background: #fff;
z-index: 1000;
box-sizing: border-box;
overflow: auto;
border: 1px solid rgba(50, 50, 50, 0.6);
}
.typeahead-standalone .tt-list.tt-hide {
display: none;
}
.typeahead-standalone .tt-list div[class^="tt-"] {
padding: 0 4px;
}
.typeahead-standalone .tt-list .tt-suggestion:hover,
.typeahead-standalone .tt-list .tt-suggestion.tt-selected {
background: #55acee;
cursor: pointer;
}
.typeahead-standalone .tt-list .tt-suggestion .tt-highlight {
font-weight: 900;
}
.typeahead-standalone .tt-list .tt-group {
background: #eee;
}
You can also use templates to add a header, footer and further style each suggestion.
💫 Templates
Templates can be used to customize the rendering of the List. Their usage is completely optional. Currently, there are 5 templates available -
templates: {
header: () => '<h1>List of Countries</h1>',
footer: () => '<div>See more</div>',
suggestion: (item) => {
return `<div class="custom-suggestion">${item.label}</div>`;
},
group: (groupName) => {
return `<div class="custom-group">${groupName}</div>`;
},
notFound: () => '<span>Nothing Found</span>',
}
As seen above, each template takes a callback that must return a string
which is later interpreted as HTML.
Each template is wrapped in a div
element with a corresponding class. i.e.
header
=> class tt-header
footer
=> class tt-footer
suggestion
=> class tt-suggestion
group
=> class tt-group
notFound
=> class tt-notFound
✨ API
Destroys the typeahead instance, removes all event handlers and cleans up the DOM. Can be used once when you no more want to have suggestions.
const instance = typeahead({ });
instance.destroy();
💡 Error Codes
Here is a small glossary of the possible errors codes that one may come across
Code | Description |
---|
e01 | Missing input DOM element |
e02 | Missing/Incorrect source of suggestions. You must provide atleast one of the 3 possible sources - local, prefetch or remote with the expected source format (Ref) |
e03 | Identifier not defined |
e04 | Prefetch request failed |
e05 | Remote request failed |
🧑💻 Contribute
Interested in contributing features and fixes?
Read more on contributing.
📝 Changelog
See the Changelog
📄 License
MIT © DigitalFortress