WAI-ARIA compliant React autosuggest component
WAI-ARIA compliant React autosuggest component
Looking for maintainers!
Unfortunately, I don't have the time to maintain this project anymore. If you are interested to help, please reach out to me on Twitter @moroshko.
React-Autosuggest Bot will help you understand this repository better. You can ask for code examples, installation guide, debugging help and much more.
Check out the Homepage and the Codepen examples.
yarn add react-autosuggestor
npm install react-autosuggest --saveYou can also use the standalone UMD build:
<script src="https://unpkg.com/react-autosuggest/dist/standalone/autosuggest.js"></script>…| Prop | Type | Required | Description |
|---|---|---|---|
suggestions |
Array | ✓ | These are the suggestions that will be displayed. Items can take an arbitrary shape. |
onSuggestionsFetchRequested |
Function | ✓ | Will be called every time you need to recalculate suggestions. |
onSuggestionsClearRequested |
Function | ✓* | Will be called every time you need to set suggestions to []. |
getSuggestionValue |
Function | ✓ | Implement it to teach Autosuggest what should be the input value when suggestion is clicked. |
renderSuggestion |
Function | ✓ | Use your imagination to define how suggestions are rendered. |
inputProps |
Object | ✓ | Pass through arbitrary props to the input. It must contain at least value and onChange. |
containerProps |
Object | Pass through arbitrary props to the container. Useful if you need to override the default props set by Autowhatever, for example, for accessibility. | |
onSuggestionSelected |
Function | Will be called every time suggestion is selected via mouse or keyboard. | |
onSuggestionHighlighted |
Function | Will be called every time the highlighted suggestion changes. | |
shouldRenderSuggestions |
Function | When the input is focused, Autosuggest will consult this function when to render suggestions. Use it, for example, if you want to display suggestions when input value is at least 2 characters long. | |
alwaysRenderSuggestions |
Boolean | Set it to true if you'd like to render suggestions even when the input is not focused. |
|
highlightFirstSuggestion |
Boolean | Set it to true if you'd like Autosuggest to automatically highlight the first suggestion. |
|
focusInputOnSuggestionClick |
Boolean | Set it to false if you don't want Autosuggest to keep the input focused when suggestions are clicked/tapped. |
|
multiSection |
Boolean | Set it to true if you'd like to display suggestions in multiple sections (with optional titles). |
|
renderSectionTitle |
Function | ✓ when multiSection={true} |
Use your imagination to define how section titles are rendered. |
getSectionSuggestions |
Function | ✓ when multiSection={true} |
Implement it to teach Autosuggest where to find the suggestions for every section. |
renderInputComponent |
Function | Use it only if you need to customize the rendering of the input. | |
renderSuggestionsContainer |
Function | Use it if you want to customize things inside the suggestions container beyond rendering the suggestions themselves. | |
theme |
Object | Use your imagination to style the Autosuggest. | |
id |
String | Use it only if you have multiple Autosuggest components on a page. |
Array of suggestions to display. The only requirement is that suggestions is an array. Items in this array can take an arbitrary shape.
For a plain list of suggestions, every item in suggestions represents a single suggestion. It's up to you what shape every suggestion takes. For example:
const suggestions = [
{
text: "Apple"
},
{
text: "Banana"
},
{
text: "Cherry"
},
{
text: "Grapefruit"
},
{
text: "Lemon"
}
];For multiple sections, every item in suggestions represents a single section. Again, it's up to you what shape every section takes. For example:
const suggestions = [
{
title: "A",
suggestions: [
{
id: "100",
text: "Apple"
},
{
id: "101",
text: "Apricot"
}
]
},
{
title: "B",
suggestions: [
{
id: "102",
text: "Banana"
}
]
},
{
title: "C",
suggestions: [
{
id: "103",
text: "Cherry"
}
]
}
];This function will be called every time you might need to update suggestions. It has the following signature:
function onSuggestionsFetchRequested({ value, reason })where:
value - the current value of the inputreason - string describing why onSuggestionsFetchRequested was called. The possible values are:'input-changed' - user typed something'input-focused' - input was focused'escape-pressed' - user pressed Escape to clear the input (and suggestions are shown for empty input)'suggestions-revealed' - user pressed Up or Down to reveal suggestions'suggestion-selected' - user selected a suggestion when alwaysRenderSuggestions={true}alwaysRenderSuggestions={true})This function will be called every time you need to clear suggestions.
All you have to do in this function is to set suggestions to [].
<a name="on-suggestions-clear-requested-
No open issues yet, or sync has not completed.