Skip to content

Commit

Permalink
feat(autocomplete): Add opening and closing upon interaction
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 471008353
  • Loading branch information
EstebanG23 authored and copybara-github committed Aug 30, 2022
1 parent c289678 commit 530b6d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion autocomplete/filled-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ declare global {
export class MdFilledAutocomplete extends Autocomplete {
protected override readonly listTag = literal`md-list`;
protected override readonly menuSurfaceTag = literal`md-menu-surface`;
protected override readonly textFieldTag = literal`md-filled-field`;
protected override readonly textFieldTag = literal`md-filled-text-field`;
}
42 changes: 41 additions & 1 deletion autocomplete/lib/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export abstract class Autocomplete extends LitElement {

/** @soyTemplate */
override render(): TemplateResult {
return html`<div class="md3-autocomplete">
return html`<div class="md3-autocomplete"
@click=${this.handleClick}
@focusout=${this.handleFocusout}>
${this.renderTextField()}
${this.renderMenuSurface()}</div>`;
}
Expand Down Expand Up @@ -80,8 +82,46 @@ export abstract class Autocomplete extends LitElement {
return staticHtml`<${this.menuSurfaceTag}
class="md3-autocomplete__menu-surface"
.corner=${'BOTTOM_START'}
?stayOpenOnBodyClick=${true}
>
<${this.listTag}><slot></slot></${this.listTag}>
</${this.menuSurfaceTag}>`;
}

isOpen() {
return this.menuSurface?.open || false;
}

open() {
this.menuSurface?.show();
// TODO(b/242594859): Add once supported by textfield
// this.textField.ariaExpanded = true;
}

close() {
this.menuSurface?.close();
// TODO(b/242594859): Add once supported by textfield
// this.textField.ariaExpanded = false;
// this.setActiveDescendant();
}

protected handleClick(event: PointerEvent) {
// When clicking the list (not items nor text field) the menu should stay
// open.
if (this.isOpen() &&
(event.target as Node)?.parentNode !== this.menuSurface) {
this.close();
} else {
this.open();
}
}

// TODO(b/243389569): Revisit focus control when extending textfield
protected handleFocusout() {
if (this.matches(':focus-within')) {
this.textField?.focus();
return;
}
this.close();
}
}

0 comments on commit 530b6d3

Please sign in to comment.