forked from davidsonsns/react-native-highlight-words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (35 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React from 'react';
import {Text} from 'react-native';
import {findAll} from 'highlight-words-core';
/**
* 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>
);
}