react-bootstrap-typeahead

repository·main·Indexed 21 days ago

https://github.com/ericgio/react-bootstrap-typeahead

A React-based typeahead component with Bootstrap styling that supports single and multi-selection. The library is WAI-ARIA compliant and provides components such as <Typeahead>, <AsyncTypeahead> for API-driven searches, and <Highlighter> for visual match highlighting. It includes hooks and HOCs like useAsync, useItem, and useToken for customizing inputs, menus, and tokens.

Tokens
17.8K
Snippets
64
Records
84
Agent score
73%

What's inside react-bootstrap-typeahead

  1. Public API components and HOCs in react-bootstrap-typeahead

    main

    The library exposes several components and higher-order components (HOCs) through its top-level module. Use these public interfaces for building typeahead functionality. Any components not listed below should be considered private and subject to change.

    Components:

    • <Typeahead>
    • <AsyncTypeahead>
    • <Highlighter>
    • <Hint>
    • <Input>
    • <Menu>
    • <MenuItem>
    • <TypeaheadInputSingle> & <TypeaheadInputMulti>
    • <TypeaheadMenu>
    • <Token>

    Higher-Order Components & Hooks:

    • useAsync & withAsync
    • useItem & withItem
    • useToken & withToken
    • useHint
  2. Format data for Typeahead components

    main

    The react-bootstrap-typeahead component accepts an array of either strings or objects as the options prop.

    • Strings: Use a simple Array<String> if you only need to display and select text values.
    • Objects: Use an Array<Object> if your data contains metadata. If using objects, each object must contain a string property to serve as the display label.

    Important: The component will throw an error if any item in the array is not a string or an object with a valid labelKey.

    // Array of strings
    var options = ['John', 'Miles'];
    
    // Array of objects (using default 'label' key)
    var options = [{id: 1, label: 'John'}, {id: 2, label: 'Miles'}];
  3. Control Typeahead selections

    main

    You can manage the component's state in two ways:

    Use the selected prop to drive the component's state from a parent component. This is the recommended approach for managing form state.

    Uncontrolled

    Use the defaultSelected prop to provide initial values. After the initial render, the component manages its own internal state.

    Note: You can control the selections (the items chosen), but you cannot control the raw input text value via these props.

    // Controlled Example
    <Typeahead
      onChange={(selected) => {
        this.setState({selected});
      }}
      options={[...]}
      selected={this.state.selected}
    />
    
    // Uncontrolled Example
    <Typeahead
      defaultSelected={[...]}
      onChange={(selected) => {
        // Handle selections...
      }}
      options={[...]}
    />
  4. Basic Usage of Typeahead

    main

    The Typeahead component functions like a standard form element. To use it, provide an options prop containing an array of strings or objects. Use the onChange callback to handle the user's selections.

    <Typeahead
      onChange={(selected) => {
        // Handle selections...
      }}
      options={[ /* Array of objects or strings */ ]}
    />
  5. Migrate `filterBy` callback signature in v3.0

    main

    In version 3.0, the filterBy callback signature changed. The second parameter is no longer the user-input text string; it is now an object containing internal props. To access the text value, use props.text.

    // v2.0
    <Typeahead
      ...
      filterBy={(option, text) => {
        // Your own filtering code goes here.
      }}
    />
    
    // v3.0
    <Typeahead
      ...
      filterBy={(option, props) => {
        // Your own filtering code goes here.
        // `text` is now `props.text`
      }}
    />
  6. Implement custom menu rendering with `innerRef` in v3.0

    main

    Version 3.0 uses Popper.js for menu positioning. If you use a custom component for the renderMenu prop, your component must consume the innerRef prop passed down by Popper and attach it to your component's ref to ensure correct positioning.

    class MyCustomMenu extends React.Component {
      render() {
        // `innerRef` is passed down by the Popper...
        const {innerRef, ...props} = this.props;
    
        // ...and must be passed to the `ref` of your custom component.
        return <div {...props} ref={innerRef} />;
      }
    }
  7. Install react-bootstrap-typeahead

    main

    You can install the package using npm or yarn. This package provides a React-based typeahead component that relies on Bootstrap for styling and supports both single- and multi-selection while being WAI-ARIA compliant.

    npm install --save react-bootstrap-typeahead
    # or
    yarn add react-bootstrap-typeahead
  8. Include the required CSS

    main

    Import as a module in your JS

    import 'react-bootstrap-typeahead/css/Typeahead.css';
    // For Bootstrap 5, also include:
    import 'react-bootstrap-typeahead/css/Typeahead.bs5.css';
    <link
      rel="stylesheet"
      href="https://unpkg.com/react-bootstrap-typeahead/css/Typeahead.css"
    />
    <!-- For Bootstrap 5, also include:
    <link
      rel="stylesheet"
      href="https://unpkg.com/react-bootstrap-typeahead/css/Typeahead.bs5.css"
    /> -->
    import 'react-bootstrap-typeahead/css/Typeahead.css';