Skip to content

Commit

Permalink
Merge pull request #325 from Souptik2001/refactor/content-search-comp…
Browse files Browse the repository at this point in the history
…onent-to-ts

Migrate `ContentSearch` component to TS
  • Loading branch information
fabiankaegy committed Jun 6, 2024
2 parents a2d8101 + 30c3332 commit 2083983
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -51,27 +50,32 @@ const ButtonStyled = styled(Button)`
}
`;

/**
* SearchItem
*
* @param {object} props react props
* @param {object} props.suggestion suggestion object
* @param {Array} props.contentTypes array of content types
* @param {Function} props.onClick callback for when the item is clicked
* @param {string} props.searchTerm the search term
* @param {boolean} props.isSelected whether the item is selected
* @param {string} props.id the id of the item
* @param {Function} props.renderType a callback to override the type text
* @returns {*} React JSX
*/
const SearchItem = ({
export interface Suggestion {
id: number;
title: string;
url: string;
type: string;
subtype: string;
}

interface SearchItemProps {
suggestion: Suggestion;
onClick: () => void;
searchTerm?: string;
isSelected?: boolean;
id?: string;
contentTypes: string[];
renderType?: (suggestion: Suggestion) => string;
}

const SearchItem: React.FC<SearchItemProps> = ({
suggestion,
onClick,
searchTerm,
isSelected,
id,
searchTerm = '',
isSelected = false,
id = '',
contentTypes,
renderType,
renderType = defaultRenderItemType,
}) => {
const showType = suggestion.type && contentTypes.length > 1;

Expand All @@ -96,7 +100,7 @@ const SearchItem = ({
<span
className="block-editor-link-control__search-item-title"
style={{
paddingRight: !showType ? 0 : null,
paddingRight: !showType ? 0 : undefined,
}}
>
<TextHighlight text={titleContent} highlight={searchTerm} />
Expand All @@ -105,7 +109,7 @@ const SearchItem = ({
aria-hidden
className="block-editor-link-control__search-item-info"
style={{
paddingRight: !showType ? 0 : null,
paddingRight: !showType ? 0 : undefined,
}}
>
<Truncate numberOfLines={1} limit={55} ellipsizeMode="middle">
Expand All @@ -123,26 +127,9 @@ const SearchItem = ({
);
};

export function defaultRenderItemType(suggestion) {
export function defaultRenderItemType(suggestion: Suggestion): string {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label.
return suggestion.type === 'post_tag' ? 'tag' : suggestion.type;
return suggestion.type === 'post_tag' ? 'tag' : suggestion.subtype;
}

SearchItem.defaultProps = {
id: '',
searchTerm: '',
isSelected: false,
renderType: defaultRenderItemType,
};

SearchItem.propTypes = {
id: PropTypes.string,
searchTerm: PropTypes.string,
suggestion: PropTypes.object.isRequired,
onClick: PropTypes.func.isRequired,
isSelected: PropTypes.bool,
contentTypes: PropTypes.array.isRequired,
renderType: PropTypes.func,
};

export default SearchItem;
Loading

0 comments on commit 2083983

Please sign in to comment.