Skip to content

Commit e9f2943

Browse files
authored
Don't automatically select 0th autocomplete candidate (#1753)
* Don't automatically select 0th autocomplete candidate This is a port of GoogleChrome/chromium-dashboard#5346 * Update frontend/src/static/js/components/webstatus-typeahead.ts
1 parent 2782c43 commit e9f2943

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

frontend/src/static/js/components/webstatus-typeahead.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
type TemplateResult,
2121
css,
2222
html,
23+
nothing,
2324
} from 'lit';
2425
import {customElement, property, state} from 'lit/decorators.js';
2526
import {SHARED_STYLES} from '../css/shared-css.js';
@@ -289,6 +290,7 @@ export class WebstatusTypeahead extends LitElement {
289290
@click=${(e: Event) => e.preventDefault()}
290291
@sl-select=${this.handleCandidateSelected}
291292
>
293+
<webstatus-typeahead-invisible-item></webstatus-typeahead-invisible-item>
292294
${this.candidates.map(
293295
c => html`
294296
<webstatus-typeahead-item
@@ -318,7 +320,11 @@ export class WebstatusTypeahead extends LitElement {
318320
@customElement('webstatus-typeahead-dropdown')
319321
export class WebstatusTypeaheadDropdown extends SlDropdown {
320322
getCurrentItem(): SlMenuItem | undefined {
321-
return this.getMenu()!.getCurrentItem();
323+
const item = this.getMenu()!.getCurrentItem();
324+
if (!item || item instanceof WebstatusTypeaheadInvisibleItem) {
325+
return undefined;
326+
}
327+
return item;
322328
}
323329

324330
setCurrentItem(newCurrentItem: SlMenuItem) {
@@ -327,6 +333,13 @@ export class WebstatusTypeaheadDropdown extends SlDropdown {
327333
newCurrentItem.scrollIntoView({block: 'nearest', behavior: 'smooth'});
328334
}
329335

336+
getAllVisibleItems(menu: SlMenu): SlMenuItem[] {
337+
const items = menu.getAllItems();
338+
return items.filter(
339+
item => !(item instanceof WebstatusTypeaheadInvisibleItem),
340+
);
341+
}
342+
330343
resetSelection() {
331344
const currentItem = this.getCurrentItem();
332345
currentItem?.setAttribute('tabindex', '-1');
@@ -337,7 +350,7 @@ export class WebstatusTypeaheadDropdown extends SlDropdown {
337350
if (!menu) {
338351
return;
339352
}
340-
const menuItems = menu.getAllItems();
353+
const menuItems = this.getAllVisibleItems(menu);
341354
if (menuItems.length === 0) {
342355
return;
343356
}
@@ -480,3 +493,10 @@ export class WebstatusTypeaheadItem extends LitElement {
480493
`;
481494
}
482495
}
496+
497+
@customElement('webstatus-typeahead-invisible-item')
498+
export class WebstatusTypeaheadInvisibleItem extends WebstatusTypeaheadItem {
499+
override render(): TemplateResult {
500+
return html`${nothing}`;
501+
}
502+
}

0 commit comments

Comments
 (0)