From 263b785a7cfca4a871057dacfada747980a29ee8 Mon Sep 17 00:00:00 2001 From: smikhalevski Date: Sat, 11 Nov 2023 19:22:15 +0300 Subject: [PATCH] Removed setElementValueAccessor; Fixed check constraints on value update --- .../src/main/constraintValidationPlugin.ts | 9 +++--- .../src/main/createElementValueAccessor.ts | 4 +-- .../src/main/uncontrolledPlugin.ts | 29 +++++-------------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/packages/constraint-validation-plugin/src/main/constraintValidationPlugin.ts b/packages/constraint-validation-plugin/src/main/constraintValidationPlugin.ts index 760b6d0..2e159d2 100644 --- a/packages/constraint-validation-plugin/src/main/constraintValidationPlugin.ts +++ b/packages/constraint-validation-plugin/src/main/constraintValidationPlugin.ts @@ -123,12 +123,14 @@ export function constraintValidationPlugin(): PluginInjector field.errorCount !== 0 }, }); - const changeListener: EventListener = event => { - if (field.element === event.currentTarget && isValidatable(field.element)) { + const changeListener = () => { + if (isValidatable(field.element)) { dispatchEvents(setError(field, field.element.validationMessage, 1, [])); } }; + field.on('change:value', changeListener); + const { ref } = field; field.ref = element => { @@ -138,7 +140,6 @@ export function constraintValidationPlugin(): PluginInjector { const events: Event[] = []; const { observedElements } = field; - const [element] = observedElements; for (const mutation of mutations) { for (let i = 0; i < mutation.removedNodes.length; ++i) { @@ -98,7 +90,7 @@ export function uncontrolledPlugin(accessor = elementValueAccessor): PluginInjec if (observedElements.length === 0) { mutationObserver.disconnect(); field.ref?.(null); - } else if (element !== observedElements[0]) { + } else { field.ref?.(observedElements[0]); } @@ -125,33 +117,28 @@ export function uncontrolledPlugin(accessor = elementValueAccessor): PluginInjec const { observedElements } = field; if ( - element === null || !(element instanceof Element) || !element.isConnected || - observedElements.indexOf(element) !== -1 + element.parentNode === null || + observedElements.includes(element) ) { return; } - mutationObserver.observe(element.parentNode!, { childList: true }); + mutationObserver.observe(element.parentNode, { childList: true }); element.addEventListener('input', changeListener); element.addEventListener('change', changeListener); - observedElements.push(element); + const elementCount = observedElements.push(element); field.elementValueAccessor.set(observedElements, field.value); - if (observedElements.length === 1) { - field.ref?.(observedElements[0]); + if (elementCount === 1) { + field.ref?.(element); } dispatchEvents([createEvent(EVENT_CHANGE_OBSERVED_ELEMENTS, field, element)]); }; - - field.setElementValueAccessor = accessor => { - field.elementValueAccessor = accessor; - return field; - }; }; }