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.
Check out the Homepage and the Codepen examples.
yarn add react-autosuggest
or
npm install react-autosuggest --save
You can also use the standalone UMD build:
<script src="https://unpkg.com/react-autosuggest/dist/standalone/autosuggest.js"></script>
import React from 'react';
import Autosuggest from 'react-autosuggest';
// Imagine you have a list of languages that you'd like to autosuggest.
const languages = [
{
name: 'C',
year: 1972
},
{
name: 'Elm',
year: 2012
},
...
];
// Teach Autosuggest how to calculate suggestions for any given input value.
const getSuggestions = value => {
const inputValue = value.trim().toLowerCase();
const inputLength = inputValue.length;
return inputLength === 0 ? [] : languages.filter(lang =>
lang.name.toLowerCase().slice(0, inputLength) === inputValue
);
};
// When suggestion is clicked, Autosuggest needs to populate the input
// based on the clicked suggestion. Teach Autosuggest how to calculate the
// input value for every given suggestion.
const getSuggestionValue = suggestion => suggestion.name;
// Use your imagination to render suggestions.
const renderSuggestion = suggestion => (
{suggestion.name}
);
class Example extends React.Component {
constructor() {
super();
// Autosuggest is a controlled component.
// This means that you need to provide an input value
// and an onChange handler that updates this value (see below).
// Suggestions also need to be provided to the Autosuggest,
// and they are initially empty because the Autosuggest is closed.
this.state = {
value: '',
suggestions: []
};
}
onChange = (event, { newValue }) => {
this.setState({
value: newValue
});
};
// Autosuggest will call this function every time you need to update suggestions.
// You already implemented this logic above, so just use it.
onSuggestionsFetchRequested = ({ value }) => {
this.setState({
suggestions: getSuggestions(value)
});
};
// Autosuggest will call this function every time you need to clear suggestions.
onSuggestionsClearRequested = () => {
this.setState({
suggestions: []
});
};
render() {
const { value, suggestions } = this.state;
// Autosuggest will pass through all these props to the input.
const inputProps = {
placeholder: 'Type a programming language',
value,
onChange: this.onChange
};
// Finally, render it!
return (
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
/>
);
}
}
| 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:
```js const suggestions = [ { title: "A", suggestions: [ { id: "100", text: "Apple" }, { id: "101", text: "Apricot" } ] }, { title: "B", suggestions: [ { id: "102", text: "Banana" } ] }, { title: "C", suggestions: [ {
$ claude mcp add react-autosuggest \
-- python -m otcore.mcp_server <graph>