File size: 1,284 Bytes
1e92f2d | 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 39 40 41 42 43 44 45 46 47 48 | 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}
/>
);
}
|