-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathResult.tsx
99 lines (91 loc) · 2.5 KB
/
Result.tsx
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React from "react";
import {
appendClassName,
getUrlSanitizer,
formatResult,
getEscapedField,
getRaw
} from "./view-helpers";
import type { SearchContextState, SearchResult } from "@elastic/search-ui";
import { BaseContainerProps } from "./types";
export type ResultContainerContext = Pick<
SearchContextState,
"trackClickThrough"
>;
export type ResultContainerProps = BaseContainerProps & {
view?: React.ComponentType<ResultViewProps>;
clickThroughTags?: string[];
titleField?: string;
urlField?: string;
thumbnailField?: string;
result: SearchResult;
shouldTrackClickThrough?: boolean;
};
export type ResultViewProps = BaseContainerProps &
Pick<
ResultContainerProps,
"result" | "titleField" | "urlField" | "thumbnailField"
> & {
key?: string;
onClickLink: () => void;
};
function Result({
className,
result,
onClickLink,
titleField,
urlField,
thumbnailField,
...rest
}: ResultViewProps) {
const fields = formatResult(result);
const title = getEscapedField(result[titleField]);
const url = getUrlSanitizer(URL, location.href)(getRaw(result[urlField]));
const thumbnail = getUrlSanitizer(
URL,
location.href
)(getRaw(result[thumbnailField]));
return (
<li className={appendClassName("sui-result", className)} {...rest}>
<div className="sui-result__header">
{title && !url && (
<span
className="sui-result__title"
dangerouslySetInnerHTML={{ __html: title }}
/>
)}
{title && url && (
<a
className="sui-result__title sui-result__title-link"
dangerouslySetInnerHTML={{ __html: title }}
href={url}
onClick={onClickLink}
target="_blank"
rel="noopener noreferrer"
/>
)}
</div>
<div className="sui-result__body">
{thumbnail && (
<div className="sui-result__image">
<img src={thumbnail} alt="" />
</div>
)}
<ul className="sui-result__details">
{Object.entries(fields).map(
([fieldName, fieldValue]: [string, string]) => (
<li key={fieldName}>
<span className="sui-result__key">{fieldName}</span>{" "}
<span
className="sui-result__value"
dangerouslySetInnerHTML={{ __html: fieldValue }}
/>
</li>
)
)}
</ul>
</div>
</li>
);
}
export default Result;