react-code-dataset / react-instantsearch /packages /react-instantsearch-hooks-web /src /widgets /Highlight.tsx
| import { | |
| getHighlightedParts, | |
| getPropertyByPath, | |
| unescape, | |
| } from 'instantsearch.js/es/lib/utils'; | |
| import React from 'react'; | |
| import { Highlight as HighlightUiComponent } from '../ui/Highlight'; | |
| import type { PartialKeys } from '../types'; | |
| import type { HighlightProps as HighlightUiComponentProps } from '../ui/Highlight'; | |
| import type { BaseHit, Hit } from 'instantsearch.js'; | |
| export type HighlightProps<THit extends Hit<BaseHit>> = { | |
| hit: THit; | |
| attribute: keyof THit | string[]; | |
| } & PartialKeys< | |
| Omit<HighlightUiComponentProps, 'parts'>, | |
| 'highlightedTagName' | 'nonHighlightedTagName' | 'separator' | |
| >; | |
| export function Highlight<THit extends Hit<BaseHit>>({ | |
| hit, | |
| attribute, | |
| highlightedTagName, | |
| nonHighlightedTagName, | |
| separator, | |
| ...props | |
| }: HighlightProps<THit>) { | |
| const property = | |
| getPropertyByPath(hit._highlightResult, attribute as string) || []; | |
| const properties = Array.isArray(property) ? property : [property]; | |
| const parts = properties.map((singleValue) => | |
| getHighlightedParts(unescape(singleValue.value || '')) | |
| ); | |
| return ( | |
| <HighlightUiComponent | |
| {...props} | |
| parts={parts} | |
| highlightedTagName={highlightedTagName} | |
| nonHighlightedTagName={nonHighlightedTagName} | |
| separator={separator} | |
| /> | |
| ); | |
| } | |