1
0
Fork 0
docsearch/src/Snippet.tsx
2020-04-03 17:55:22 +02:00

32 lines
736 B
TypeScript

import { createElement } from 'react';
import { StoredDocSearchHit } from './types';
function getPropertyByPath(object: object, path: string): any {
const parts = path.split('.');
return parts.reduce((current, key) => current && current[key], object);
}
interface SnippetProps<TItem> {
[prop: string]: unknown;
hit: TItem;
attribute: string;
tagName?: string;
}
export function Snippet<TItem extends StoredDocSearchHit>({
hit,
attribute,
tagName = 'span',
...rest
}: SnippetProps<TItem>) {
return createElement(tagName, {
...rest,
dangerouslySetInnerHTML: {
__html:
getPropertyByPath(hit, `_snippetResult.${attribute}.value`) ||
getPropertyByPath(hit, attribute),
},
});
}