A typeahead component for inputs
Parameters
elHTMLInputElement A valid HTML input elementdataArray An array of data used for resultsoptionsObjectoptions.limit[Number] Max number of results to display in the auto suggest list. (optional, default5)options.minLength[Number] Number of characters typed into an input to trigger suggestions. (optional, default2)
Examples
// in the browser
var input = document.querySelector('input');
var data = [
'Roy Eldridge',
'Roy Hargrove',
'Rex Stewart'
];
new Suggestions(input, data);
// with options
var input = document.querySelector('input');
var data = [{
name: 'Roy Eldridge',
year: 1911
}, {
name: 'Roy Hargrove',
year: 1969
}, {
name: 'Rex Stewart',
year: 1907
}];
var typeahead = new Suggestions(input, data, {
filter: false, // Disable filtering
minLength: 3, // Number of characters typed into an input to trigger suggestions.
limit: 3 // Max number of results to display.
});
// As we're passing an object of an arrays as data, override
// `getItemValue` by specifying the specific property to search on.
typeahead.getItemValue = function(item) { return item.name };
input.addEventListener('change', function() {
console.log(typeahead.selected); // Current selected item.
});
// With browserify
var Suggestions = require('suggestions');
new Suggestions(input, data);Returns Suggestions this
Clears data
For a given item in the data array, return what should be used as the candidate string
Parameters
itemObject or String an item from the data array
Returns String item
Evaluates whether an array item qualifies as a match with the current query
Parameters
candidateString a possible item from the array passedqueryString the current query
Returns Boolean
Normalize the results list and input value for matching
Parameters
valueString
Returns String
For a given item in the data array, return a string of html that should be rendered in the dropdown
Parameters
itemObject or String an item from the data arraysourceFormattingString a string that has pre-formatted html that should be passed directly through the render function
Returns String html
Update data previously passed
Parameters
revisedDataArray