moroshko /react-autosuggest
WAI-ARIA compliant React autosuggest component
★ 5,932⑂ 565👁 55⑃ 171● 668▤ 44
+0
日增长
5,932
stars
565
forks
55
watchers
171
prs
668
issues
44
releases
README
!gzip size
React Autosuggest
Project Status
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 AI Bot
React-Autosuggest Bot will help you understand this repository better. You can ask for code examples, installation guide, debugging help and much more.
Demo
Check out the Homepage and the Codepen examples.
Features
- WAI-ARIA compliant, with support for ARIA attributes and keyboard interactions
- Mobile friendly
- Plugs in nicely to Flux and Redux applications
- Full control over suggestions rendering
- Suggestions can be presented as plain list or multiple sections
- Suggestions can be retrieved asynchronously
- Highlight the first suggestion in the list if you wish
- Supports styling using CSS Modules, Radium, Aphrodite, JSS, and more
- You decide when to show suggestions (e.g. when user types 2 or more characters)
- Always render suggestions (useful for mobile and modals)
- Pass through arbitrary props to the input (e.g. placeholder, type, onChange, onBlur, or any other), or take full control on the rendering of the input (useful for integration with other libraries)
- Thoroughly tested
Installation
shell
yarn add react-autosuggest
or
shell
npm install react-autosuggest --save
You can also use the standalone UMD build:
html
Basic Usage
js
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 sTop issues
#368Uncaught TypeError: Cannot read property 'trim' of undefined20 comments · 2017-05-21#835Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended16 comments · 2021-12-09#646Uncaught TypeError: Cannot read property 'focus' of undefined15 comments · 2019-06-07#370Allow blur() on component15 comments · 2017-05-23#358Best way to render differently each sections?11 comments · 2017-04-24#349Multiple Autocompletes on one page11 comments · 2017-03-30#400Documentation needs instructions on how to theme10 comments · 2017-07-07#699Is there a way to append the suggestions container to the body?9 comments · 2019-12-24
Latest release
v10.0.2 v10.0.2 2020-05-02Fixes 738