1
1
import React , { Fragment } from 'react' ;
2
- import { StyleSheet , Text } from 'react-native' ;
2
+ import { StyleProp , StyleSheet , Text , TextStyle } from 'react-native' ;
3
3
import { Hit as AlgoliaHit } from '@algolia/client-search' ;
4
4
import {
5
5
getHighlightedParts ,
@@ -9,27 +9,28 @@ import {
9
9
type HighlightPartProps = {
10
10
children : React . ReactNode ;
11
11
isHighlighted : boolean ;
12
+ style : StyleProp < TextStyle > ;
12
13
} ;
13
14
14
- function HighlightPart ( { children, isHighlighted } : HighlightPartProps ) {
15
- return (
16
- < Text style = { isHighlighted ? styles . highlighted : styles . nonHighlighted } >
17
- { children }
18
- </ Text >
19
- ) ;
15
+ function HighlightPart ( { children, style } : HighlightPartProps ) {
16
+ return < Text style = { style } > { children } </ Text > ;
20
17
}
21
18
22
19
type HighlightProps < THit > = {
23
20
hit : THit ;
24
21
attribute : keyof THit | string [ ] ;
25
22
className ?: string ;
26
23
separator ?: string ;
24
+ highlightedStyle ?: StyleProp < TextStyle > ;
25
+ nonHighlightedStyle ?: StyleProp < TextStyle > ;
27
26
} ;
28
27
29
28
export function Highlight < THit extends AlgoliaHit < Record < string , unknown > > > ( {
30
29
hit,
31
30
attribute,
32
31
separator = ', ' ,
32
+ highlightedStyle = styles . highlighted ,
33
+ nonHighlightedStyle = styles . nonHighlighted ,
33
34
} : HighlightProps < THit > ) {
34
35
const { value : attributeValue = '' } =
35
36
getPropertyByPath ( hit . _highlightResult , attribute as string ) || { } ;
@@ -47,6 +48,11 @@ export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
47
48
< HighlightPart
48
49
key = { subPartIndex }
49
50
isHighlighted = { subPart . isHighlighted }
51
+ style = {
52
+ subPart . isHighlighted
53
+ ? highlightedStyle
54
+ : nonHighlightedStyle
55
+ }
50
56
>
51
57
{ subPart . value }
52
58
</ HighlightPart >
@@ -58,7 +64,11 @@ export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
58
64
}
59
65
60
66
return (
61
- < HighlightPart key = { partIndex } isHighlighted = { part . isHighlighted } >
67
+ < HighlightPart
68
+ key = { partIndex }
69
+ isHighlighted = { part . isHighlighted }
70
+ style = { part . isHighlighted ? highlightedStyle : nonHighlightedStyle }
71
+ >
62
72
{ part . value }
63
73
</ HighlightPart >
64
74
) ;
0 commit comments