forked from elastic/search-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.js
More file actions
109 lines (97 loc) · 2.98 KB
/
Copy pathResult.js
File metadata and controls
109 lines (97 loc) · 2.98 KB
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
100
101
102
103
104
105
106
107
108
109
import PropTypes from "prop-types";
import React from "react";
import { appendClassName } from "./view-helpers";
import { isFieldValueWrapper } from "./types/FieldValueWrapper";
function getFieldType(result, field, type) {
if (result[field]) return result[field][type];
}
function getRaw(result, field) {
return getFieldType(result, field, "raw");
}
function getSnippet(result, field) {
return getFieldType(result, field, "snippet");
}
function htmlEscape(str) {
if (!str) return "";
return String(str)
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/</g, "<")
.replace(/>/g, ">");
}
function getEscapedField(result, field) {
// Fallback to raw values here, because non-string fields
// will not have a snippet fallback. Raw values MUST be html escaped.
const safeField =
getSnippet(result, field) || htmlEscape(getRaw(result, field));
return Array.isArray(safeField) ? safeField.join(", ") : safeField;
}
function getEscapedFields(result) {
return Object.keys(result).reduce((acc, field) => {
// If we receive an arbitrary value from the response, we may not properly
// handle it, so we should filter out arbitrary values here.
//
// I.e.,
// Arbitrary value: "_metaField: '1939191'"
// vs.
// FieldValueWrapper: "_metaField: {raw: '1939191'}"
if (!isFieldValueWrapper(result[field])) return acc;
return { ...acc, [field]: getEscapedField(result, field) };
}, {});
}
function Result({
className,
result,
onClickLink,
titleField,
urlField,
...rest
}) {
const fields = getEscapedFields(result);
const title = getEscapedField(result, titleField);
const url = getRaw(result, urlField);
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">
<ul className="sui-result__details">
{Object.entries(fields).map(([fieldName, fieldValue]) => (
<li key={fieldName}>
<span className="sui-result__key">{fieldName}</span>{" "}
<span
className="sui-result__value"
dangerouslySetInnerHTML={{ __html: fieldValue }}
/>
</li>
))}
</ul>
</div>
</li>
);
}
Result.propTypes = {
result: PropTypes.object.isRequired,
onClickLink: PropTypes.func.isRequired,
className: PropTypes.string,
titleField: PropTypes.string,
urlField: PropTypes.string
};
export default Result;