diff --git a/labs/behaviors/on-report-validity.ts b/labs/behaviors/on-report-validity.ts index 01834e82a8..71f0401261 100644 --- a/labs/behaviors/on-report-validity.ts +++ b/labs/behaviors/on-report-validity.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import {LitElement} from 'lit'; +import {LitElement, isServer} from 'lit'; import {ConstraintValidation} from './constraint-validation.js'; import {MixinBase, MixinReturn} from './mixin.js'; @@ -46,6 +46,7 @@ export const onReportValidity = Symbol('onReportValidity'); // Private symbol members, used to avoid name clashing. const privateCleanupFormListeners = Symbol('privateCleanupFormListeners'); +const privateDoNotReportInvalid = Symbol('privateDoNotReportInvalid'); /** * Mixes in a callback for constraint validation when validity should be @@ -91,23 +92,67 @@ export function mixinOnReportValidity< */ [privateCleanupFormListeners] = new AbortController(); - override reportValidity() { - let invalidEvent = null as Event | null; - const cleanupInvalidListener = new AbortController(); + /** + * Used to determine if an invalid event should report validity. Invalid + * events from `checkValidity()` do not trigger reporting. + */ + [privateDoNotReportInvalid] = false; + + // Mixins must have a constructor with `...args: any[]` + // tslint:disable-next-line:no-any + constructor(...args: any[]) { + super(...args); + if (isServer) { + return; + } + this.addEventListener( 'invalid', - (event) => { - invalidEvent = event; + (invalidEvent) => { + // Listen for invalid events dispatched by a `