forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (190 loc) · 5.41 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { select } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { v4 as uuidv4 } from 'uuid';
import { ContentSearch } from '../content-search';
import SortableList from './SortableList';
const NAMESPACE = 'tenup-content-picker';
/**
* Unfortunately, we had to use !important because on PickedItem we couldn't @emotion/styled css
* as it was breaking sortability from react-sortable-hoc
*/
const StyleWrapper = styled('div')`
& .block-editor-link-control__search-item {
cursor: default;
&:hover {
background: transparent;
}
}
`;
/**
* Without this, the flex parents will limit the width of the picker. Fixes view when the results
* all have short titles.
*/
const ContentPickerWrapper = styled('div')`
width: 100%;
`;
/**
* Content Picker
*
* @param {object} props React props
* @param {string} props.label label for the picker
* @param {string} props.mode mode of the picker
* @param {Array} props.contentTypes array of content types to filter by
* @param {string} props.placeholder placeholder text for the search input
* @param {Function} props.onPickChange callback for when the picker changes
* @param {Function} props.queryFilter callback that allows to modify the query
* @param {number} props.maxContentItems max number of items to show in the picker
* @param {boolean} props.isOrderable whether or not the picker is sortable
* @param {string} props.singlePickedLabel label for the single picked item
* @param {string} props.multiPickedLabel label for the multi picked item
* @param {Array} props.content items to show in the picker
* @param {boolean} props.uniqueContentItems whether or not the picker should only show unique items
* @param {boolean} props.excludeCurrentPost whether or not to exclude the current post from the picker
* @param {number} props.perPage number of items to show per page
* @returns {*} React JSX
*/
const ContentPicker = ({
label,
mode,
contentTypes,
placeholder,
onPickChange,
queryFilter,
maxContentItems,
isOrderable,
singlePickedLabel,
multiPickedLabel,
content,
uniqueContentItems,
excludeCurrentPost,
perPage,
}) => {
const currentPostId = select('core/editor')?.getCurrentPostId();
/**
* This legacy code allows you to pass in only IDs to content like [ 1, 4, 5 ].
* This really shouldn't be done as of version 1.5.0.
*/
if (content.length && typeof content[0] !== 'object') {
for (let i = 0; i < content.length; i++) {
content[i] = {
id: content[i],
type: contentTypes[0],
};
}
}
const handleSelect = (item) => {
const newItems = [
{
id: item.id,
uuid: uuidv4(),
type: 'subtype' in item ? item.subtype : item.type,
},
...content,
];
onPickChange(newItems);
};
const onDeleteItem = (deletedItem) => {
const newItems = content.filter(({ id, uuid }) => {
if (deletedItem.uuid) {
return uuid !== deletedItem.uuid;
}
return id !== deletedItem.id;
});
onPickChange(newItems);
};
const excludeItems = useMemo(() => {
const items = uniqueContentItems ? [...content] : [];
if (excludeCurrentPost && currentPostId) {
items.push({
id: currentPostId,
});
}
return items;
}, [content, currentPostId, excludeCurrentPost, uniqueContentItems]);
return (
<ContentPickerWrapper className={NAMESPACE}>
{!content.length || (content.length && content.length < maxContentItems) ? (
<ContentSearch
placeholder={placeholder}
label={label}
excludeItems={excludeItems}
onSelectItem={handleSelect}
contentTypes={contentTypes}
mode={mode}
queryFilter={queryFilter}
perPage={perPage}
/>
) : (
label && (
<div
style={{
marginBottom: '8px',
}}
>
{label}
</div>
)
)}
{Boolean(content?.length) && (
<StyleWrapper>
<span
style={{
marginTop: '15px',
marginBottom: '2px',
display: 'block',
}}
>
{content.length > 1 ? multiPickedLabel : singlePickedLabel}
</span>
<ul className="block-editor-link-control__search-items">
<SortableList
posts={content}
handleItemDelete={onDeleteItem}
isOrderable={isOrderable}
mode={mode}
setPosts={onPickChange}
/>
</ul>
</StyleWrapper>
)}
</ContentPickerWrapper>
);
};
ContentPicker.defaultProps = {
label: '',
mode: 'post',
onPickChange: (ids) => {
console.log('Content picker list change', ids); // eslint-disable-line no-console
},
queryFilter: undefined,
contentTypes: ['post', 'page'],
placeholder: '',
content: [],
perPage: 20,
maxContentItems: 1,
uniqueContentItems: true,
isOrderable: false,
excludeCurrentPost: true,
multiPickedLabel: __('You have selected the following items:', '10up-block-components'),
singlePickedLabel: __('You have selected the following item:', '10up-block-components'),
};
ContentPicker.propTypes = {
contentTypes: PropTypes.array,
content: PropTypes.array,
placeholder: PropTypes.string,
mode: PropTypes.oneOf(['post', 'user', 'term']),
label: PropTypes.string,
multiPickedLabel: PropTypes.string,
singlePickedLabel: PropTypes.string,
isOrderable: PropTypes.bool,
onPickChange: PropTypes.func,
queryFilter: PropTypes.func,
uniqueContentItems: PropTypes.bool,
excludeCurrentPost: PropTypes.bool,
maxContentItems: PropTypes.number,
perPage: PropTypes.number,
};
export { ContentPicker };