-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Clauderic Demers
committed
Sep 22, 2016
0 parents
commit 5abda39
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016, Claudéric Demers | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# React Native Highlight Words | ||
React Native component used to highlight words within a larger body of text. This is a port port of [react-highlight-words](https://github.com/bvaughn/react-highlight-words), by Brian Vaughn. | ||
|
||
## Installation | ||
Using [npm](https://www.npmjs.com/package/react-native-highlight-words): | ||
``` | ||
npm i --save react-native-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'; | ||
|
||
<Highlighter | ||
highlightStyle={{backgroundColor: 'yellow'}} | ||
searchWords={['and', 'or', 'the']} | ||
textToHighlight='The dog is chasing the cat. Or perhaps they're just playing?' | ||
/> | ||
``` | ||
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"> | ||
## 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 | | ||
## License | ||
MIT License - fork, modify and use however you want. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, {PropTypes} from 'react'; | ||
import {Text} from 'react-native'; | ||
import {findAll} from 'highlight-words-core'; | ||
|
||
Highlighter.propTypes = { | ||
autoEscape: PropTypes.bool, | ||
highlightStyle: Text.propTypes.style, | ||
searchWords: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
textToHighlight: PropTypes.string.isRequired, | ||
sanitize: PropTypes.func, | ||
style: Text.propTypes.style | ||
}; | ||
|
||
/** | ||
* Highlights all occurrences of search terms (searchText) within a string (textToHighlight). | ||
* This function returns an array of strings and <Text> elements (wrapping highlighted words). | ||
*/ | ||
export default function Highlighter({ | ||
autoEscape, | ||
highlightStyle, | ||
searchWords, | ||
textToHighlight, | ||
sanitize, | ||
style, | ||
...props | ||
}) { | ||
const chunks = findAll({textToHighlight, searchWords, sanitize, autoEscape}); | ||
|
||
return ( | ||
<Text style={style} {...props}> | ||
{chunks.map((chunk, index) => { | ||
const text = textToHighlight.substr(chunk.start, chunk.end - chunk.start); | ||
|
||
return (!chunk.highlight) | ||
? text | ||
: ( | ||
<Text | ||
key={index} | ||
style={chunk.highlight && highlightStyle} | ||
> | ||
{text} | ||
</Text> | ||
); | ||
})} | ||
</Text> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "react-native-highlight-words", | ||
"version": "1.0.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/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" | ||
} | ||
} |