Skip to content

Commit

Permalink
fix(textfield): focus style lost after reportValidity() during change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597047043
  • Loading branch information
asyncLiz authored and copybara-github committed Jan 9, 2024
1 parent cef1b74 commit 6efc904
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
5 changes: 0 additions & 5 deletions textfield/demo/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ const forms: MaterialStoryInit<StoryKnobs> = {

async function reportValidity(event: Event) {
const textField = event.target as MdFilledTextField;
// Calling reportValidity() will focus the text field. Since we do this on
// "change" (blur), wait for other focus changes to finish, like tabbing.
await new Promise<void>((resolve) => {
setTimeout(resolve);
});
textField.reportValidity();
}

Expand Down
20 changes: 10 additions & 10 deletions textfield/internal/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ export abstract class TextField extends textFieldBaseClass {
cols=${this.cols}
.value=${live(this.value)}
@change=${this.redispatchEvent}
@focusin=${this.handleFocusin}
@focusout=${this.handleFocusout}
@focus=${this.handleFocusChange}
@blur=${this.handleFocusChange}
@input=${this.handleInput}
@select=${this.redispatchEvent}></textarea>
`;
Expand Down Expand Up @@ -653,8 +653,8 @@ export abstract class TextField extends textFieldBaseClass {
type=${this.type}
.value=${live(this.value)}
@change=${this.redispatchEvent}
@focusin=${this.handleFocusin}
@focusout=${this.handleFocusout}
@focus=${this.handleFocusChange}
@blur=${this.handleFocusChange}
@input=${this.handleInput}
@select=${this.redispatchEvent} />
${suffix}
Expand Down Expand Up @@ -687,12 +687,12 @@ export abstract class TextField extends textFieldBaseClass {
return this.error ? this.errorText : this.nativeErrorText;
}

private handleFocusin() {
this.focused = true;
}

private handleFocusout() {
this.focused = false;
private handleFocusChange() {
// When calling focus() or reportValidity() during change, it's possible
// for blur to be called after the new focus event. Rather than set
// `this.focused` to true/false on focus/blur, we always set it to whether
// or not the input itself is focused.
this.focused = this.inputOrTextarea?.matches(':focus') ?? false;
}

private handleInput(event: InputEvent) {
Expand Down

0 comments on commit 6efc904

Please sign in to comment.