Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/content picker improvements #19

Merged
merged 4 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/ContentPicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { __ } from '@wordpress/i18n';
import { ContentSearch } from '../ContentSearch';
import SortableList from './SortableList';

const NAMESPACE = '10up-block-components';
const NAMESPACE = '10up-content-picker';

/**
* Content Picker
Expand Down
22 changes: 18 additions & 4 deletions components/ContentSearch/SearchItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { Button, TextHighlight } from '@wordpress/components';
* @param props.id
* @return {*} React JSX
*/
const SearchItem = ({ suggestion, onClick, searchTerm, isSelected, id }) => {
const SearchItem = ({ suggestion, onClick, searchTerm, isSelected, id, contentTypes }) => {
const showType = suggestion.type && contentTypes.length > 1;
johnwatkins0 marked this conversation as resolved.
Show resolved Hide resolved

return (
<Button
id={id}
Expand All @@ -27,14 +29,25 @@ const SearchItem = ({ suggestion, onClick, searchTerm, isSelected, id }) => {
}}
>
<span className="block-editor-link-control__search-item-header">
<span className="block-editor-link-control__search-item-title">
<span
className="block-editor-link-control__search-item-title"
style={{
paddingRight: !showType ? 0 : null,
}}
>
<TextHighlight text={decodeEntities(suggestion.title)} highlight={searchTerm} />
</span>
<span aria-hidden className="block-editor-link-control__search-item-info">
<span
aria-hidden
className="block-editor-link-control__search-item-info"
style={{
paddingRight: !showType ? 0 : null,
}}
>
{filterURLForDisplay(safeDecodeURI(suggestion.url)) || ''}
</span>
</span>
{suggestion.type && (
{showType && (
<span className="block-editor-link-control__search-item-type">
{/* Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label. */}
{suggestion.type === 'post_tag' ? 'tag' : suggestion.type}
Expand All @@ -56,6 +69,7 @@ SearchItem.propTypes = {
suggestion: PropTypes.object.isRequired,
onClick: PropTypes.func.isRequired,
isSelected: PropTypes.bool,
contentTypes: PropTypes.array.isRequired,
};

export default SearchItem;
68 changes: 45 additions & 23 deletions components/ContentSearch/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { TextControl, Spinner, NavigableMenu } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { useState } from '@wordpress/element'; // eslint-disable-line
import { useState, useRef, useEffect } from '@wordpress/element'; // eslint-disable-line
import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n';
import SearchItem from './SearchItem';

const NAMESPACE = '10up-block-components';
const NAMESPACE = '10up-content-search';

const searchCache = {};
johnwatkins0 marked this conversation as resolved.
Show resolved Hide resolved

const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, excludeItems }) => {
const [searchString, setSearchString] = useState('');
const [searchResults, setSearchResults] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [selectedItem, setSelectedItem] = useState(null);

const mounted = useRef(true);

/**
* handleSelection
*
Expand Down Expand Up @@ -55,28 +59,45 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
',',
)}&type=${mode}`;

apiFetch({
path: searchQuery,
}).then((results) => {
const newResults = results.filter((result) => {
let keep = true;

if (excludeItems.length) {
excludeItems.forEach((item) => {
if (item.id === result.id) {
keep = false;
}
});
if (searchCache[searchQuery]) {
setSearchResults(searchCache[searchQuery]);
setIsLoading(false);
} else {
apiFetch({
path: searchQuery,
}).then((results) => {
if (mounted.current === false) {
return;
}

return keep;
});
const newResults = results.filter((result) => {
let keep = true;
johnwatkins0 marked this conversation as resolved.
Show resolved Hide resolved

setSearchResults(newResults);
setIsLoading(false);
});
if (excludeItems.length) {
excludeItems.forEach((item) => {
if (item.id === result.id) {
keep = false;
}
});
}

return keep;
});
johnwatkins0 marked this conversation as resolved.
Show resolved Hide resolved

searchCache[searchQuery] = newResults;

setSearchResults(newResults);
setIsLoading(false);
});
}
};

useEffect(() => {
return () => {
mounted.current = false;
};
}, []);

return (
<NavigableMenu onNavigate={handleSelection} orientation="vertical">
<TextControl
Expand All @@ -88,7 +109,7 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
/>
{hasSearchString ? (
<ul
className={`${NAMESPACE}-grid`}
className={`${NAMESPACE}-list`}
style={{
marginTop: '0',
marginBottom: '0',
Expand All @@ -100,7 +121,7 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
{isLoading && <Spinner />}
{!isLoading && !hasSearchResults && (
<li
className={`${NAMESPACE}-grid-item components-button`}
className={`${NAMESPACE}-list-item components-button`}
style={{ color: 'inherit', cursor: 'default', paddingLeft: '3px' }}
>
{__('Nothing found.', '10up-block-components')}
Expand All @@ -114,7 +135,7 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
return (
<li
key={item.id}
className={`${NAMESPACE}-grid-item`}
className={`${NAMESPACE}-list-item`}
style={{
marginBottom: '0',
}}
Expand All @@ -123,6 +144,7 @@ const ContentSearch = ({ onSelectItem, placeholder, label, contentTypes, mode, e
onClick={() => handleItemSelection(item)}
searchTerm={searchString}
suggestion={item}
contentTypes={contentTypes}
isSelected={selectedItem === index + 1}
/>
</li>
Expand All @@ -141,7 +163,7 @@ ContentSearch.defaultProps = {
excludeItems: [],
mode: 'post',
onSelectItem: () => {
console.log('Select!');
console.log('Select!'); // eslint-disable-line no-console
},
};

Expand Down