Skip to content

Commit

Permalink
fix(textfield): broken required validity on Safari
Browse files Browse the repository at this point in the history
Fixes #4796

PiperOrigin-RevId: 564508324
  • Loading branch information
asyncLiz authored and copybara-github committed Sep 12, 2023
1 parent 61c8f6d commit 89b30bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion checkbox/internal/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export class Checkbox extends LitElement {
@state() private prevDisabled = false;
@state() private prevIndeterminate = false;
@query('input') private readonly input!: HTMLInputElement|null;
// Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432
// Replace with this.internals.validity.customError when resolved.
private hasCustomValidityError = false;
private readonly internals =
(this as HTMLElement /* needed for closure */).attachInternals();

Expand Down Expand Up @@ -188,6 +191,7 @@ export class Checkbox extends LitElement {
* @param error The error message to display.
*/
setCustomValidity(error: string) {
this.hasCustomValidityError = !!error;
this.internals.setValidity({customError: !!error}, error, this.getInput());
}

Expand Down Expand Up @@ -273,7 +277,7 @@ export class Checkbox extends LitElement {
// Sync the internal <input>'s validity and the host's ElementInternals
// validity. We do this to re-use native `<input>` validation messages.
const input = this.getInput();
if (this.internals.validity.customError) {
if (this.hasCustomValidityError) {
input.setCustomValidity(this.internals.validationMessage);
} else {
input.setCustomValidity('');
Expand Down
6 changes: 5 additions & 1 deletion switch/internal/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export class Switch extends LitElement {
}

@query('input') private readonly input!: HTMLInputElement|null;
// Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432
// Replace with this.internals.validity.customError when resolved.
private hasCustomValidityError = false;
private readonly internals =
(this as HTMLElement /* needed for closure */).attachInternals();

Expand Down Expand Up @@ -190,6 +193,7 @@ export class Switch extends LitElement {
* @param error The error message to display.
*/
setCustomValidity(error: string) {
this.hasCustomValidityError = !!error;
this.internals.setValidity({customError: !!error}, error, this.getInput());
}

Expand Down Expand Up @@ -303,7 +307,7 @@ export class Switch extends LitElement {
// Sync the internal <input>'s validity and the host's ElementInternals
// validity. We do this to re-use native `<input>` validation messages.
const input = this.getInput();
if (this.internals.validity.customError) {
if (this.hasCustomValidityError) {
input.setCustomValidity(this.internals.validationMessage);
} else {
input.setCustomValidity('');
Expand Down
6 changes: 5 additions & 1 deletion textfield/internal/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ export abstract class TextField extends LitElement {
private readonly leadingIcons!: Element[];
@queryAssignedElements({slot: 'trailing-icon'})
private readonly trailingIcons!: Element[];
// Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432
// Replace with this.internals.validity.customError when resolved.
private hasCustomValidityError = false;
private readonly internals =
(this as HTMLElement /* needed for closure */).attachInternals();

Expand Down Expand Up @@ -426,6 +429,7 @@ export abstract class TextField extends LitElement {
* @param error The error message to display.
*/
setCustomValidity(error: string) {
this.hasCustomValidityError = !!error;
this.internals.setValidity(
{customError: !!error}, error, this.getInputOrTextarea());
}
Expand Down Expand Up @@ -750,7 +754,7 @@ export abstract class TextField extends LitElement {
// Sync the internal <input>'s validity and the host's ElementInternals
// validity. We do this to re-use native `<input>` validation messages.
const input = this.getInputOrTextarea();
if (this.internals.validity.customError) {
if (this.hasCustomValidityError) {
input.setCustomValidity(this.internals.validationMessage);
} else {
input.setCustomValidity('');
Expand Down

0 comments on commit 89b30bd

Please sign in to comment.