import React, { Fragment } from 'react';
import { StyleSheet, Text } from 'react-native';
import { Hit as AlgoliaHit } from '@algolia/client-search';
import {
getHighlightedParts,
getPropertyByPath,
} from 'instantsearch.js/es/lib/utils';
type HighlightPartProps = {
children: React.ReactNode;
isHighlighted: boolean;
};
function HighlightPart({ children, isHighlighted }: HighlightPartProps) {
return (
{children}
);
}
type HighlightProps = {
hit: THit;
attribute: keyof THit | string[];
className?: string;
separator?: string;
};
export function Highlight>>({
hit,
attribute,
separator = ', ',
}: HighlightProps) {
const { value: attributeValue = '' } =
getPropertyByPath(hit._highlightResult, attribute as string) || {};
const parts = getHighlightedParts(attributeValue);
return (
<>
{parts.map((part, partIndex) => {
if (Array.isArray(part)) {
const isLastPart = partIndex === parts.length - 1;
return (
{part.map((subPart, subPartIndex) => (
{subPart.value}
))}
{!isLastPart && separator}
);
}
return (
{part.value}
);
})}
>
);
}
const styles = StyleSheet.create({
highlighted: {
fontWeight: 'bold',
backgroundColor: '#f5df4d',
color: '#6f6106',
},
nonHighlighted: {
fontWeight: 'normal',
backgroundColor: 'transparent',
color: 'black',
},
});