Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
sample.gif
53 changes: 34 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
# React Native Highlight Words
# RN Highlight Words

React Native component used to highlight words within a larger body of text. This is a port of [react-highlight-words](https://github.com/bvaughn/react-highlight-words).

Check out a [demo](https://getexponent.com/@clauderic/react-native-highlight-words) using Exponent.
## Changelog

- [x] OnPress event added for Normal/Highlighted text

## Installation
Using [npm](https://www.npmjs.com/package/react-native-highlight-words):

Using [npm](https://www.npmjs.com/package/rn-highlight-words):

```
npm i --save react-native-highlight-words
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:

```jsx
import Highlighter from 'react-native-highlight-words';
import Highlighter from "rn-highlight-words";

<Highlighter
highlightStyle={{backgroundColor: 'yellow'}}
searchWords={['and', 'or', 'the']}
textToHighlight='The dog is chasing the cat. Or perhaps they're just playing?'
/>
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:

<img width="368" alt="screen shot 2015-12-19 at 8 23 43 am" src="https://cloud.githubusercontent.com/assets/29597/11914033/e3c319f6-a629-11e5-896d-1a5ce22c9ea2.png">
<img src="https://github.com/adityasonel/rn-highlight-words/blob/master/sample.gif">

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

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

## Props

| Property | Type | Required? | Description |
|:----------------|:--------------|:---------:|:------------------------------------------------------------------------------------------------------------------------|
| autoEscape | Boolean | | Escape characters which are meaningful in regular expressions |
| highlightStyle | Object | | Styles applied to highlighted text |
| sanitize | Function | | Process each search word and text to highlight before comparing (eg remove accents); signature `(text: string): string` |
| searchWords | Array<String> | ✓ | Array of search words |
| style | Object | | Styles applied to the text wrapper |
| textToHighlight | String | ✓ | Text to highlight matches in |
| Property | Type | Required? | Description |
| :--------------------- | :------------ | :-------: | :---------------------------------------------------------------------------------------------------------------------- |
| autoEscape | Boolean | | Escape characters which are meaningful in regular expressions |
| highlightStyle | Object | | Styles applied to highlighted text |
| sanitize | Function | | Process each search word and text to highlight before comparing (eg remove accents); signature `(text: string): string` |
| searchWords | Array<String> | ✓ | Array of search words |
| style | Object | | Styles applied to the text wrapper |
| textToHighlight | String | ✓ | Text to highlight matches in |
| onPressNormalText | Function | | onPress event for normal text |
| onPressHighlightedText | Function | | onPress event for highlighted text (returns text that clicked) |

## License
MIT License - fork, modify and use however you want.

[MIT License](LICENSE)
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Highlighter.propTypes = {
searchWords: PropTypes.arrayOf(PropTypes.string).isRequired,
textToHighlight: PropTypes.string.isRequired,
sanitize: PropTypes.func,
onPressNormalText = PropTypes.func,
onPressHighlightedText = PropTypes.func,
style: Text.propTypes.style
};

Expand All @@ -22,20 +24,22 @@ export default function Highlighter({
searchWords,
textToHighlight,
sanitize,
onPressNormalText,
onPressHighlightedText,
style,
...props
}) {
const chunks = findAll({textToHighlight, searchWords, sanitize, autoEscape});

return (
<Text style={style} {...props}>
<Text style={style} {...props} onPress = {onPressNormalText} >
{chunks.map((chunk, index) => {
const text = textToHighlight.substr(chunk.start, chunk.end - chunk.start);

return (!chunk.highlight)
? text
: (
<Text
onPress = {() => onPressHighlightedText(text)}
key={index}
style={chunk.highlight && highlightStyle}
>
Expand Down
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 38 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "react-native-highlight-words",
"version": "1.0.1",
"description": "A React Native port of react-highlight-words. This component is used to highlight words within a larger body of text.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/clauderic/react-native-highlight-words.git"
},
"keywords": [
"react-native",
"react",
"reactjs",
"react-component",
"highlighter",
"highlight",
"text",
"words",
"matches",
"substring",
"occurrences",
"search"
],
"author": "Clauderic Demers",
"license": "MIT",
"bugs": {
"url": "https://github.com/clauderic/react-native-highlight-words/issues"
},
"homepage": "https://github.com/clauderic/react-native-highlight-words",
"dependencies": {
"highlight-words-core": "^1.0.3",
"prop-types": "^15.5.7"
},
"peerDependencies": {
"react": "^15.5.0 || ^16"
}
"name": "rn-highlight-words",
"version": "1.1.0",
"description": "A React Native port of react-highlight-words. This component is used to highlight words within a larger body of text.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/adityasonel/rn-highlight-words.git"
},
"keywords": [
"react-native",
"react",
"reactjs",
"react-component",
"highlighter",
"highlight",
"text",
"words",
"matches",
"substring",
"occurrences",
"search"
],
"author": "Aditya Sonel",
"license": "MIT",
"bugs": {
"url": "https://github.com/adityasonel/rn-highlight-words/issues"
},
"homepage": "https://github.com/adityasonel/rn-highlight-words",
"dependencies": {
"highlight-words-core": "^1.0.3",
"prop-types": "^15.5.7"
},
"peerDependencies": {
"react": "^15.5.0 || ^16"
}
}
Binary file added sample.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.