1.1.0 • Published 5 years ago

rn-highlight-words v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

RN Highlight Words

React Native component used to highlight words within a larger body of text. This is a port of react-highlight-words.

Changelog

  • OnPress event added for Normal/Highlighted text

Installation

Using npm:

npm i --save rn-highlight-words

Usage

To use it, just provide it with an array of search terms and a body of text to highlight:

import Highlighter from "rn-highlight-words";

<Highlighter
    highlightStyle={{ backgroundColor: "yellow" }}
    searchWords={["and", "or", "the"]}
    textToHighlight="The dog is chasing the cat. Or perhaps they are just playing?"
    onPressNormalText={() => console.log("normal text is clickeddd!")}
    onPressHighlightedText={() => console.log("highlighted text is clickeddd!")}
/>;

And the Highlighter component will highlight all occurrences of search terms within the text:

And if you want to get highlighted text that clicked, get in onPressHighlightedText as

onPressHighlightedText = {text => console.log("highlighted text that click: " + text)}

Props

PropertyTypeRequired?Description
autoEscapeBooleanEscape characters which are meaningful in regular expressions
highlightStyleObjectStyles applied to highlighted text
sanitizeFunctionProcess each search word and text to highlight before comparing (eg remove accents); signature (text: string): string
searchWordsArrayArray of search words
styleObjectStyles applied to the text wrapper
textToHighlightStringText to highlight matches in
onPressNormalTextFunctiononPress event for normal text
onPressHighlightedTextFunctiononPress event for highlighted text (returns text that clicked)

License

MIT License