-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSearchItem.tsx
More file actions
137 lines (121 loc) · 3.44 KB
/
Copy pathSearchItem.tsx
File metadata and controls
137 lines (121 loc) · 3.44 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import styled from '@emotion/styled';
import { safeHTML } from '@wordpress/dom';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';
import {
Button,
TextHighlight,
Tooltip,
__experimentalTruncate as Truncate,
} from '@wordpress/components';
import { getTextContent, create } from '@wordpress/rich-text';
import { RenderItemComponentProps } from './types';
import { NormalizedSuggestion } from './utils';
const SearchItemWrapper = styled(Button)`
&&& {
display: flex;
flex-direction: column;
text-align: left;
width: 100%;
align-items: flex-start;
border-radius: 2px;
box-sizing: border-box;
height: auto !important;
padding: 0.3em 0.7em;
overflow: hidden;
&:hover {
/* Add opacity background to support future color changes */
/* Reduce background from #ddd to 0.05 for text contrast */
background-color: rgba(0, 0, 0, 0.05);
}
}
`;
const SearchItemHeaderWrapper = styled.span`
display: flex;
flex-direction: row;
width: 100%;
justify-content: space-between;
align-items: center;
`;
const SearchItemHeader = styled.span`
display: flex;
flex-direction: column;
align-items: flex-start;
`;
const SearchItemTitle = styled.span<{ showType: boolean }>`
padding-right: ${({ showType }) => (showType ? 0 : undefined)};
`;
const SearchItemURL = styled.span<{ showType: boolean }>`
padding-right: ${({ showType }) => (showType ? 0 : undefined)};
`;
const SearchItemInfo = styled.span`
font-size: 0.75rem;
line-height: 1.4;
color: #757575;
margin-top: 4px;
`;
const SearchItemType = styled.span`
background-color: rgba(0, 0, 0, 0.05);
color: black;
padding: 2px 4px;
text-transform: capitalize;
border-radius: 2px;
flex-shrink: 0;
`;
const StyledTextHighlight = styled(TextHighlight)`
margin: 0 !important;
padding: 0 !important;
`;
export function defaultRenderItemType(suggestion: NormalizedSuggestion): string {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localized CPT or taxonomy label.
if (suggestion.type === 'post_tag') {
return 'tag';
}
if (suggestion.subtype) {
return suggestion.subtype;
}
return suggestion.type;
}
const SearchItem: React.FC<RenderItemComponentProps> = ({
item: suggestion,
onSelect: onClick,
searchTerm = '',
id = '',
contentTypes,
renderType = defaultRenderItemType,
}) => {
const { type, title, url, info } = suggestion;
const showType = !!(type && contentTypes.length > 1);
const richTextContent = create({ html: title });
const textContent = getTextContent(richTextContent);
const titleContent = decodeEntities(textContent);
return (
<Tooltip text={decodeEntities(title)}>
<SearchItemWrapper id={id} onClick={onClick}>
<SearchItemHeaderWrapper>
<SearchItemHeader>
<SearchItemTitle showType={showType}>
<StyledTextHighlight text={titleContent} highlight={searchTerm} />
</SearchItemTitle>
{url && (
<SearchItemURL aria-hidden showType={showType}>
<Truncate numberOfLines={1} limit={55} ellipsizeMode="middle">
{filterURLForDisplay(safeDecodeURI(url)) || ''}
</Truncate>
</SearchItemURL>
)}
</SearchItemHeader>
{showType && <SearchItemType>{renderType(suggestion)}</SearchItemType>}
</SearchItemHeaderWrapper>
{info && (
<SearchItemInfo
dangerouslySetInnerHTML={{
__html: safeHTML(info),
}}
/>
)}
</SearchItemWrapper>
</Tooltip>
);
};
export default SearchItem;