Convert Figma logo to code with AI

ericgio logoreact-bootstrap-typeahead

React typeahead with Bootstrap styling

1,004
409
1,004
24

Top Related Projects

WAI-ARIA compliant React autosuggest component

12,045

🏎 A set of primitives to build simple, flexible, WAI-ARIA compliant React autocomplete, combobox or select dropdown components.

The Select Component for React.js

Quick Overview

React Bootstrap Typeahead is a React-based typeahead component that enhances form input with autocomplete functionality. It's built on top of React Bootstrap and provides a flexible, customizable solution for creating typeahead/autocomplete inputs in React applications.

Pros

  • Highly customizable with numerous options and callbacks
  • Supports both single and multi-select modes
  • Integrates well with React Bootstrap for consistent styling
  • Includes accessibility features out of the box

Cons

  • Requires React Bootstrap as a peer dependency, which may not fit all project needs
  • Learning curve for advanced customizations
  • Limited built-in styling options beyond React Bootstrap defaults

Code Examples

  1. Basic usage:
import { Typeahead } from 'react-bootstrap-typeahead';

const options = ['Apple', 'Banana', 'Cherry', 'Date'];

function Example() {
  return (
    <Typeahead
      id="basic-example"
      options={options}
      placeholder="Choose a fruit..."
    />
  );
}
  1. Multi-select mode:
import { Typeahead } from 'react-bootstrap-typeahead';

const options = ['Red', 'Blue', 'Green', 'Yellow'];

function MultiSelectExample() {
  return (
    <Typeahead
      id="multi-select-example"
      multiple
      options={options}
      placeholder="Choose colors..."
    />
  );
}
  1. Custom filtering:
import { Typeahead } from 'react-bootstrap-typeahead';

const options = [
  { name: 'John Doe', email: 'john@example.com' },
  { name: 'Jane Smith', email: 'jane@example.com' },
];

function CustomFilterExample() {
  return (
    <Typeahead
      id="custom-filter-example"
      labelKey="name"
      options={options}
      placeholder="Search users..."
      filterBy={['name', 'email']}
    />
  );
}

Getting Started

  1. Install the package and its peer dependencies:
npm install react-bootstrap-typeahead react-bootstrap bootstrap
  1. Import and use the component in your React application:
import React from 'react';
import { Typeahead } from 'react-bootstrap-typeahead';
import 'react-bootstrap-typeahead/css/Typeahead.css';

function App() {
  const options = ['Option 1', 'Option 2', 'Option 3'];

  return (
    <div className="App">
      <Typeahead
        id="basic-typeahead"
        options={options}
        placeholder="Choose an option..."
      />
    </div>
  );
}

export default App;

Make sure to import the CSS file for proper styling. You can customize the appearance further using React Bootstrap classes or your own CSS.

Competitor Comparisons

WAI-ARIA compliant React autosuggest component

Pros of react-autosuggest

  • More flexible and customizable, allowing for a wider range of use cases
  • Lightweight with minimal dependencies
  • Better accessibility support out of the box

Cons of react-autosuggest

  • Requires more setup and configuration for basic functionality
  • Less integrated with Bootstrap styling and components
  • May require additional work to achieve some features that come built-in with react-bootstrap-typeahead

Code Comparison

react-autosuggest:

import Autosuggest from 'react-autosuggest';

const renderSuggestion = suggestion => (
  <div>{suggestion.name}</div>
);

<Autosuggest
  suggestions={suggestions}
  onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  getSuggestionValue={getSuggestionValue}
  renderSuggestion={renderSuggestion}
  inputProps={inputProps}
/>

react-bootstrap-typeahead:

import { Typeahead } from 'react-bootstrap-typeahead';

<Typeahead
  id="basic-typeahead"
  labelKey="name"
  options={options}
  placeholder="Choose a state..."
/>

The code comparison shows that react-bootstrap-typeahead requires less setup for basic functionality, while react-autosuggest offers more granular control over rendering and behavior.

12,045

🏎 A set of primitives to build simple, flexible, WAI-ARIA compliant React autocomplete, combobox or select dropdown components.

Pros of downshift

  • More flexible and customizable, allowing for a wider range of use cases
  • Smaller bundle size, which can lead to better performance
  • Provides more low-level control over the component's behavior

Cons of downshift

  • Requires more setup and configuration to achieve a fully functional typeahead
  • Less out-of-the-box styling, which may require additional work for a polished look
  • Steeper learning curve due to its more generic nature

Code Comparison

downshift:

<Downshift
  onChange={selection => console.log(selection)}
  itemToString={item => (item ? item.name : '')}
>
  {({ getInputProps, getItemProps, isOpen, inputValue, selectedItem, highlightedIndex }) => (
    <div>
      <input {...getInputProps()} />
      {isOpen && (
        <div>
          {items
            .filter(item => !inputValue || item.name.includes(inputValue))
            .map((item, index) => (
              <div
                {...getItemProps({
                  key: item.id,
                  index,
                  item,
                  style: {
                    backgroundColor: highlightedIndex === index ? 'lightgray' : 'white',
                    fontWeight: selectedItem === item ? 'bold' : 'normal',
                  },
                })}
              >
                {item.name}
              </div>
            ))}
        </div>
      )}
    </div>
  )}
</Downshift>

react-bootstrap-typeahead:

<Typeahead
  id="basic-example"
  onChange={selected => console.log(selected)}
  options={options}
  placeholder="Choose a state..."
/>

The code comparison shows that downshift requires more setup but offers greater flexibility, while react-bootstrap-typeahead provides a more straightforward implementation with less customization options.

The Select Component for React.js

Pros of react-select

  • More customizable and flexible, with a wide range of styling options
  • Supports advanced features like async loading and multi-select
  • Larger community and more frequent updates

Cons of react-select

  • Steeper learning curve due to its extensive API
  • Larger bundle size, which may impact performance in some applications
  • Less integrated with Bootstrap styling out of the box

Code Comparison

react-select:

import Select from 'react-select';

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

<Select options={options} />

react-bootstrap-typeahead:

import { Typeahead } from 'react-bootstrap-typeahead';

const options = ['Chocolate', 'Strawberry', 'Vanilla'];

<Typeahead
  id="basic-typeahead"
  options={options}
  placeholder="Choose a flavor..."
/>

Both libraries offer powerful select/typeahead functionality for React applications. react-select provides more customization options and advanced features, making it suitable for complex use cases. react-bootstrap-typeahead integrates well with Bootstrap and offers a simpler API, which can be advantageous for projects already using Bootstrap or requiring a more straightforward implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Bootstrap Typeahead

A React-based typeahead that relies on Bootstrap for styling and was originally inspired by Twitter's typeahead.js. It supports both single- and multi-selection and is compliant with WAI-ARIA authoring practices. Try the live examples.

npm version npm downloads CI Codecov MIT License

Please note that documentation and examples apply to the most recent release and may no longer be applicable if you're using an outdated version.

Installation

npm install --save react-bootstrap-typeahead

or

yarn add react-bootstrap-typeahead

Include the module in your project:

import { Typeahead } from 'react-bootstrap-typeahead'; // ES2015
var Typeahead = require('react-bootstrap-typeahead').Typeahead; // CommonJS

UMD Build

Development and production builds are included in the NPM package. Alternatively, you can get them from CDNJS or unpkg.

Documentation

CSS

While the component relies primarily on Bootstrap, some additional styling is needed. You should include the provided CSS file in your project:

// Import as a module in your JS
import 'react-bootstrap-typeahead/css/Typeahead.css';

or

<!-- Link as a stylesheet in your HTML -->
<link
  rel="stylesheet"
  href="https://unpkg.com/react-bootstrap-typeahead/css/Typeahead.css"
/>

Bootstrap 5

In an effort to support Bootstrap 5, this package also contains a CSS file named Typeahead.bs5.css that should be included alongside the base CSS file above.

Examples

Try the live examples, which also include code samples. If you'd like to modify the examples, clone the repository and run npm install and npm start to build the example file. You can then open the HTML file locally in your browser.

You can also try out the following sandbox examples:

If you have an example use case that would be useful to others, please create a sandbox and submit a pull request to add it to the list!

Browser Support

Recent versions of the following browsers are supported:

  • Chrome
  • Firefox
  • Edge
  • Safari

Special thanks to BrowserStack for providing cross-browser testing support.

http://i.imgur.com/9aLP6Fx.png?1

License

MIT

NPM DownloadsLast 30 Days