No Preview

Sorry, but you either have no stories or none are selected somehow.

If the problem persists, check the browser console, or the terminal you've run Storybook from.

SuggestionBlock

A SuggestionBlock component shows the different options of a suggestion block that are given by the usePreviewSearch widget.

Note: SuggestionBlock component needs to be within the context of a PreviewSearch widget. It cannot be used in isolation. The examples above describe how to use it.

Properties

Property NameDescriptionTypeRequired
itemsSuggestion items to showArray<{ text: string }>true
titleTitle of the sectionstringtrue
blockIdId of the suggestion block configured on CECstringtrue
filterAttributefield id from where the suggestion block should filterstringfalse
disabledDeactivates the hover actionbooleanfalse

Example


  // First, import what we need from React SDK
  import { widget, usePreviewSearch, WidgetDataType } from "@sitecore-search/react";
  // Import PreviewSearch primitive from UI package
  import { PreviewSearch } from '@sitecore-search/ui';
  //import our component
  import SuggestionBlock from '@/components/SuggestionBlock';
  // SuggestionBlock needs to be under the Preview Search widget context
  // and also wrapped by PreviewSearch.Root component.
  // To accomplish that, we need to create a widget using the
  // widget function and to get facet data we use usePreviewSearch hook.
  // Also we need to import PreviewSearch component from @sitecore-search/ui
  // and add the content within PreviewSearch.Root.
  const PreviewSearchComponent = () => {
  const {
    queryResult: {
      isLoading,
      data: {
        suggestion: {
          title_context_aware: suggestions = [],
        } = {},
      } = {},
    },
  } = usePreviewSearch({
    state: {
      suggestionsList: [{ suggestion: 'title_context_aware', max: 4 }],
      itemsPerPage: 4,
      keyphrase: 'sitecore'
    },
  });
  return <PreviewSearch.Root>{!isLoading && <SuggestionBlock items={suggestions} title={'Suggestions'} blockId={'title_context_aware'} />}</PreviewSearch.Root>;
};

  // Convert the regular component to a compatible widget.
  const PreviewSearchWrapperWidget = widget(PreviewSearchComponent, WidgetDataType.PREVIEW_SEARCH, 'content');

  // Then, we can use the widget
  function MyApp() {
    return (
      <WidgetsProvider {...CONFIG}>
        <PreviewSearchWrapperWidget rfkId="rfkid_6" />
      </WidgetsProvider>
    )
  };

Installation

npx sc-search new-component -w -s styled -l typescript -n SuggestionBlock -o <destination/path>