File tree Expand file tree Collapse file tree 2 files changed +43
-7
lines changed
examples/react-renderer/src Expand file tree Collapse file tree 2 files changed +43
-7
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import { Hit } from '@algolia/client-search';
8
8
import algoliasearch from 'algoliasearch/lite' ;
9
9
import React from 'react' ;
10
10
11
+ import { Highlight } from './Highlight' ;
11
12
import { ClearIcon } from './ClearIcon' ;
12
13
import { SearchIcon } from './SearchIcon' ;
13
14
@@ -63,8 +64,6 @@ export function Autocomplete(
63
64
query,
64
65
params : {
65
66
hitsPerPage : 5 ,
66
- highlightPreTag : '<mark>' ,
67
- highlightPostTag : '</mark>' ,
68
67
} ,
69
68
} ,
70
69
] ,
@@ -173,11 +172,9 @@ export function Autocomplete(
173
172
< div className = "aa-ItemContentBody" >
174
173
< div
175
174
className = "aa-ItemContentTitle"
176
- dangerouslySetInnerHTML = { {
177
- __html : item . _highlightResult ! . name !
178
- . value ,
179
- } }
180
- />
175
+ >
176
+ < Highlight hit = { item } attribute = "name" />
177
+ </ div >
181
178
< div className = "aa-ItemContentDescription" >
182
179
By < strong > { item . brand } </ strong > in{ ' ' }
183
180
< strong > { item . categories [ 0 ] } </ strong >
Original file line number Diff line number Diff line change
1
+ import { createElement , Fragment } from "react" ;
2
+ import { parseAlgoliaHitHighlight } from "@algolia/autocomplete-preset-algolia" ;
3
+
4
+ type HighlightHitParams < THit > = {
5
+ /**
6
+ * The Algolia hit whose attribute to retrieve the highlighted parts from.
7
+ */
8
+ hit : THit ;
9
+ /**
10
+ * The attribute to retrieve the highlighted parts from.
11
+ *
12
+ * You can use the array syntax to reference nested attributes.
13
+ */
14
+ attribute : keyof THit | string [ ] ;
15
+ /**
16
+ * The tag name to use for highlighted parts.
17
+ *
18
+ * @default "mark"
19
+ */
20
+ tagName ?: string ;
21
+ } ;
22
+
23
+ export function Highlight < THit > ( {
24
+ hit,
25
+ attribute,
26
+ tagName = "mark" ,
27
+ } : HighlightHitParams < THit > ) : JSX . Element {
28
+ return createElement (
29
+ Fragment ,
30
+ { } ,
31
+ parseAlgoliaHitHighlight < THit > ( { hit, attribute } ) . map (
32
+ ( { value, isHighlighted } , index ) => {
33
+ if ( isHighlighted ) return createElement ( tagName , { key : index } , value ) ;
34
+
35
+ return value ;
36
+ }
37
+ )
38
+ ) ;
39
+ }
You can’t perform that action at this time.
0 commit comments