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

Allow passing in custom search result render function to ContentSearch #294

Merged
merged 3 commits into from
Feb 27, 2024
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
5 changes: 5 additions & 0 deletions components/content-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ContentPickerWrapper = styled.div`
* @param {number} props.perPage number of items to show per page
* @param {boolean} props.fetchInitialResults whether or not to fetch initial results on mount
* @param {Function} props.renderItemType callback to render the item type
* @param {Function} props.renderItem react component to render the search result item
* @returns {*} React JSX
*/
const ContentPicker = ({
Expand All @@ -74,6 +75,7 @@ const ContentPicker = ({
perPage,
fetchInitialResults,
renderItemType,
renderItem,
}) => {
const currentPostId = select('core/editor')?.getCurrentPostId();

Expand Down Expand Up @@ -141,6 +143,7 @@ const ContentPicker = ({
perPage={perPage}
fetchInitialResults={fetchInitialResults}
renderItemType={renderItemType}
renderItem={renderItem}
/>
) : (
label && (
Expand Down Expand Up @@ -205,6 +208,7 @@ ContentPicker.defaultProps = {
singlePickedLabel: __('You have selected the following item:', '10up-block-components'),
fetchInitialResults: false,
renderItemType: defaultRenderItemType,
renderItem: undefined,
};

ContentPicker.propTypes = {
Expand All @@ -225,6 +229,7 @@ ContentPicker.propTypes = {
perPage: PropTypes.number,
fetchInitialResults: PropTypes.bool,
renderItemType: PropTypes.func,
renderItem: PropTypes.func,
};

export { ContentPicker };
53 changes: 41 additions & 12 deletions components/content-search/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Spinner, NavigableMenu, Button, SearchControl } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';
import { addQueryArgs } from '@wordpress/url';
import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n';
import styled from '@emotion/styled';
Expand Down Expand Up @@ -50,6 +51,7 @@ const ContentSearch = ({
queryFilter,
excludeItems,
renderItemType,
renderItem: RenderItemComponent,
fetchInitialResults,
}) => {
const [searchString, setSearchString] = useState('');
Expand Down Expand Up @@ -111,12 +113,20 @@ const ContentSearch = ({

switch (mode) {
case 'user':
searchQuery = `wp/v2/users/?search=${keyword}`;
searchQuery = addQueryArgs('wp/v2/users', {
search: keyword,
});
break;
default:
searchQuery = `wp/v2/search/?search=${keyword}&subtype=${contentTypes.join(
',',
)}&type=${mode}&_embed&per_page=${perPage}&page=${page}`;
searchQuery = addQueryArgs('wp/v2/search', {
search: keyword,
subtype: contentTypes.join(','),
type: mode,
_embed: true,
per_page: perPage,
page,
});

break;
}

Expand Down Expand Up @@ -385,6 +395,12 @@ const ContentSearch = ({
return null;
}

const isSelected = selectedItem === index + 1;

const selectItem = () => {
handleItemSelection(item);
};

return (
<li
key={item.id}
Expand All @@ -393,14 +409,25 @@ const ContentSearch = ({
marginBottom: '0',
}}
>
<SearchItem
onClick={() => handleItemSelection(item)}
searchTerm={searchString}
suggestion={item}
contentTypes={contentTypes}
isSelected={selectedItem === index + 1}
renderType={renderItemType}
/>
{RenderItemComponent ? (
<RenderItemComponent
item={item}
onSelect={selectItem}
searchTerm={searchString}
contentTypes={contentTypes}
isSelected={isSelected}
renderType={renderItemType}
/>
) : (
<SearchItem
onClick={selectItem}
searchTerm={searchString}
suggestion={item}
contentTypes={contentTypes}
isSelected={isSelected}
renderType={renderItemType}
/>
)}
</li>
);
})}
Expand Down Expand Up @@ -435,6 +462,7 @@ ContentSearch.defaultProps = {
console.log('Select!'); // eslint-disable-line no-console
},
renderItemType: defaultRenderItemType,
renderItem: undefined,
fetchInitialResults: false,
};

Expand All @@ -449,6 +477,7 @@ ContentSearch.propTypes = {
hideLabelFromVision: PropTypes.bool,
perPage: PropTypes.number,
renderItemType: PropTypes.func,
renderItem: PropTypes.func,
fetchInitialResults: PropTypes.bool,
};

Expand Down
Loading