Skip to content

Commit

Permalink
fix(select): focus() delegates properly, focus on reporting validity
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586815117
  • Loading branch information
asyncLiz authored and copybara-github committed Dec 1, 2023
1 parent c53a419 commit 897d977
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions select/internal/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export abstract class Select extends selectBaseClass {
requestUpdateOnAriaChange(Select);
}

/** @nocollapse */
static override shadowRootOptions = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
};

/**
* Opens the menu synchronously with no animation.
*/
Expand Down Expand Up @@ -235,6 +241,16 @@ export abstract class Select extends selectBaseClass {
@queryAssignedElements({slot: 'leading-icon', flatten: true})
private readonly leadingIcons!: Element[];

constructor() {
super();
if (isServer) {
return;
}

this.addEventListener('focus', this.handleFocus.bind(this));
this.addEventListener('blur', this.handleBlur.bind(this));
}

/**
* Selects an option given the value of the option, and updates MdSelect's
* value.
Expand Down Expand Up @@ -277,7 +293,13 @@ export abstract class Select extends selectBaseClass {
return;
}

invalidEvent?.preventDefault();
if (invalidEvent) {
// Prevent default pop-up behavior. This also prevents focusing, so we
// manually focus.
invalidEvent.preventDefault();
this.focus();
}

const prevMessage = this.getErrorText();
this.nativeError = !!invalidEvent;
this.nativeErrorText = this.validationMessage;
Expand Down Expand Up @@ -362,9 +384,7 @@ export abstract class Select extends selectBaseClass {
supporting-text=${this.supportingText}
error-text=${this.getErrorText()}
@keydown=${this.handleKeydown}
@click=${this.handleClick}
@focus=${this.handleFocus}
@blur=${this.handleBlur}>
@click=${this.handleClick}>
${this.renderFieldContent()}
<div id="description" slot="aria-describedby"></div>
</${this.fieldTag}>`;
Expand Down

0 comments on commit 897d977

Please sign in to comment.