forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchItem.js
114 lines (105 loc) · 3.11 KB
/
SearchItem.js
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
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';
import { Button, TextHighlight, Tooltip } from '@wordpress/components';
import { getTextContent, create } from '@wordpress/rich-text';
const ButtonStyled = styled(Button)`
&: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);
.block-editor-link-control__search-item-type {
color: black;
}
}
.block-editor-link-control__search-item-type {
background-color: rgba(0, 0, 0, 0.05);
}
`;
/**
* SearchItem
*
* @param {object} props react props
* @param {object} props.suggestion suggestion object
* @param {Array} props.contentTypes array of content types
* @param {Function} props.onClick callback for when the item is clicked
* @param {string} props.searchTerm the search term
* @param {boolean} props.isSelected whether the item is selected
* @param {string} props.id the id of the item
* @param {Function} props.renderType a callback to override the type text
* @returns {*} React JSX
*/
const SearchItem = ({
suggestion,
onClick,
searchTerm,
isSelected,
id,
contentTypes,
renderType,
}) => {
const showType = suggestion.type && contentTypes.length > 1;
const richTextContent = create({ html: suggestion.title });
const textContent = getTextContent(richTextContent);
const titleContent = decodeEntities(textContent);
return (
<Tooltip text={decodeEntities(suggestion.title)}>
<ButtonStyled
id={id}
onClick={onClick}
className={`block-editor-link-control__search-item is-entity ${
isSelected && 'is-selected'
}`}
style={{
borderRadius: '0',
boxSizing: 'border-box',
}}
>
<span className="block-editor-link-control__search-item-header">
<span
className="block-editor-link-control__search-item-title"
style={{
paddingRight: !showType ? 0 : null,
}}
>
<TextHighlight text={titleContent} highlight={searchTerm} />
</span>
<span
aria-hidden
className="block-editor-link-control__search-item-info"
style={{
paddingRight: !showType ? 0 : null,
}}
>
{filterURLForDisplay(safeDecodeURI(suggestion.url)) || ''}
</span>
</span>
{showType && (
<span className="block-editor-link-control__search-item-type">
{renderType(suggestion)}
</span>
)}
</ButtonStyled>
</Tooltip>
);
};
SearchItem.defaultProps = {
id: '',
searchTerm: '',
isSelected: false,
renderType: (suggestion) => {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label.
return suggestion.type === 'post_tag' ? 'tag' : suggestion.type;
},
};
SearchItem.propTypes = {
id: PropTypes.string,
searchTerm: PropTypes.string,
suggestion: PropTypes.object.isRequired,
onClick: PropTypes.func.isRequired,
isSelected: PropTypes.bool,
contentTypes: PropTypes.array.isRequired,
renderType: PropTypes.func,
};
export default SearchItem;