-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathatomic-commerce-search-box.tsx
More file actions
766 lines (702 loc) · 24.5 KB
/
atomic-commerce-search-box.tsx
File metadata and controls
766 lines (702 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import {isNullOrUndefined} from '@coveo/bueno';
import {
SearchBoxOptions,
StandaloneSearchBoxState,
StandaloneSearchBox,
SearchBox,
SearchBoxState,
buildSearchBox,
buildStandaloneSearchBox,
loadQuerySetActions,
} from '@coveo/headless/commerce';
import {
Component,
Element,
Event,
EventEmitter,
Host,
Listen,
Prop,
State,
Watch,
h,
} from '@stencil/core';
import {AriaLiveRegion} from '../../../utils/accessibility-utils';
import {hasKeyboard, isMacOS} from '../../../utils/device-utils';
import {
BindStateToController,
InitializableComponent,
InitializeBindings,
} from '../../../utils/initialization-utils';
import {
SafeStorage,
StandaloneSearchBoxData,
StorageItems,
} from '../../../utils/local-storage-utils';
import {updateBreakpoints} from '../../../utils/replace-breakpoint';
import {
isFocusingOut,
once,
randomID,
spreadProperties,
} from '../../../utils/utils';
import {SearchBoxWrapper} from '../../common/search-box/search-box-wrapper';
import {SearchTextArea} from '../../common/search-box/search-text-area';
import {SubmitButton} from '../../common/search-box/submit-button';
import {SuggestionManager} from '../../common/suggestions/suggestion-manager';
import {
SearchBoxSuggestionElement,
SearchBoxSuggestionsBindings,
SearchBoxSuggestionsEvent,
elementHasQuery,
} from '../../common/suggestions/suggestions-common';
import {RedirectionPayload} from '../../search/atomic-search-box/redirection-payload';
import {
ButtonSearchSuggestion,
SimpleSearchSuggestion,
} from '../../search/atomic-search-box/search-suggestion';
import type {CommerceBindings as Bindings} from '../atomic-commerce-interface/atomic-commerce-interface';
import {SelectChildProductEventArgs} from '../product-template-components/atomic-product-children/atomic-product-children';
/**
* The `atomic-commerce-search-box` component creates a search box with built-in support for suggestions.
*
* @slot default - The default slot where you can add child components to the search box.
*
* @part wrapper - The search box wrapper.
* @part input - The search box input.
* @part loading - The search box loading animation.
* @part clear-button - The button to clear the search box of input.
* @part clear-icon - The clear button's icon.
* @part submit-button - The search box submit button.
* @part submit-icon - The search box submit button's icon.
* @part suggestions - A list of suggested query corrections on each panel.
* @part suggestions-left - A list of suggested query corrections on the left panel.
* @part suggestions-right - A list of suggested query corrections on the right panel.
* @part suggestions-wrapper - The wrapper that contains suggestion panels.
* @part suggestions-single-list - The wrapper that contains 1 suggestion list.
* @part suggestions-double-list - The wrapper that contains 2 suggestion lists.
* @part suggestion - A suggested query correction.
* @part active-suggestion - The currently active suggestion.
* @part suggestion-divider - An item in the list that separates groups of suggestions.
* @part suggestion-with-query - An item in the list that will update the search box query.
*
* @part query-suggestion-item - A suggestion from the `atomic-commerce-search-box-query-suggestions` component.
* @part query-suggestion-content - The contents of a suggestion from the `atomic-commerce-search-box-query-suggestions` component.
* @part query-suggestion-icon - The icon of a suggestion from the `atomic-commerce-search-box-query-suggestions` component.
* @part query-suggestion-text - The text of a suggestion from the `atomic-commerce-search-box-query-suggestions` component.
*
* @part recent-query-item - A suggestion from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-content - The contents of a suggestion from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-icon - The icon of a suggestion from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-text - The text of a suggestion from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-text-highlight - The highlighted portion of the text of a suggestion from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-title-item - The clear button above suggestions from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-title-content - The contents of the clear button above suggestions from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-title - The "recent searches" text of the clear button above suggestions from the `atomic-commerce-search-box-recent-queries` component.
* @part recent-query-clear - The "clear" text of the clear button above suggestions from the `atomic-commerce-search-box-recent-queries` component.
*
* @part instant-results-item - An instant result rendered by an `atomic-commerce-search-box-instant-products` component.
* @part instant-results-show-all - The clickable suggestion to show all items for the current instant results search rendered by an `atomic-commerce-search-box-instant-products` component.
* @part instant-results-show-all-button - The button inside the clickable suggestion from the `atomic-commerce-search-box-instant-products` component.
*
* @alpha
*/
@Component({
tag: 'atomic-commerce-search-box',
styleUrl: 'atomic-commerce-search-box.pcss',
shadow: true,
})
export class AtomicCommerceSearchBox
implements InitializableComponent<Bindings>
{
@InitializeBindings() public bindings!: Bindings;
private searchBox!: SearchBox | StandaloneSearchBox;
private searchBoxSuggestionEventsQueue: CustomEvent<
SearchBoxSuggestionsEvent<SearchBox | StandaloneSearchBox>
>[] = [];
private id!: string;
private textAreaRef!: HTMLTextAreaElement;
private suggestionManager!: SuggestionManager<
SearchBox | StandaloneSearchBox
>;
@Element() private host!: HTMLElement;
@BindStateToController('searchBox')
@State()
private searchBoxState!: SearchBoxState | StandaloneSearchBoxState;
@State() public error!: Error;
@State() private isExpanded = false;
/**
* The amount of queries displayed when the user interacts with the search box.
* By default, a mix of query suggestions and recent queries will be shown.
* You can configure those settings using the following components as children:
* - atomic-commerce-search-box-query-suggestions
* - atomic-commerce-search-box-recent-queries
*/
@Prop({reflect: true}) public numberOfQueries = 8;
/**
* Defining this option makes the search box standalone (see [Use a
* Standalone Search Box](https://docs.coveo.com/en/atomic/latest/usage/ssb/)).
*
* This option defines the default URL the user should be redirected to, when a query is submitted.
* If a query pipeline redirect is triggered, it will redirect to that URL instead
* (see [query pipeline triggers](https://docs.coveo.com/en/1458)).
*/
@Prop({reflect: true}) public redirectionUrl?: string;
/**
* The timeout for suggestion queries, in milliseconds.
* If a suggestion query times out, the suggestions from that particular query won't be shown.
*/
@Prop() public suggestionTimeout = 400;
/**
* The delay for suggestion queries on input, in milliseconds.
*
* The suggestion request will be delayed until the end user stops typing for at least the specified amount of time.
*
* This delay is used to avoid sending too many requests to the Coveo Platform when the user is typing, as well as reducing potential input lag on low end devices.
* A higher delay will reduce input lag, at the cost of suggestions freshness.
*/
@Prop() public suggestionDelay = 0;
/**
* Whether to prevent the user from triggering searches and query suggestions from the component.
* Perfect for use cases where you need to disable the search conditionally.
* For the specific case when you need to disable the search based on the length of the query, refer to {@link minimumQueryLength}.
*/
@Prop({reflect: true}) public disableSearch = false;
/**
* The minimum query length required to enable search.
* For example, to disable the search for empty queries, set this to `1`.
*/
@Prop({reflect: true}) public minimumQueryLength = 0;
/**
* Whether to clear all active query filters when the end user submits a new query from the search box.
* Setting this option to "false" is not recommended & can lead to an increasing number of queries returning no results.
*/
@Prop({reflect: true}) public clearFilters = true;
/**
* Event that is emitted when a standalone search box redirection is triggered. By default, the search box will directly change the URL and redirect accordingly, so if you want to handle the redirection differently, use this event.
*
* Example:
* ```html
* <script>
* document.querySelector('atomic-commerce-search-box').addEventListener((e) => {
* e.preventDefault();
* // handle redirection
* });
* </script>
* ...
* <atomic-commerce-search-box redirection-url="/search"></atomic-commerce-search-box>
* ```
*/
@Event({
eventName: 'redirect',
})
public redirect!: EventEmitter<RedirectionPayload>;
@AriaLiveRegion('search-box')
protected searchBoxAriaMessage!: string;
@AriaLiveRegion('search-suggestions', true)
protected suggestionsAriaMessage!: string;
public disconnectedCallback = () => {};
private isStandaloneSearchBox(
searchBox: SearchBox | StandaloneSearchBox
): searchBox is StandaloneSearchBox {
return 'redirectTo' in searchBox;
}
public initialize() {
this.id ??= randomID('atomic-commerce-search-box-');
this.initializeSearchboxController();
this.initializeSuggestionManager();
}
private updateRedirectionUrl() {
if (this.isStandaloneSearchBox(this.searchBox) && this.redirectionUrl) {
this.searchBox.updateRedirectUrl(this.redirectionUrl);
} else {
this.registerNewSearchBoxController();
}
}
private registerNewSearchBoxController() {
this.disconnectedCallback();
this.initialize();
}
private initializeSearchboxController() {
this.searchBox = this.redirectionUrl
? buildStandaloneSearchBox(this.bindings.engine, {
options: {
...this.searchBoxOptions,
redirectionUrl: this.redirectionUrl,
overwrite: true,
},
})
: buildSearchBox(this.bindings.engine, {
options: this.searchBoxOptions,
});
}
public componentWillUpdate() {
if (
!('redirectTo' in this.searchBoxState) ||
!('afterRedirection' in this.searchBox)
) {
return;
}
const {redirectTo, value} = this.searchBoxState;
if (redirectTo === '') {
return;
}
const data: Omit<StandaloneSearchBoxData, 'analytics'> = {
value,
enableQuerySyntax: false,
};
const storage = new SafeStorage();
storage.setJSON(StorageItems.STANDALONE_SEARCH_BOX_DATA, data);
this.searchBox.afterRedirection();
const event = this.redirect.emit({redirectTo, value});
if (!event.defaultPrevented) {
window.location.href = redirectTo;
}
}
@Listen('atomic/searchBoxSuggestion/register')
public registerSuggestions(
event: CustomEvent<
SearchBoxSuggestionsEvent<SearchBox | StandaloneSearchBox>
>
) {
if (!this.bindings) {
this.searchBoxSuggestionEventsQueue.push(event);
} else {
this.suggestionManager.registerSuggestionsFromEvent(
event,
this.suggestionBindings
);
}
}
@Listen('atomic/selectChildProduct')
public onSelectChildProduct(event: CustomEvent<SelectChildProductEventArgs>) {
event.stopPropagation();
this.bindings.store.state.activeProductChild = event.detail.child;
this.suggestionManager.forceUpdate();
}
private registerSearchboxSuggestionEvents() {
this.searchBoxSuggestionEventsQueue.forEach((evt) => {
this.suggestionManager.registerSuggestionsFromEvent(
evt,
this.suggestionBindings
);
});
this.searchBoxSuggestionEventsQueue = [];
}
@Watch('redirectionUrl')
watchRedirectionUrl() {
this.updateRedirectionUrl();
}
private initializeSuggestionManager() {
if (this.suggestionManager) {
return;
}
this.suggestionManager = new SuggestionManager({
getNumberOfSuggestionsToDisplay: () => this.numberOfQueries,
updateQuery: (query) => this.searchBox.updateText(query),
getSearchBoxValue: () => this.searchBoxState.value,
getSuggestionTimeout: () => this.suggestionTimeout,
getSuggestionDelay: () => this.suggestionDelay,
getHost: () => this.host,
getLogger: () => this.bindings.engine.logger,
});
this.suggestionManager.initializeSuggestions(this.suggestionBindings);
}
private get suggestionBindings(): SearchBoxSuggestionsBindings<
SearchBox | StandaloneSearchBox
> {
return spreadProperties(
this.bindings,
this.suggestionManager.partialSuggestionBindings,
this.partialSuggestionBindings
);
}
private get partialSuggestionBindings(): Pick<
SearchBoxSuggestionsBindings<SearchBox | StandaloneSearchBox>,
| 'id'
| 'isStandalone'
| 'searchBoxController'
| 'numberOfQueries'
| 'clearFilters'
> {
return Object.defineProperties(
{...this.bindings},
{
id: {
get: () => this.id,
enumerable: true,
},
searchBoxController: {
get: () => this.searchBox,
enumerable: true,
},
isStandalone: {
get: () => !!this.redirectionUrl,
enumerable: true,
},
numberOfQueries: {
get: () => this.numberOfQueries,
enumerable: true,
},
clearFilters: {
get: () => this.clearFilters,
enumerable: true,
},
}
) as unknown as Pick<
SearchBoxSuggestionsBindings<SearchBox | StandaloneSearchBox>,
| 'id'
| 'isStandalone'
| 'searchBoxController'
| 'numberOfQueries'
| 'clearFilters'
>;
}
private get searchBoxOptions(): SearchBoxOptions {
return {
id: this.id,
highlightOptions: {
notMatchDelimiters: {
open: '<span class="font-bold">',
close: '</span>',
},
correctionDelimiters: {
open: '<span class="font-normal">',
close: '</span>',
},
},
clearFilters: this.clearFilters,
};
}
private updateBreakpoints = once(() => updateBreakpoints(this.host));
private async onInput(value: string) {
this.updateQueryWithoutQuerySuggestionTrigger(value);
if (this.isSearchDisabledForEndUser(value)) {
this.suggestionManager.clearSuggestions();
return;
}
this.isExpanded = true;
await this.suggestionManager.triggerSuggestions();
this.announceNewSuggestionsToScreenReader();
}
private async onFocus() {
if (this.isExpanded) {
return;
}
if (this.isSearchDisabledForEndUser(this.searchBoxState.value)) {
return;
}
this.isExpanded = true;
await this.suggestionManager.triggerSuggestions();
this.announceNewSuggestionsToScreenReader();
}
private onSubmit() {
this.isExpanded = false;
if (this.suggestionManager.keyboardActiveDescendant) {
this.suggestionManager.onSubmit();
return;
}
this.searchBox.submit();
}
private onKeyDown(e: KeyboardEvent) {
if (this.isSearchDisabledForEndUser(this.searchBoxState.value)) {
return;
}
switch (e.key) {
case 'Enter':
this.onSubmit();
break;
case 'Escape':
this.suggestionManager.clearSuggestions();
break;
case 'ArrowDown':
e.preventDefault();
this.suggestionManager.focusNextValue();
this.announceNewActiveSuggestionToScreenReader();
break;
case 'ArrowUp':
e.preventDefault();
this.suggestionManager.focusPreviousValue();
this.announceNewActiveSuggestionToScreenReader();
break;
case 'ArrowRight':
if (
this.suggestionManager.hasActiveDescendant ||
!this.searchBox.state.value
) {
e.preventDefault();
this.suggestionManager.focusPanel('right');
this.announceNewActiveSuggestionToScreenReader();
}
break;
case 'ArrowLeft':
if (
this.suggestionManager.hasActiveDescendant ||
!this.searchBox.state.value
) {
e.preventDefault();
this.suggestionManager.focusPanel('left');
this.announceNewActiveSuggestionToScreenReader();
}
break;
case 'Tab':
this.suggestionManager.clearSuggestions();
break;
}
}
private triggerTextAreaChange(value: string) {
this.textAreaRef.value = value;
this.textAreaRef.dispatchEvent(new window.Event('change'));
}
private renderSuggestion(
item: SearchBoxSuggestionElement,
index: number,
lastIndex: number,
side: 'left' | 'right'
) {
const id = `${this.id}-${side}-suggestion-${item.key}`;
const isSelected = id === this.suggestionManager.activeDescendant;
if (index === lastIndex && item.hideIfLast) {
return null;
}
const isButton = item.onSelect || item.query;
if (!isButton) {
return (
<SimpleSearchSuggestion
bindings={this.bindings}
id={id}
suggestion={item}
isSelected={isSelected}
side={side}
index={index}
lastIndex={lastIndex}
isDoubleList={this.suggestionManager.isDoubleList}
></SimpleSearchSuggestion>
);
}
return (
<ButtonSearchSuggestion
bindings={this.bindings}
id={id}
suggestion={item}
isSelected={isSelected}
side={side}
index={index}
lastIndex={lastIndex}
isDoubleList={this.suggestionManager.isDoubleList}
onClick={(e: Event) => {
this.suggestionManager.onSuggestionClick(item, e);
this.isExpanded = false;
this.triggerTextAreaChange(item.query ?? '');
}}
onMouseOver={() => {
this.suggestionManager.onSuggestionMouseOver(item, side, id);
}}
></ButtonSearchSuggestion>
);
}
private renderPanel(
side: 'left' | 'right',
elements: SearchBoxSuggestionElement[],
setRef: (el: HTMLElement | undefined) => void,
getRef: () => HTMLElement | undefined
) {
if (!elements.length) {
return null;
}
return (
<div
part={`suggestions suggestions-${side}`}
ref={setRef}
class="flex flex-grow basis-1/2 flex-col"
onMouseDown={(e) => {
if (e.target === getRef()) {
e.preventDefault();
}
}}
>
{elements.map((suggestion, index) =>
this.renderSuggestion(suggestion, index, elements.length - 1, side)
)}
</div>
);
}
private renderSuggestions() {
if (!this.suggestionManager.hasSuggestions) {
return null;
}
return (
<div
id={`${this.id}-popup`}
part={`suggestions-wrapper ${
this.suggestionManager.isDoubleList
? 'suggestions-double-list'
: 'suggestions-single-list'
}`}
class={`bg-background border-neutral absolute left-0 top-full z-10 flex w-full rounded-md border ${
this.suggestionManager.hasSuggestions &&
this.isExpanded &&
!this.isSearchDisabledForEndUser(this.searchBoxState.value)
? ''
: 'hidden'
}`}
role="application"
aria-label={this.bindings.i18n.t(
this.suggestionManager.isDoubleList
? 'search-suggestions-double-list'
: 'search-suggestions-single-list'
)}
aria-activedescendant={this.suggestionManager.activeDescendant}
>
{this.renderPanel(
'left',
this.suggestionManager.leftSuggestionElements,
(el) => (this.suggestionManager.leftPanel = el),
() => this.suggestionManager.leftPanel
)}
{this.renderPanel(
'right',
this.suggestionManager.rightSuggestionElements,
(el) => (this.suggestionManager.rightPanel = el),
() => this.suggestionManager.rightPanel
)}
</div>
);
}
private renderTextBox = (searchLabel: string) => {
const props = {
loading: this.searchBoxState.isLoading,
bindings: this.bindings,
value: this.searchBoxState.value,
title: searchLabel,
ariaLabel: searchLabel,
onFocus: () => this.onFocus(),
onInput: (e: Event) =>
this.onInput((e.target as HTMLTextAreaElement).value),
onKeyDown: (e: KeyboardEvent) => this.onKeyDown(e),
onClear: () => this.searchBox.clear(),
popup: {
id: `${this.id}-popup`,
activeDescendant: this.suggestionManager.activeDescendant,
expanded: this.isExpanded,
hasSuggestions: this.suggestionManager.hasSuggestions,
},
};
return (
<SearchTextArea
textAreaRef={this.textAreaRef}
ref={(el) => (this.textAreaRef = el as HTMLTextAreaElement)}
{...props}
onClear={() => {
props.onClear();
this.triggerTextAreaChange('');
}}
/>
);
};
private renderAbsolutePositionSpacer() {
return (
<textarea
aria-hidden
part="textarea-spacer"
class="invisible w-full px-4 py-3.5 text-lg"
rows={1}
></textarea>
);
}
private isSearchDisabledForEndUser(queryValue?: string) {
if (isNullOrUndefined(queryValue)) {
return this.disableSearch;
}
if (queryValue.trim().length < this.minimumQueryLength) {
return true;
}
return this.disableSearch;
}
private getSearchInputLabel(minimumQueryLength = 0) {
if (this.isSearchDisabledForEndUser(this.searchBoxState.value)) {
return this.bindings.i18n.t('search-disabled', {
length: minimumQueryLength,
});
}
if (isMacOS()) {
return this.bindings.i18n.t('search-box-with-suggestions-macos');
}
if (!hasKeyboard()) {
return this.bindings.i18n.t('search-box-with-suggestions-keyboardless');
}
return this.bindings.i18n.t('search-box-with-suggestions');
}
private announceNewActiveSuggestionToScreenReader() {
const ariaLabel = this.suggestionManager.activeDescendantElement?.ariaLabel;
if (isMacOS() && ariaLabel) {
this.suggestionsAriaMessage = ariaLabel;
}
}
private announceNewSuggestionsToScreenReader() {
const elsLength =
this.suggestionManager.allSuggestionElements.filter(
elementHasQuery
).length;
this.searchBoxAriaMessage = elsLength
? this.bindings.i18n.t('query-suggestions-available', {
count: elsLength,
})
: this.bindings.i18n.t('query-suggestions-unavailable');
}
private updateQueryWithoutQuerySuggestionTrigger(query: string) {
const {engine} = this.bindings;
engine.dispatch(
loadQuerySetActions(engine).updateQuerySetQuery({
id: this.id,
query,
})
);
}
public render() {
this.updateBreakpoints();
const searchLabel = this.getSearchInputLabel(this.minimumQueryLength);
const Submit = SubmitButton;
const isDisabled = this.isSearchDisabledForEndUser(
this.searchBoxState.value
);
if (!this.suggestionManager.suggestions.length) {
this.registerSearchboxSuggestionEvents();
}
return (
<Host>
{this.renderAbsolutePositionSpacer()}
{[
<SearchBoxWrapper
disabled={isDisabled}
onFocusout={(event) => {
if (!isFocusingOut(event)) {
return;
}
this.suggestionManager.clearSuggestions();
this.isExpanded = false;
}}
>
{this.renderTextBox(searchLabel)}
<Submit
bindings={this.bindings}
disabled={isDisabled}
onClick={() => {
this.searchBox.submit();
this.suggestionManager.clearSuggestions();
}}
title={searchLabel}
/>
{this.renderSuggestions()}
</SearchBoxWrapper>,
!this.suggestionManager.suggestions.length && (
<slot>
<atomic-commerce-search-box-recent-queries></atomic-commerce-search-box-recent-queries>
<atomic-commerce-search-box-query-suggestions></atomic-commerce-search-box-query-suggestions>
</slot>
),
]}
</Host>
);
}
}