react-fuzzy-highlighter
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

react-fuzzy-highlighter

NPM Build

Lightweight fuzzy search highlighting component using fuse.js.

This component wraps fuse.js and provides matched and unmatched text in an iterable format.

Demo · Changelog

Install

yarn add react-fuzzy-highlighter

Usage

import * as React from "react";
import FuzzyHighlighter, { Highlighter } from "react-fuzzy-highlighter";

export default class extends React.Component {
  render() {
    return (
      <FuzzyHighlighter
        query="old"
        data={[
          { title: "Old Man's War" },
          { title: "The Lock Artist" },
          { title: "HTML5" },
        ]}
        options={{
          shouldSort: true,
          includeMatches: true,
          threshold: 0.6,
          location: 0,
          distance: 100,
          maxPatternLength: 32,
          minMatchCharLength: 1,
          keys: ["title"],
        }}
      >
        {({ results, formattedResults, timing }) => {
          return (
            <ul>
              {formattedResults.map((formattedResult, resultIndex) => {
                if (formattedResult.formatted.title === undefined) {
                  return null;
                }

                return (
                  <li key={resultIndex}>
                    <Highlighter text={formattedResult.formatted.title} />
                  </li>
                );
              })}
            </ul>
          );
        }}
      </FuzzyHighlighter>
    );
  }
}

Example

The examples folder contains the source code used for the demo.

License

MIT

Package Sidebar

Install

npm i react-fuzzy-highlighter

Weekly Downloads

261

Version

1.1.1

License

MIT

Unpacked Size

17.5 kB

Total Files

14

Last publish

Collaborators

  • metonym