-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntitySelectControl.js
More file actions
executable file
·30 lines (24 loc) · 939 Bytes
/
EntitySelectControl.js
File metadata and controls
executable file
·30 lines (24 loc) · 939 Bytes
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
// WordPress dependencies.
const { SelectControl } = wp.components;
const { withSelect } = wp.data;
const EntitySelectControl = ({ entity, items = [], placeholder = '', ...restProps }) => {
const options = [];
if (placeholder) {
options.push({ value: 0, label: placeholder });
}
if (items) {
switch (entity) {
case 'postType':
items.forEach((item) => options.push({ value: item.id, label: item.title.rendered }));
break;
case 'taxonomy':
items.forEach((item) => options.push({ value: item.id, label: item.name }));
break;
}
}
if (!items && !placeholder && restProps.label) {
options.push({ value: 0, label: restProps.label });
}
return <SelectControl { ...restProps } options={ options } disabled={ !items } />;
};
export default withSelect((select, { entity, type, args = {} }) => ({ items: select('core').getEntityRecords(entity, type, args) }))(EntitySelectControl);