React Autosuggest

10.1.0 · maintenance · verified Sun Apr 19

React Autosuggest is a WAI-ARIA compliant React component designed to provide robust autosuggest and autocomplete functionality. It allows for highly customizable suggestion lists, including asynchronous data fetching, sectioned suggestions, and full control over the rendering of both suggestions and the input field itself. The current stable version is 10.1.0. While it has been widely adopted, the project is currently in a maintenance phase and is actively seeking new maintainers, which may affect its release cadence. Key differentiators include its strong emphasis on accessibility (WAI-ARIA compliance), extensive flexibility in styling (supporting CSS Modules, Radium, Aphrodite, JSS), and its comprehensive customization options, making it suitable for integration with state management libraries like Redux.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic setup of `react-autosuggest` with static suggestions, including how to define `getSuggestions`, `getSuggestionValue`, `renderSuggestion`, and manage component state.

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 => (
  <div>
    {suggestion.name}
  </div>
);

class MyAutosuggest extends React.Component {
  constructor() {
    super();
    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 inputProps 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}
      />
    );
  }
}

export default MyAutosuggest;

view raw JSON →