Skip to content

Commit c1962d3

Browse files
committed
MAGE-744 Implement autocomplete min char incl suggestions plugin
1 parent d4dfbd9 commit c1962d3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

view/frontend/web/autocomplete.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ define(
1616

1717
const DEFAULT_HITS_PER_SECTION = 2;
1818
const DEBOUNCE_MS = 300;
19+
const MIN_SEARCH_LENGTH_CHARS = 3;
1920

2021
// global state
2122
let suggestionSection = false;
@@ -349,8 +350,8 @@ define(
349350
transformSource({source}) {
350351
return {
351352
...source,
352-
getItems() {
353-
const items = source.getItems();
353+
getItems({ query }) {
354+
const items = filterMinChars(query, source.getItems());
354355
const oldTransform = items.transformResponse;
355356
items.transformResponse = arg => {
356357
const hits = oldTransform ? oldTransform(arg) : arg.hits;
@@ -385,6 +386,12 @@ define(
385386
});
386387
};
387388

389+
const filterMinChars = (query, result) => {
390+
return (query.length >= MIN_SEARCH_LENGTH_CHARS)
391+
? result
392+
: [];
393+
}
394+
388395
const debouncePromise = (fn, time) => {
389396
let timerId = undefined;
390397

@@ -453,9 +460,12 @@ define(
453460
window.location.href = algoliaConfig.resultPageUrl + `?q=${data.state.query}`;
454461
}
455462
},
456-
getSources() {
457-
return debounced(autocompleteConfig);
463+
getSources({query}) {
464+
return filterMinChars(query, debounced(autocompleteConfig));
458465
},
466+
shouldPanelOpen({ state }) {
467+
return state.query.length >= MIN_SEARCH_LENGTH_CHARS;
468+
}
459469
};
460470

461471
if (isMobile() === true) {

0 commit comments

Comments
 (0)