diff --git a/plugins/search-react/src/components/SearchResult/SearchResult.tsx b/plugins/search-react/src/components/SearchResult/SearchResult.tsx index a3e4c0e1f0..40c60c3263 100644 --- a/plugins/search-react/src/components/SearchResult/SearchResult.tsx +++ b/plugins/search-react/src/components/SearchResult/SearchResult.tsx @@ -36,7 +36,7 @@ export type SearchResultContextProps = { /** * A child function that receives an asynchronous result set and returns a react element. */ - children: (state: AsyncState) => JSX.Element; + children: (state: AsyncState) => JSX.Element | null; }; /** diff --git a/plugins/search-react/src/components/SearchResultGroup/SearchResultGroup.tsx b/plugins/search-react/src/components/SearchResultGroup/SearchResultGroup.tsx index 9c55996ca7..2a82dd7fee 100644 --- a/plugins/search-react/src/components/SearchResultGroup/SearchResultGroup.tsx +++ b/plugins/search-react/src/components/SearchResultGroup/SearchResultGroup.tsx @@ -282,6 +282,10 @@ export type SearchResultGroupLayoutProps = ListProps & { * Icon that representing a result group. */ icon: JSX.Element; + /** + * Optional component to render when no results. Default to component. + */ + noResultsComponent?: React.ReactNode; /** * The results group title content, it could be a text or an element. */ @@ -347,6 +351,9 @@ export function SearchResultGroupLayout( const { loading, error, + noResultsComponent = ( + + ), icon, title, titleProps = {}, @@ -438,9 +445,7 @@ export function SearchResultGroupLayout( ? resultItems.map(resultItem => renderResultItem?.(resultItem) ?? null) : null} {!loading && !error && !resultItems?.length ? ( - - - + {noResultsComponent} ) : null} ); @@ -458,6 +463,10 @@ export type SearchResultGroupProps = Omit< * A search query used for requesting the results to be grouped. */ query: Partial; + /** + * Optional property to provide if component should not render the group when no results are found. + */ + disableRenderingWithNoResults?: boolean; }; /** @@ -474,6 +483,7 @@ export function SearchResultGroup( renderResultItem = ({ document }) => ( ), + disableRenderingWithNoResults, ...rest } = props; @@ -495,17 +505,22 @@ export function SearchResultGroup( }} > - {({ loading, error, value }) => ( - - )} + {({ loading, error, value }) => { + if (disableRenderingWithNoResults && value?.results.length === 0) + return null; + + return ( + + ); + }} );