File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change 55 aria-label =" Search"
66 class =" sf-header__search"
77 :value =" term"
8- @input =" handleSearch "
9- @keydown .enter =" handleSearch ($event) "
8+ @input =" debouncedHandleSearch($event) "
9+ @keyup .enter =" handleKeydownEnter ($event.target.value) /* https://github.com/vuestorefront/storefront-ui/issues/2453#issuecomment-1160231619 */ "
1010 @keydown.tab =" hideSearch"
1111 @focus =" showSearch"
1212 @click =" showSearch"
@@ -130,7 +130,7 @@ export default defineComponent({
130130 }
131131 };
132132
133- const handleSearch = debounce ( async (searchTerm : string ) => {
133+ const rawHandleSearch = async (searchTerm : string ) => {
134134 term .value = searchTerm ;
135135 if (term .value .length < props .minTermLen ) return ;
136136
@@ -141,7 +141,15 @@ export default defineComponent({
141141 }) as unknown as Products ;
142142
143143 emit (' set-search-results' , productList ! .items );
144- }, 1000 );
144+ };
145+
146+ const debouncedHandleSearch = debounce (rawHandleSearch , 1000 );
147+
148+ const handleKeydownEnter = (searchTerm : string ) => {
149+ // cancel debounce timeout started by typing into the searchbar - this is to avoid making two network requests instead of one
150+ debouncedHandleSearch .cancel ();
151+ rawHandleSearch (searchTerm );
152+ };
145153
146154 watch (route , () => {
147155 hideSearch ();
@@ -153,7 +161,9 @@ export default defineComponent({
153161 showSearch ,
154162 hideSearch ,
155163 toggleSearch ,
156- handleSearch ,
164+ rawHandleSearch ,
165+ debouncedHandleSearch ,
166+ handleKeydownEnter ,
157167 term ,
158168 };
159169 },
You can’t perform that action at this time.
0 commit comments