A React component for Autocomplete Hint.
Demo can be found here: https://ejmudi.github.io/react-autocomplete-hint/
npm install --save react-autocomplete-hint
or
yarn add react-autocomplete-hint
import { Hint } from 'react-autocomplete-hint';
const options = ["orange", "banana", "apple"];
// OR
const options = [
{id: 1, label: "orange"},
{id: '2', label: "banana"},
{id: 3, label: "apple"}
];
<Hint options={options}>
<input
value={text}
onChange={e => setText(e.target.value)} />
</Hint>
Click on the hint or use your keyboard Right key or Tab key(if allowTabFill
is set to true) to fill your input with the suggested hint.
If you're using objects for your options. object schema is as follows:
Returns the option selected immediately the input is filled with the suggested hint.
Note that it won't return the selected option with the casing the user typed, rather it returns the option with the casing specified in your options prop. For example, if the options are specified like this:...
const options = ["orange", "banana", "apple"];
...and the input gets filled with "ORange", onFill will still return "orange".
If you are using objects for your options, You may have unexpected results if your data options contain objects with duplicate labels. For this reason, it is highly recommended that you pass in objects with unique labels if possible.
For example, if you pass in optionsWithDuplicateLabels
as seen below and you then fill the input with the orange hint, the orange will be the first orange object found in the array as can be seen in the onFill
prop:
const optionsWithDuplicateLabels = [
{id: "1", label: "orange"},
{id: "2", label: "orange"},
{id: "3", label: "banana"}
];
<Hint options={optionsWithDuplicateLabels} onFill={value=> {
// will always log {id: "1", label: "orange"} when orange is selected
// {id: "2", label: "orange"} will never be logged.
console.log(value);
}}>
<input
value={text}
onChange={e => setText(e.target.value)} />
</Hint>
Inspired by React Bootstrap Typeahead.