react-hook-form
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -151,8 +151,2 @@ import { useRef, useState, useEffect } from 'react'; | ||
function detectRegistered(fields, { ref: { type, name, value } }) { | ||
return !!(isRadioInput(type) | ||
? Object.values(fields).find(({ ref: { name: localName }, options }) => name === localName && options ? !!options.find(option => option.ref.value === value) : false) | ||
: fields[name]); | ||
} | ||
function onDomRemove(element, onDetachCallback) { | ||
@@ -181,25 +175,32 @@ const observer = new MutationObserver(() => { | ||
function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate }) { | ||
function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate, }) { | ||
const field = allFields[name]; | ||
if (!field) | ||
return; | ||
const isOnChange = mode === 'onChange' || watchFields.current[ref.name]; | ||
const isOnBlur = mode === 'onBlur'; | ||
if (isOnChange || isOnBlur) { | ||
if (isRadioInput(type)) { | ||
const options = field.options; | ||
if (!field || (!isOnChange && !isOnBlur)) | ||
return; | ||
const isRadio = isRadioInput(type); | ||
if (isRadio) { | ||
const options = field.options; | ||
const eventAttached = options[radioOptionIndex].eventAttached; | ||
if (eventAttached) { | ||
removeAllEventListeners(ref, validateWithStateUpdate); | ||
} | ||
else { | ||
options[radioOptionIndex].ref.addEventListener(isOnChange ? 'change' : 'blur', validateWithStateUpdate); | ||
options[radioOptionIndex].eventAttached = true; | ||
} | ||
options[radioOptionIndex].eventAttached = !eventAttached; | ||
} | ||
else { | ||
const eventAttached = field.eventAttached; | ||
if (eventAttached) { | ||
removeAllEventListeners(ref, validateWithStateUpdate); | ||
} | ||
else { | ||
ref.addEventListener(isOnChange ? 'input' : 'blur', validateWithStateUpdate); | ||
field.eventAttached = true; | ||
} | ||
field.eventAttached = !eventAttached; | ||
} | ||
} | ||
function getOptionNonEventAttached(options, type, value) { | ||
return options.findIndex(({ ref, eventAttached }) => value === ref.value && !eventAttached); | ||
} | ||
function useForm({ mode } = { mode: 'onSubmit' }) { | ||
@@ -242,4 +243,2 @@ const fields = useRef({}); | ||
const allFields = fields.current; | ||
if (detectRegistered(allFields, data)) | ||
return; | ||
if (isRadioInput(type)) { | ||
@@ -249,13 +248,16 @@ if (!allFields[name]) { | ||
} | ||
// detect if contains | ||
allFields[name].options.push(Object.assign({}, data, { mutationWatcher: onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)) })); | ||
radioOptionIndex = getOptionNonEventAttached(allFields[name].options, type, value); | ||
if (radioOptionIndex < 0) | ||
return; | ||
const options = allFields[name].options || []; | ||
radioOptionIndex = options.findIndex(({ ref }) => value === ref.value); | ||
if (radioOptionIndex > -1) { | ||
options[radioOptionIndex] = Object.assign({}, options[radioOptionIndex], data); | ||
} | ||
else { | ||
options.push(Object.assign({}, data, { mutationWatcher: onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)) })); | ||
} | ||
} | ||
else { | ||
allFields[name] = data; | ||
allFields[name].mutationWatcher = onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)); | ||
if (allFields[name].eventAttached) | ||
return; | ||
if (!allFields[name]) { | ||
allFields[name].mutationWatcher = onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)); | ||
} | ||
} | ||
@@ -278,2 +280,4 @@ attachEventListeners({ allFields, watchFields, ref, type, radioOptionIndex, name, mode, validateWithStateUpdate }); | ||
Object.values(fields.current).forEach(({ ref }) => { | ||
if (!ref) | ||
return; | ||
if (!watchFields.current[ref.name]) | ||
@@ -280,0 +284,0 @@ return; |
@@ -153,8 +153,2 @@ 'use strict'; | ||
function detectRegistered(fields, { ref: { type, name, value } }) { | ||
return !!(isRadioInput(type) | ||
? Object.values(fields).find(({ ref: { name: localName }, options }) => name === localName && options ? !!options.find(option => option.ref.value === value) : false) | ||
: fields[name]); | ||
} | ||
function onDomRemove(element, onDetachCallback) { | ||
@@ -183,25 +177,32 @@ const observer = new MutationObserver(() => { | ||
function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate }) { | ||
function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate, }) { | ||
const field = allFields[name]; | ||
if (!field) | ||
return; | ||
const isOnChange = mode === 'onChange' || watchFields.current[ref.name]; | ||
const isOnBlur = mode === 'onBlur'; | ||
if (isOnChange || isOnBlur) { | ||
if (isRadioInput(type)) { | ||
const options = field.options; | ||
if (!field || (!isOnChange && !isOnBlur)) | ||
return; | ||
const isRadio = isRadioInput(type); | ||
if (isRadio) { | ||
const options = field.options; | ||
const eventAttached = options[radioOptionIndex].eventAttached; | ||
if (eventAttached) { | ||
removeAllEventListeners(ref, validateWithStateUpdate); | ||
} | ||
else { | ||
options[radioOptionIndex].ref.addEventListener(isOnChange ? 'change' : 'blur', validateWithStateUpdate); | ||
options[radioOptionIndex].eventAttached = true; | ||
} | ||
options[radioOptionIndex].eventAttached = !eventAttached; | ||
} | ||
else { | ||
const eventAttached = field.eventAttached; | ||
if (eventAttached) { | ||
removeAllEventListeners(ref, validateWithStateUpdate); | ||
} | ||
else { | ||
ref.addEventListener(isOnChange ? 'input' : 'blur', validateWithStateUpdate); | ||
field.eventAttached = true; | ||
} | ||
field.eventAttached = !eventAttached; | ||
} | ||
} | ||
function getOptionNonEventAttached(options, type, value) { | ||
return options.findIndex(({ ref, eventAttached }) => value === ref.value && !eventAttached); | ||
} | ||
function useForm({ mode } = { mode: 'onSubmit' }) { | ||
@@ -244,4 +245,2 @@ const fields = react.useRef({}); | ||
const allFields = fields.current; | ||
if (detectRegistered(allFields, data)) | ||
return; | ||
if (isRadioInput(type)) { | ||
@@ -251,13 +250,16 @@ if (!allFields[name]) { | ||
} | ||
// detect if contains | ||
allFields[name].options.push(Object.assign({}, data, { mutationWatcher: onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)) })); | ||
radioOptionIndex = getOptionNonEventAttached(allFields[name].options, type, value); | ||
if (radioOptionIndex < 0) | ||
return; | ||
const options = allFields[name].options || []; | ||
radioOptionIndex = options.findIndex(({ ref }) => value === ref.value); | ||
if (radioOptionIndex > -1) { | ||
options[radioOptionIndex] = Object.assign({}, options[radioOptionIndex], data); | ||
} | ||
else { | ||
options.push(Object.assign({}, data, { mutationWatcher: onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)) })); | ||
} | ||
} | ||
else { | ||
allFields[name] = data; | ||
allFields[name].mutationWatcher = onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)); | ||
if (allFields[name].eventAttached) | ||
return; | ||
if (!allFields[name]) { | ||
allFields[name].mutationWatcher = onDomRemove(ref, () => removeReferenceAndEventListeners(data, true)); | ||
} | ||
} | ||
@@ -280,2 +282,4 @@ attachEventListeners({ allFields, watchFields, ref, type, radioOptionIndex, name, mode, validateWithStateUpdate }); | ||
Object.values(fields.current).forEach(({ ref }) => { | ||
if (!ref) | ||
return; | ||
if (!watchFields.current[ref.name]) | ||
@@ -282,0 +286,0 @@ return; |
@@ -1,2 +0,2 @@ | ||
export default function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate }: { | ||
export default function attachEventListeners({ mode, allFields, watchFields, radioOptionIndex, ref, type, name, validateWithStateUpdate, }: { | ||
mode: any; | ||
@@ -3,0 +3,0 @@ allFields: any; |
{ | ||
"name": "react-hook-form", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "dist/index.js", | ||
@@ -18,2 +18,12 @@ "module": "dist/index.es.js", | ||
}, | ||
"keywords": [ | ||
"react", | ||
"hooks", | ||
"form", | ||
"forms", | ||
"form-validation", | ||
"validation", | ||
"typescript", | ||
"react-hooks" | ||
], | ||
"repository": "https://github.com/bluebill1049/react-forme.git", | ||
@@ -20,0 +30,0 @@ "author": "<bluebill1049@hotmail.com>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
763
33778
26