Skip to content

Commit 7e59686

Browse files
committed
Merge branch 'develop' into trunk
2 parents e80740e + 5213452 commit 7e59686

File tree

9 files changed

+46469
-1855
lines changed

9 files changed

+46469
-1855
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": ["@10up/eslint-config/wordpress"]
2+
"extends": ["@10up/eslint-config/react"],
3+
"rules": {
4+
"import/no-unresolved": 0
5+
}
36
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.DS_Store
12
node_modules
23
.vscode

components/ContentPicker/PickedItem.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { decodeEntities } from '@wordpress/html-entities';
55
import { __ } from '@wordpress/i18n';
66
import { useSelect } from '@wordpress/data';
77
import { sortableHandle } from 'react-sortable-hoc';
8+
import { useEffect } from '@wordpress/element';
89

910
/**
1011
* PickedItem
@@ -44,12 +45,17 @@ const Wrapper = styled('div')`
4445
}
4546
`;
4647

47-
const PickedItem = ({ item, isOrderable, handleItemDelete, sortIndex, mode }) => {
48+
const PickedItem = ({ item, isOrderable, handleItemDelete, mode }) => {
4849
const type = mode === 'post' ? 'postType' : 'taxonomy';
4950

51+
// This will return undefined while the item data is being fetched. If the item comes back
52+
// empty, it will return null, which is handled in the effect below.
5053
const preparedItem = useSelect(
5154
(select) => {
52-
const result = select('core').getEntityRecord(type, item.type, item.id);
55+
const { getEntityRecord, hasFinishedResolution } = select('core');
56+
57+
const getEntityRecordParameters = [type, item.type, item.id];
58+
const result = getEntityRecord(...getEntityRecordParameters);
5359

5460
if (result) {
5561
return {
@@ -59,11 +65,22 @@ const PickedItem = ({ item, isOrderable, handleItemDelete, sortIndex, mode }) =>
5965
};
6066
}
6167

62-
return null;
68+
if (hasFinishedResolution('getEntityRecord', getEntityRecordParameters)) {
69+
return null;
70+
}
71+
72+
return undefined;
6373
},
6474
[item.id, type],
6575
);
6676

77+
// If `getEntityRecord` did not return an item, pass it to the delete callback.
78+
useEffect(() => {
79+
if (preparedItem === null) {
80+
handleItemDelete(item);
81+
}
82+
}, [item, handleItemDelete, preparedItem]);
83+
6784
return preparedItem ? (
6885
<Wrapper
6986
style={{
@@ -87,7 +104,7 @@ const PickedItem = ({ item, isOrderable, handleItemDelete, sortIndex, mode }) =>
87104
<button
88105
type="button"
89106
onClick={() => {
90-
handleItemDelete(preparedItem, sortIndex);
107+
handleItemDelete(preparedItem);
91108
}}
92109
aria-label={__('Delete item', '10up-block-components')}
93110
>
@@ -107,7 +124,6 @@ PickedItem.propTypes = {
107124
item: PropTypes.object.isRequired,
108125
isOrderable: PropTypes.bool,
109126
handleItemDelete: PropTypes.func.isRequired,
110-
sortIndex: PropTypes.number.isRequired,
111127
mode: PropTypes.string.isRequired,
112128
};
113129

components/ContentPicker/index.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@ import PropTypes from 'prop-types';
22
import arrayMove from 'array-move';
33
import styled from '@emotion/styled';
44
import { select } from '@wordpress/data';
5-
import { useState } from '@wordpress/element'; // eslint-disable-line
5+
import { useState, useEffect, useMemo } from '@wordpress/element';
66
import { __ } from '@wordpress/i18n';
77
import { ContentSearch } from '../ContentSearch';
88
import SortableList from './SortableList';
99

1010
const NAMESPACE = '10up-content-picker';
1111

12+
/**
13+
* Unfortunately, we had to use !important because on PickedItem we couldn't @emotion/styled css
14+
* as it was breaking sortability from react-sortable-hoc
15+
*/
16+
const StyleWrapper = styled('div')`
17+
& .block-editor-link-control__search-item {
18+
border: none !important;
19+
cursor: default;
20+
21+
&:hover {
22+
background: transparent;
23+
}
24+
}
25+
`;
26+
1227
/**
1328
* Content Picker
1429
*
15-
* @param {Object} props React props
30+
* @param {object} props React props
1631
* @param props.label
1732
* @param props.mode
1833
* @param props.contentTypes
@@ -25,7 +40,7 @@ const NAMESPACE = '10up-content-picker';
2540
* @param props.content
2641
* @param props.uniqueContentItems
2742
* @param props.excludeCurrentPost
28-
* @return {*} React JSX
43+
* @returns {*} React JSX
2944
*/
3045
const ContentPicker = ({
3146
label,
@@ -54,55 +69,40 @@ const ContentPicker = ({
5469
}
5570
}
5671

57-
function handleSelect(item) {
58-
const newContent = [...content];
59-
60-
newContent.unshift({
61-
id: item.id,
62-
type: 'subtype' in item ? item.subtype : item.type,
63-
});
64-
65-
onPickChange(newContent);
66-
67-
setContent(newContent);
68-
}
69-
70-
function handleItemDelete(item, sortIndex) {
71-
const newContent = [...content];
72-
73-
newContent.splice(sortIndex, 1);
74-
75-
onPickChange(newContent);
76-
77-
setContent(newContent);
78-
}
79-
80-
/**
81-
* Unfortunately, we had to use !important because on PickedItem we couldn't @emotion/styled css
82-
* as it was breaking sortability from react-sortable-hoc
83-
*/
84-
const StyleWrapper = styled('div')`
85-
& .block-editor-link-control__search-item {
86-
border: none !important;
87-
cursor: default;
88-
89-
&:hover {
90-
background: transparent;
91-
}
72+
// Run onPickChange callback when content changes.
73+
useEffect(() => {
74+
onPickChange(content);
75+
}, [content, onPickChange]);
76+
77+
const handleSelect = (item) => {
78+
setContent((previousContent) => [
79+
{
80+
id: item.id,
81+
type: 'subtype' in item ? item.subtype : item.type,
82+
},
83+
...previousContent,
84+
]);
85+
};
86+
87+
const onDeleteItem = (deletedItem) => {
88+
setContent((previousContent) => previousContent.filter(({ id }) => id !== deletedItem.id));
89+
};
90+
91+
const excludeItems = useMemo(() => {
92+
const items = uniqueContentItems ? [...content] : [];
93+
94+
if (excludeCurrentPost && currentPostId) {
95+
items.push({
96+
id: currentPostId,
97+
});
9298
}
93-
`;
94-
95-
const excludeItems = uniqueContentItems ? [...content] : [];
9699

97-
if (excludeCurrentPost && currentPostId) {
98-
excludeItems.push({
99-
id: currentPostId,
100-
});
101-
}
100+
return items;
101+
}, [content, currentPostId, excludeCurrentPost, uniqueContentItems]);
102102

103103
return (
104104
<div className={`${NAMESPACE}`}>
105-
{!content.length || (content.length && content.length < maxContentItems) ? (
105+
{(!content.length || (content.length && content.length < maxContentItems)) && (
106106
<ContentSearch
107107
placeholder={placeholder}
108108
label={label}
@@ -111,8 +111,8 @@ const ContentPicker = ({
111111
contentTypes={contentTypes}
112112
mode={mode}
113113
/>
114-
) : null}
115-
{content.length ? (
114+
)}
115+
{Boolean(content?.length) > 0 && (
116116
<StyleWrapper>
117117
<span
118118
style={{
@@ -127,19 +127,17 @@ const ContentPicker = ({
127127
<SortableList
128128
items={content}
129129
useDragHandle
130+
handleItemDelete={onDeleteItem}
130131
isOrderable={isOrderable}
131132
mode={mode}
132-
handleItemDelete={handleItemDelete}
133133
onSortEnd={({ oldIndex, newIndex }) => {
134134
const newContent = [...arrayMove(content, oldIndex, newIndex)];
135135

136-
onPickChange(newContent);
137-
138136
setContent(newContent);
139137
}}
140138
/>
141139
</StyleWrapper>
142-
) : null}
140+
)}
143141
</div>
144142
);
145143
};

components/ContentSearch/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
5757

5858
const searchQuery = `wp/v2/search/?search=${keyword}&subtype=${contentTypes.join(
5959
',',
60-
)}&type=${mode}`;
60+
)}&type=${mode}&_embed`;
6161

6262
if (searchCache[searchQuery]) {
6363
setSearchResults(searchCache[searchQuery]);

example/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "@wordpress/eslint-plugin"
2+
"extends": ["@10up/eslint-config/react"]
33
}

0 commit comments

Comments
 (0)