diff --git a/packages/catalog-search/src/SearchBox.jsx b/packages/catalog-search/src/SearchBox.jsx
index 5d76226c..22007aac 100644
--- a/packages/catalog-search/src/SearchBox.jsx
+++ b/packages/catalog-search/src/SearchBox.jsx
@@ -44,12 +44,14 @@ export const SearchBoxBase = ({
enterpriseSlug,
suggestionSubmitOverride,
disableSuggestionRedirect,
+ isPreQueryEnabled,
}) => {
const { dispatch, trackingName } = useContext(SearchContext);
const [autocompleteHits, setAutocompleteHits] = useState([]);
const [showSuggestions, setShowSuggestions] = useState(false);
const [searchQuery, setSearchQuery] = useState('');
+ const [preQueryHits, setPreQueryHits] = useState([]);
/**
* Handles when a search is submitted by adding the user's search
@@ -91,7 +93,6 @@ export const SearchBoxBase = ({
// Track the focused element
const focusedElement = useActiveElement();
-
// Function to be called when the user stops typing, will fetch algolia hits for query after `DEBOUNCE_TIME_MS` has
// elapsed
const debounceFunc = async (query) => {
@@ -103,15 +104,23 @@ export const SearchBoxBase = ({
attributesToRetrieve: ALGOLIA_ATTRIBUTES_TO_RETRIEVE,
});
if (nbHits > 0) {
+ setPreQueryHits([]);
setAutocompleteHits(hits);
setShowSuggestions(true);
} else {
// If there are no results of the suggested search, hide the empty suggestion component
setShowSuggestions(false);
}
- // Hide the results as soon as the user removes the entire query string, instead of waiting a second
- } else {
- setShowSuggestions(false);
+ // If isPreQueryEnabled is true display the prequery results when user clicks on search box but has not began typing
+ } else if (query === '' && isPreQueryEnabled) {
+ const { hits } = await index.search(query, {
+ filters,
+ attributesToHighlight: ['title'],
+ attributesToRetrieve: ALGOLIA_ATTRIBUTES_TO_RETRIEVE,
+ });
+ setAutocompleteHits([]);
+ setPreQueryHits(hits);
+ setShowSuggestions(true);
}
};
// Since the debounced method is called in a useEffect hook, use `useCallback` to account for repeated invoking of the
@@ -124,7 +133,7 @@ export const SearchBoxBase = ({
if (index !== undefined && focusedElement.classList.contains(SEARCH_BOX_CLASS_NAME)) {
debounceHandler(searchQuery);
}
- // Retry this method if the focused element or the search query changes
+ // Retry this method if the focused element or the search query changes
}, [searchQuery, focusedElement]);
/**
@@ -143,10 +152,10 @@ export const SearchBoxBase = ({
return (