forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickedItem.js
155 lines (133 loc) · 3.99 KB
/
PickedItem.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { DragHandle } from '../drag-handle';
const StyledCloseButton = styled('button')`
display: block;
padding: 2px 8px 6px 8px;
margin-left: auto;
font-size: 2em;
cursor: pointer;
border: none;
background-color: transparent;
&:hover {
background-color: #ccc;
}
`;
function getType(mode) {
let type;
switch (mode) {
case 'post':
type = 'postType';
break;
case 'user':
type = 'root';
break;
default:
type = 'taxonomy';
break;
}
return type;
}
/**
* PickedItem
*
* @param {object} props react props
* @param {object} props.item item to show in the picker
* @param {boolean} props.isOrderable whether or not the picker is sortable
* @param {Function} props.handleItemDelete callback for when the item is deleted
* @param {string} props.mode mode of the picker
* @param {number|string} props.id id of the item
* @returns {*} React JSX
*/
const PickedItem = ({ item, isOrderable, handleItemDelete, mode, id }) => {
const type = getType(mode);
const { attributes, isDragging, listeners, setNodeRef, transform, transition } = useSortable({
id,
});
// This will return undefined while the item data is being fetched. If the item comes back
// empty, it will return null, which is handled in the effect below.
const preparedItem = useSelect(
(select) => {
const { getEntityRecord, hasFinishedResolution } = select('core');
const getEntityRecordParameters = [type, item.type, item.id];
const result = getEntityRecord(...getEntityRecordParameters);
if (result) {
const newItem = {
title: mode === 'post' ? result.title.rendered : result.name,
url: result.link,
id: result.id,
};
if (item.uuid) {
newItem.uuid = item.uuid;
}
return newItem;
}
if (hasFinishedResolution('getEntityRecord', getEntityRecordParameters)) {
return null;
}
return undefined;
},
[item.id, type],
);
// If `getEntityRecord` did not return an item, pass it to the delete callback.
useEffect(() => {
if (preparedItem === null) {
handleItemDelete(item);
}
}, [item, handleItemDelete, preparedItem]);
const style = {
transform: CSS.Transform.toString(transform),
transition,
border: isDragging ? '2px dashed #ddd' : '2px dashed transparent',
paddingTop: '10px',
paddingBottom: '10px',
display: 'flex',
alignItems: 'center',
paddingLeft: isOrderable ? '3px' : '8px',
};
const normalizedItemType = item?.type ? item.type : 'post';
const className = `block-editor-link-control__search-item is-entity is-type-${normalizedItemType}`;
if (!preparedItem) {
return null;
}
return (
<li className={className} ref={setNodeRef} style={style}>
{isOrderable ? <DragHandle {...attributes} {...listeners} /> : ''}
<span className="block-editor-link-control__search-item-header">
<span className="block-editor-link-control__search-item-title">
{decodeEntities(preparedItem.title)}
</span>
<span aria-hidden className="block-editor-link-control__search-item-info">
{filterURLForDisplay(safeDecodeURI(preparedItem.url)) || ''}
</span>
</span>
<StyledCloseButton
type="button"
onClick={() => {
handleItemDelete(preparedItem);
}}
aria-label={__('Delete item', '10up-block-components')}
>
×
</StyledCloseButton>
</li>
);
};
PickedItem.defaultProps = {
isOrderable: false,
};
PickedItem.propTypes = {
item: PropTypes.object.isRequired,
isOrderable: PropTypes.bool,
handleItemDelete: PropTypes.func.isRequired,
mode: PropTypes.string.isRequired,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
};
export default PickedItem;