3
3
import { autocomplete } from '@algolia/autocomplete-js' ;
4
4
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions' ;
5
5
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches' ;
6
- import { h , Fragment } from 'preact ' ;
6
+ import { InstantSearch } from 'instantsearch.js ' ;
7
7
8
8
import {
9
9
debouncedSetInstantSearchUiState ,
@@ -42,29 +42,27 @@ function getItemUrl({ query, category }) {
42
42
} ) ;
43
43
}
44
44
45
- function ItemWrapper ( { children, query, category } ) {
45
+ function getItemWrapper ( { html , children, query, category } ) {
46
46
const uiState = {
47
47
query,
48
48
hierarchicalMenu : {
49
49
[ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE ] : [ category ] ,
50
50
} ,
51
51
} ;
52
52
53
- return (
54
- < a
55
- className = "aa-ItemLink"
56
- href = { getInstantSearchUrl ( uiState ) }
57
- onClick = { ( event ) => {
58
- if ( ! isModifierEvent ( event ) ) {
59
- // Bypass the original link behavior if there's no event modifier
60
- // to set the InstantSearch UI state without reloading the page.
61
- event . preventDefault ( ) ;
62
- }
63
- } }
64
- >
65
- { children }
66
- </ a >
67
- ) ;
53
+ return html `< a
54
+ class ="aa-ItemLink "
55
+ href =${ getInstantSearchUrl ( uiState ) }
56
+ onClick =${ ( event ) => {
57
+ if ( ! isModifierEvent ( event ) ) {
58
+ // Bypass the original link behavior if there's no event modifier
59
+ // to set the InstantSearch UI state without reloading the page.
60
+ event . preventDefault ( ) ;
61
+ }
62
+ } }
63
+ >
64
+ ${ children }
65
+ </ a > ` ;
68
66
}
69
67
70
68
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin ( {
@@ -94,15 +92,14 @@ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
94
92
// and plug it to the InstantSearch router.
95
93
item ( params ) {
96
94
const { children } = ( source . templates . item ( params ) as any ) . props ;
95
+ const { item, html } = params ;
97
96
98
- return (
99
- < ItemWrapper
100
- query = { params . item . label }
101
- category = { params . item . category }
102
- >
103
- { children }
104
- </ ItemWrapper >
105
- ) ;
97
+ return getItemWrapper ( {
98
+ query : item . label ,
99
+ category : item . category ,
100
+ html,
101
+ children,
102
+ } ) ;
106
103
} ,
107
104
} ,
108
105
} ;
@@ -115,7 +112,7 @@ const querySuggestionsPluginInCategory = createQuerySuggestionsPlugin({
115
112
getSearchParams ( ) {
116
113
const currentCategory = getInstantSearchCurrentCategory ( ) ;
117
114
118
- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
115
+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
119
116
hitsPerPage : 3 ,
120
117
facetFilters : [
121
118
`${ INSTANT_SEARCH_INDEX_NAME } .facets.exact_matches.${ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE } .value:${ currentCategory } ` ,
@@ -152,22 +149,22 @@ const querySuggestionsPluginInCategory = createQuerySuggestionsPlugin({
152
149
} ,
153
150
templates : {
154
151
...source . templates ,
155
- header ( ) {
156
- return (
157
- < Fragment >
158
- < span className = "aa-SourceHeaderTitle" > In { currentCategory } </ span >
159
- < div className = "aa-SourceHeaderLine" />
160
- </ Fragment >
161
- ) ;
152
+ header ( { html } ) {
153
+ return html `
154
+ < span class ="aa-SourceHeaderTitle "> In ${ currentCategory } </ span >
155
+ < div class ="aa-SourceHeaderLine " />
156
+ ` ;
162
157
} ,
163
158
item ( params ) {
164
159
const { children } = ( source . templates . item ( params ) as any ) . props ;
160
+ const { item, html } = params ;
165
161
166
- return (
167
- < ItemWrapper query = { params . item . query } category = { currentCategory } >
168
- { children }
169
- </ ItemWrapper >
170
- ) ;
162
+ return getItemWrapper ( {
163
+ query : item . query ,
164
+ category : currentCategory ,
165
+ html,
166
+ children,
167
+ } ) ;
171
168
} ,
172
169
} ,
173
170
} ;
@@ -181,12 +178,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
181
178
const currentCategory = getInstantSearchCurrentCategory ( ) ;
182
179
183
180
if ( ! currentCategory ) {
184
- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
181
+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
185
182
hitsPerPage : 6 ,
186
183
} ) ;
187
184
}
188
185
189
- return recentSearchesPlugin . data . getAlgoliaSearchParams ( {
186
+ return recentSearchesPlugin . data ! . getAlgoliaSearchParams ( {
190
187
hitsPerPage : 3 ,
191
188
facetFilters : [
192
189
`${ INSTANT_SEARCH_INDEX_NAME } .facets.exact_matches.${ INSTANT_SEARCH_HIERARCHICAL_ATTRIBUTE } .value:-${ currentCategory } ` ,
@@ -229,29 +226,26 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
229
226
} ,
230
227
templates : {
231
228
...source . templates ,
232
- header ( ) {
229
+ header ( { html } ) {
233
230
if ( ! currentCategory ) {
234
231
return null ;
235
232
}
236
233
237
- return (
238
- < Fragment >
239
- < span className = "aa-SourceHeaderTitle" > In other categories</ span >
240
- < div className = "aa-SourceHeaderLine" />
241
- </ Fragment >
242
- ) ;
234
+ return html `
235
+ < span class ="aa-SourceHeaderTitle "> In other categories</ span >
236
+ < div class ="aa-SourceHeaderLine " />
237
+ ` ;
243
238
} ,
244
239
item ( params ) {
245
240
const { children } = ( source . templates . item ( params ) as any ) . props ;
241
+ const { item, html } = params ;
246
242
247
- return (
248
- < ItemWrapper
249
- query = { params . item . query }
250
- category = { params . item . __autocomplete_qsCategory }
251
- >
252
- { children }
253
- </ ItemWrapper >
254
- ) ;
243
+ return getItemWrapper ( {
244
+ query : item . query ,
245
+ category : item . __autocomplete_qsCategory ,
246
+ html,
247
+ children,
248
+ } ) ;
255
249
} ,
256
250
} ,
257
251
} ;
@@ -260,8 +254,10 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
260
254
261
255
const searchPageState = getInstantSearchUiState ( ) ;
262
256
263
- export function startAutocomplete ( ) {
264
- autocomplete ( {
257
+ export function startAutocomplete ( searchInstance : InstantSearch ) {
258
+ let skipInstantSearchStateUpdate = false ;
259
+
260
+ const { setQuery } = autocomplete ( {
265
261
container : '#autocomplete' ,
266
262
placeholder : 'Search for products' ,
267
263
openOnFocus : true ,
@@ -292,9 +288,17 @@ export function startAutocomplete() {
292
288
} ) ;
293
289
} ,
294
290
onStateChange ( { prevState, state } ) {
295
- if ( prevState . query !== state . query ) {
291
+ if ( ! skipInstantSearchStateUpdate && prevState . query !== state . query ) {
296
292
debouncedSetInstantSearchUiState ( { query : state . query } ) ;
297
293
}
294
+ skipInstantSearchStateUpdate = false ;
298
295
} ,
299
296
} ) ;
297
+
298
+ window . addEventListener ( 'popstate' , ( ) => {
299
+ skipInstantSearchStateUpdate = true ;
300
+ setQuery (
301
+ ( searchInstance . helper && searchInstance . helper . state . query ) || ''
302
+ ) ;
303
+ } ) ;
300
304
}
0 commit comments