import { Children, createContext, useContext } from 'react'; import { ConditionalWrapper, WrapperTag } from '../../common'; import AspectBox from '../AspectBox'; import Metadata from '../PageMetadata'; import { SearchResultContextProps, SearchResultProps } from './types'; import clsx from 'clsx'; const SearchResultLinkHrefContext = createContext(''); const SearchResultContent = ({ children }: React.AllHTMLAttributes) => { const otherChildren: React.ReactNode[] = []; let imageChild: React.ReactNode = null; // assign to slots Children.forEach(children, (child: React.ReactNode) => { const thisChild = child as React.JSX.Element; if (thisChild && thisChild.type === SearchResultMedia) { imageChild = thisChild; } else { otherChildren.push(thisChild); } }); return ( imageChild ? (
{imageChild}
{otherChildren}
) : (
{otherChildren}
) ) }; const SearchResultContext = ({ children, title = 'Part of' }: SearchResultContextProps) => { return (
{title}:
{children}
) }; const SearchResultContextItem = ({ children }: React.AllHTMLAttributes) => { return (
{children}
) }; const SearchResultMedia = ({ children }: React.AllHTMLAttributes) => { return (
) }; const SearchResultMeta = ({ children }: React.AllHTMLAttributes) => { return ( {children} ) }; const SearchResult = ({ children, href, isPromoted, linkComponent, promotedTitle = 'Recommended', tagName = 'li', title, ...props }: SearchResultProps) => { const LINK_CLASS = 'ds_search-result__link'; return (
{promotedTitle}
{children}
} >

{linkComponent ? linkComponent({ className: LINK_CLASS, children: title, href }) : {title} }

{children}
); }; SearchResult.Content = SearchResultContent; SearchResult.Context = SearchResultContext; SearchResult.ContextItem = SearchResultContextItem; SearchResult.Media = SearchResultMedia; SearchResult.Metadata = SearchResultMeta; SearchResultContent.displayName = 'SearchResult.Content'; SearchResultContext.displayName = 'SearchResult.Context'; SearchResultContextItem.displayName = 'SearchResult.ContextItem'; SearchResultMedia.displayName = 'SearchResult.Media'; SearchResultMeta.displayName = 'SearchResult.Metadata'; export default SearchResult;