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

Refactor ContentSearch to use react query #326

Merged
merged 15 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
130 changes: 53 additions & 77 deletions components/content-search/SearchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import {
__experimentalTruncate as Truncate,
} from '@wordpress/components';
import { getTextContent, create } from '@wordpress/rich-text';
import { RenderItemComponentProps } from './types';
import { NormalizedSuggestion } from './utils';

const ButtonStyled = styled(Button)`
const SearchItemWrapper = styled(Button)`
display: flex;
text-align: left;
width: 100%;
justify-content: space-between;
align-items: center;
border-radius: 2px;
box-sizing: border-box;
height: auto !important;
padding: 0.3em 0.7em;
overflow: hidden;
Expand All @@ -24,112 +27,85 @@ const ButtonStyled = styled(Button)`
/* Add opacity background to support future color changes */
/* Reduce background from #ddd to 0.05 for text contrast */
background-color: rgba(0, 0, 0, 0.05);

.block-editor-link-control__search-item-type {
color: black;
}
}
`;

.block-editor-link-control__search-item-type {
background-color: rgba(0, 0, 0, 0.05);
padding: 2px 4px;
text-transform: capitalize;
border-radius: 2px;
flex-shrink: 0;
}
const SearchItemHeader = styled.span`
display: flex;
flex-direction: column;
align-items: flex-start;
`;

.block-editor-link-control__search-item-header {
display: flex;
flex-direction: column;
align-items: flex-start;
}
const SearchItemTitle = styled.span<{showType: boolean}>`
padding-right: ${({ showType }) => (showType ? 0 : undefined)};
`;

mark {
padding: 0 !important;
margin: 0 !important;
}
const SearchItemInfo = styled.span<{showType: boolean}>`
padding-right: ${({ showType }) => (showType ? 0 : undefined)};
`;

export interface Suggestion {
id: number;
title: string;
url: string;
type: string;
subtype: string;
}
const SearchItemType = styled.span`
background-color: rgba(0, 0, 0, 0.05);
color: black;
padding: 2px 4px;
text-transform: capitalize;
border-radius: 2px;
flex-shrink: 0;
`;

interface SearchItemProps {
suggestion: Suggestion;
onClick: () => void;
searchTerm?: string;
isSelected?: boolean;
id?: string;
contentTypes: string[];
renderType?: (suggestion: Suggestion) => string;
}
const StyledTextHighlight = styled(TextHighlight)`
margin: 0 !important;
padding: 0 !important;
`;

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

const richTextContent = create({ html: suggestion.title });
const textContent = getTextContent(richTextContent);
const titleContent = decodeEntities(textContent);

return (
<Tooltip text={decodeEntities(suggestion.title)}>
<ButtonStyled
id={id}
onClick={onClick}
className={`block-editor-link-control__search-item is-entity ${
isSelected && 'is-selected'
}`}
style={{
borderRadius: '0',
boxSizing: 'border-box',
}}
>
<span className="block-editor-link-control__search-item-header">
<span
className="block-editor-link-control__search-item-title"
style={{
paddingRight: !showType ? 0 : undefined,
}}
>
<TextHighlight text={titleContent} highlight={searchTerm} />
</span>
<span
aria-hidden
className="block-editor-link-control__search-item-info"
style={{
paddingRight: !showType ? 0 : undefined,
}}
>
<SearchItemWrapper id={id} onClick={onClick}>
<SearchItemHeader>
<SearchItemTitle showType={showType}>
<StyledTextHighlight text={titleContent} highlight={searchTerm} />
</SearchItemTitle>
<SearchItemInfo aria-hidden showType={showType}>
<Truncate numberOfLines={1} limit={55} ellipsizeMode="middle">
{filterURLForDisplay(safeDecodeURI(suggestion.url)) || ''}
</Truncate>
</span>
</span>
</SearchItemInfo>
</SearchItemHeader>
{showType && (
<span className="block-editor-link-control__search-item-type">
<SearchItemType>
{renderType(suggestion)}
</span>
</SearchItemType>
)}
</ButtonStyled>
</SearchItemWrapper>
</Tooltip>
);
};

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.subtype;
export function defaultRenderItemType(suggestion: NormalizedSuggestion): string {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localized CPT or taxonomy label.
if ( suggestion.type === 'post_tag' ) {
return 'tag';
}

if ( suggestion.subtype ) {
return suggestion.subtype;
}

return suggestion.type;
}

export default SearchItem;
Loading
Loading