From b281c5bbc1951ff98129c807c2c5352bdb119583 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 7 Feb 2023 13:56:02 -0500 Subject: [PATCH] use Set instead of WeakMap --- src/internal/form.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/internal/form.ts b/src/internal/form.ts index 81123cd8..14b795db 100644 --- a/src/internal/form.ts +++ b/src/internal/form.ts @@ -9,18 +9,18 @@ import type SlButton from '../components/button/button'; // export const formCollections: WeakMap> = new WeakMap(); -// -// We store a WeakMap of controls that users have interacted with. This allows us to determine the interaction state -// without littering the DOM with additional data attributes. -// -const userInteractedControls: WeakMap = new WeakMap(); - // // We store a WeakMap of reportValidity() overloads so we can override it when form controls connect to the DOM and // restore the original behavior when they disconnect. // const reportValidityOverloads: WeakMap boolean> = new WeakMap(); +// +// We store a Set of controls that users have interacted with. This allows us to determine the interaction state +// without littering the DOM with additional data attributes. +// +const userInteractedControls: Set = new Set(); + export interface FormControlControllerOptions { /** A function that returns the form containing the form control. */ form: (input: ShoelaceFormControl) => HTMLFormElement | null; @@ -234,7 +234,12 @@ export class FormControlController implements ReactiveController { } private setUserInteracted(el: ShoelaceFormControl, hasInteracted: boolean) { - userInteractedControls.set(el, hasInteracted); + if (hasInteracted) { + userInteractedControls.add(el); + } else { + userInteractedControls.delete(el); + } + el.requestUpdate(); } @@ -290,7 +295,7 @@ export class FormControlController implements ReactiveController { */ setValidity(isValid: boolean) { const host = this.host; - const hasInteracted = Boolean(userInteractedControls.get(host)); + const hasInteracted = Boolean(userInteractedControls.has(host)); const required = Boolean(host.required); //