React Fuzzy Search Component

1.3.1 · active · verified Sun Apr 19

react-fuzzy is a React component that provides a customizable fuzzy search input field and results dropdown. It enables developers to integrate powerful fuzzy search capabilities into their React applications with minimal setup, abstracting away the underlying search logic. The current stable version is 1.3.1, which includes compatibility updates for React 17 and 18. The package demonstrates a moderate release cadence, with minor updates addressing new features, bug fixes, and peer dependency compatibility. Key differentiators include extensive customization options for the UI (via `resultsTemplate`, `inputProps`, and various style props) and the ability to configure the underlying fuzzy search algorithm parameters like `threshold`, `distance`, and `keys` to search within specific object properties.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the FuzzySearch component with a predefined list of objects, including state management for selected items and an example of a custom result template for enhanced UI.

import React, { useState } from 'react';
import { FuzzySearch } from 'react-fuzzy';

function BookSearchComponent() {
  const list = [{
    id: 1,
    title: 'The Great Gatsby',
    author: 'F. Scott Fitzgerald'
  }, {
    id: 2,
    title: 'The DaVinci Code',
    author: 'Dan Brown'
  }, {
    id: 3,
    title: 'Angels & Demons',
    author: 'Dan Brown'
  }];

  const [selectedItem, setSelectedItem] = useState(null);

  return (
    <div>
      <p>Selected Book: {selectedItem ? `${selectedItem.title} by ${selectedItem.author}` : 'None'}</p>
      <FuzzySearch
        list={list}
        keys={['author', 'title']}
        width={430}
        onSelect={(newSelectedItem) => {
          setSelectedItem(newSelectedItem);
        }}
        placeholder="Search for books or authors..."
      />
      <div style={{ marginTop: '20px', borderTop: '1px solid #eee', paddingTop: '10px' }}>
        <h3>Custom Result Template:</h3>
        <FuzzySearch
          list={list}
          keys={['author', 'title']}
          width={430}
          onSelect={(newSelectedItem) => {
            setSelectedItem(newSelectedItem);
          }}
          resultsTemplate={(props, state, styles, clickHandler) => {
            return state.results.map((val, i) => {
              const style = state.selectedIndex === i ? styles.selectedResultStyle : styles.resultsStyle;
              return (
                <div
                  key={i}
                  style={{ ...style, padding: '8px', borderBottom: '1px solid #f0f0f0' }}
                  onClick={() => clickHandler(i)}
                >
                  <strong>{val.title}</strong>
                  <span style={{ float: 'right', opacity: 0.7, fontSize: '0.9em' }}>by {val.author}</span>
                </div>
              );
            });
          }}
        />
      </div>
    </div>
  );
}

export default BookSearchComponent;

view raw JSON →