moroshko /react-autosuggest

WAI-ARIA compliant React autosuggest component

★ 5,932⑂ 565👁 55⑃ 171● 668▤ 44
JavaScriptMITCreated 2015-03-07react-autosuggest.js.org
下载:v10.0.2v10.0.2 ↓10.0.22020-05-02
+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 s

Top issues

Latest release

v10.0.2 v10.0.2 2020-05-02
Fixes 738

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON