1
0
Fork 0
docsearch/packages/docsearch-react/src/Snippet.tsx
renovate[bot] 10c5b2ea14
chore(deps): update lint dependency (#1120)
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
2021-11-02 12:06:22 +01:00

35 lines
801 B
TypeScript

import { createElement } from 'react';
import type { StoredDocSearchHit } from './types';
function getPropertyByPath(object: Record<string, any>, path: string): any {
const parts = path.split('.');
return parts.reduce((prev, current) => {
if (prev?.[current]) return prev[current];
return null;
}, object);
}
interface SnippetProps<TItem> {
hit: TItem;
attribute: string;
tagName?: string;
[prop: string]: unknown;
}
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),
},
});
}