UNPKG

react-hook-form

Version:

Performant, flexible and extensible forms library for React Hooks

3 lines (2 loc) • 24 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ReactHookForm={},e.React)}(this,(function(e,r){"use strict";var t=e=>e instanceof HTMLElement;const n="blur",s="change",c="input",u="onBlur",i="onChange",a="onSubmit",o="onTouched",l="all",f="max",d="min",b="maxLength",g="minLength",y="pattern",h="required",m="validate";var v=e=>null==e,p=e=>Array.isArray(e);const O=e=>"object"==typeof e;var j=e=>!v(e)&&!p(e)&&O(e)&&!(e instanceof Date),R=e=>!p(e)&&(/^\w*$/.test(e)||!/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/.test(e)),F=e=>{const r=[];return e.replace(/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,(e,t,n,s)=>{r.push(n?s.replace(/\\(\\)?/g,"$1"):t||e)}),r};function V(e,r,t){let n=-1;const s=R(r)?[r]:F(r),c=s.length,u=c-1;for(;++n<c;){const r=s[n];let c=t;if(n!==u){const t=e[r];c=j(t)||p(t)?t:isNaN(+s[n+1])?{}:[]}e[r]=c,e=e[r]}return e}var k=e=>Object.entries(e).reduce((e,[r,t])=>R(r)?Object.assign(Object.assign({},e),{[r]:t}):(V(e,r,t),e),{}),x=e=>void 0===e,C=e=>e.filter(Boolean),w=(e,r,t)=>{const n=C(r.split(/[,[\].]+?/)).reduce((e,r)=>v(e)?e:e[r],e);return x(n)||n===e?x(e[r])?t:e[r]:n},S=(e,r)=>{t(e)&&e.removeEventListener&&(e.removeEventListener(c,r),e.removeEventListener(s,r),e.removeEventListener(n,r))};const E={isValid:!1,value:""};var D=e=>p(e)?e.reduce((e,r)=>r&&r.ref.checked?{isValid:!0,value:r.ref.value}:e,E):E,A=e=>"radio"===e.type,B=e=>"file"===e.type,L=e=>"checkbox"===e.type,N=e=>"select-multiple"===e.type;const T={value:!1,isValid:!1},W={value:!0,isValid:!0};var M=e=>{if(p(e)){if(e.length>1){const r=e.filter(e=>e&&e.ref.checked).map(({ref:{value:e}})=>e);return{value:r,isValid:!!r.length}}const{checked:r,value:t,attributes:n}=e[0].ref;return r?n&&!x(n.value)?x(t)||""===t?W:{value:t,isValid:!0}:W:T}return T};function P(e,r,t){const n=e.current[r];if(n){const{ref:{value:e,disabled:r},ref:t}=n;if(r)return;return B(t)?t.files:A(t)?D(n.options).value:N(t)?(s=t.options,[...s].filter(({selected:e})=>e).map(({value:e})=>e)):L(t)?M(n.options).value:e}var s;if(t)return w(t.current,r)}function $(e){return!e||e instanceof HTMLElement&&e.nodeType!==Node.DOCUMENT_NODE&&$(e.parentNode)}var H=e=>j(e)&&!Object.keys(e).length,I=e=>"boolean"==typeof e;function q(e,r){const t=R(r)?[r]:F(r),n=1==t.length?e:function(e,r){const t=r.slice(0,-1).length;let n=0;for(;n<t;)e=x(e)?n++:e[r[n++]];return e}(e,t),s=t[t.length-1];let c=void 0;n&&delete n[s];for(let r=0;r<t.slice(0,-1).length;r++){let n=-1,s=void 0;const u=t.slice(0,-(r+1)),i=u.length-1;for(r>0&&(c=e);++n<u.length;){const r=u[n];s=s?s[r]:e[r],i===n&&(j(s)&&H(s)||p(s)&&!s.filter(e=>j(e)&&!H(e)||I(e)).length)&&(c?delete c[r]:delete e[r]),c=s}}return e}const _=(e,r)=>e&&e.ref===r;var U=e=>"string"==typeof e;var z=(e,r,t)=>{const n={};for(const r in e.current)(x(t)||(U(t)?r.startsWith(t):p(t)&&t.find(e=>r.startsWith(e))))&&(n[r]=P(e,r));return function e(r,t){if(!j(r)||!j(t))return t;for(const n in t){const s=r[n],c=t[n];try{j(s)&&j(c)?r[n]=e(s,c):r[n]=c}catch(e){}}return r}(Object.assign({},(r||{}).current||{}),k(n))},G=(e,{type:r,types:t={},message:n})=>j(e)&&e.type===r&&e.message===n&&Object.keys(e.types||{}).length===Object.keys(t).length&&Object.entries(e.types||{}).every(([e,r])=>t[e]===r);var J=e=>e instanceof RegExp;var K=e=>{return j(r=e)&&!J(r)?e:{value:e,message:""};var r},Q=e=>"function"==typeof e,X=e=>U(e)||j(e)&&r.isValidElement(e);function Y(e,r,t="validate"){if(X(e)||I(e)&&!e)return{type:t,message:X(e)?e:"",ref:r}}var Z=(e,r,t,n,s)=>{if(r){const r=t[e];return Object.assign(Object.assign({},r),{types:Object.assign(Object.assign({},r&&r.types?r.types:{}),{[n]:s||!0})})}return{}},ee=async(e,r,{ref:t,ref:{type:n,value:s},options:c,required:u,maxLength:i,minLength:a,min:o,max:l,pattern:p,validate:O},R)=>{const F=e.current,V=t.name,k={},x=A(t),C=L(t),w=x||C,S=""===s,E=Z.bind(null,V,r,k),B=(e,r,n,s=b,c=g)=>{const u=e?r:n;k[V]=Object.assign({type:e?s:c,message:u,ref:t},E(e?s:c,u))};if(u&&(!x&&!C&&(S||v(s))||I(s)&&!s||C&&!M(c).isValid||x&&!D(c).isValid)){const{value:e,message:n}=X(u)?{value:!!u,message:u}:K(u);if(e&&(k[V]=Object.assign({type:h,message:n,ref:w?(F[V].options||[])[0].ref:t},E(h,n)),!r))return k}if(!v(o)||!v(l)){let e,c;const{value:u,message:i}=K(l),{value:a,message:b}=K(o);if("number"===n||!n&&!isNaN(s)){const r=t.valueAsNumber||parseFloat(s);v(u)||(e=r>u),v(a)||(c=r<a)}else{const r=t.valueAsDate||new Date(s);U(u)&&(e=r>new Date(u)),U(a)&&(c=r<new Date(a))}if((e||c)&&(B(!!e,i,b,f,d),!r))return k}if(U(s)&&!S&&(i||a)){const{value:e,message:t}=K(i),{value:n,message:c}=K(a),u=s.toString().length,o=!v(e)&&u>e,l=!v(n)&&u<n;if((o||l)&&(B(!!o,t,c),!r))return k}if(p&&!S){const{value:e,message:n}=K(p);if(J(e)&&!e.test(s)&&(k[V]=Object.assign({type:y,message:n,ref:t},E(y,n)),!r))return k}if(O){const n=P(e,V,R),s=w&&c?c[0].ref:t;if(Q(O)){const e=Y(await O(n),s);if(e&&(k[V]=Object.assign(Object.assign({},e),E(m,e.message)),!r))return k}else if(j(O)){let e={};for(const[t,c]of Object.entries(O)){if(!H(e)&&!r)break;const u=Y(await c(n),s,t);u&&(e=Object.assign(Object.assign({},u),E(t,u.message)),r&&(k[V]=e))}if(!H(e)&&(k[V]=Object.assign({ref:s},e),!r))return k}}return k},re=e=>v(e)||!O(e);const te=(e,r)=>Object.entries(r).map(([t,n])=>((r,t,n)=>{const s=n?`${e}.${t}`:`${e}[${t}]`;return re(r)?s:te(s,r)})(n,t,j(r))).flat(1/0);var ne=(e,r,t,n,s)=>{let c;return t.add(r),H(e)?c=void 0:(c=w(e,r),(j(c)||p(c))&&te(r,c).forEach(e=>t.add(e))),x(c)?s?n:w(n,r):c},se=({isOnBlur:e,isOnChange:r,isOnTouch:t,isTouched:n,isReValidateOnBlur:s,isReValidateOnChange:c,isBlurEvent:u,isSubmitted:i,isOnAll:a})=>!a&&(!i&&t?!(n||u):(i?s:e)?!u:!(i?c:r)||u),ce=e=>e.substring(0,e.indexOf("["));function ue(e=[],r=[]){const t=Object.keys(e),n=Object.keys(r);if(t.length!==n.length)return!1;for(const n of t){const t=e[n],s=r[n];if((j(t)||p(t))&&(j(s)||p(s))?!ue(t,s):t!==s)return!1}return!0}const ie=(e,r)=>RegExp(`^${r}[\\d+]`.replace(/\[/g,"\\[").replace(/\]/g,"\\]")).test(e);var ae=(e,r)=>[...e].some(e=>ie(r,e));var oe=e=>({isOnSubmit:!e||e===a,isOnBlur:e===u,isOnChange:e===i,isOnAll:e===l,isOnTouch:e===o}),le=e=>A(e)||L(e);const fe="undefined"==typeof window,de="undefined"!=typeof document&&!fe&&!x(window.HTMLElement),be=de?"Proxy"in window:"undefined"!=typeof Proxy;function ge(e,r){var t={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&r.indexOf(n)<0&&(t[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)r.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(t[n[s]]=e[n[s]])}return t}const ye=r.createContext(null);ye.displayName="RHFContext";const he=()=>r.useContext(ye);var me=()=>{const e="undefined"==typeof performance?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(r){const t=(16*Math.random()+e)%16|0;return("x"==r?t:3&t|8).toString(16)}))};var ve=(e,r)=>x(r)?[]:p(r)?function(e,r){let t=-1;for(;++t<e.length;)r.indexOf(t)>=0&&delete e[t];return C(e)}(e,r):((e,r)=>[...e.slice(0,r),...e.slice(r+1)])(e,r),pe=(e,r,t)=>p(e)?(x(e[t])&&(e[t]=void 0),e.splice(t,0,e.splice(r,1)[0]),e):[],Oe=(e,r,t)=>{const n=[e[t],e[r]];e[r]=n[0],e[t]=n[1]};function je(e,r){return[...p(r)?r:[r||void 0],...e]}function Re(e,r,t){return[...e.slice(0,r),...p(t)?t:[t||void 0],...e.slice(r)]}var Fe=e=>p(e)?Array(e.length).fill(void 0):void 0;function Ve(e){if(j(e)){const r={};for(const t in e)r[t]=!0;return[r]}return[!0]}const ke=e=>p(e)?e.map(Ve).flat():Ve(e),xe=(e,r)=>Object.assign({[r]:me()},e),Ce=(e,r)=>(p(e)?e:[]).map(e=>xe(e,r));e.Controller=e=>{var{name:t,rules:n,as:s,render:c,defaultValue:u,control:i,onFocus:a}=e,o=ge(e,["name","rules","as","render","defaultValue","control","onFocus"]);const l=he(),{defaultValuesRef:f,setValue:d,register:b,unregister:g,trigger:y,mode:h,reValidateMode:{isReValidateOnBlur:m,isReValidateOnChange:v},formStateRef:{current:{isSubmitted:p,touched:O}},updateFormState:R,readFormStateRef:F,fieldsRef:k,fieldArrayNamesRef:C,unmountFieldsStateRef:S}=i||l.control,E=!ae(C.current,t),D=()=>!x(w(S.current,t))&&E?w(S.current,t):x(u)?w(f.current,t):u,[A,B]=r.useState(D()),L=r.useRef(A),N=r.useRef(a),T=e=>!se(Object.assign({isBlurEvent:e,isReValidateOnBlur:m,isReValidateOnChange:v,isSubmitted:p},h)),W=([e])=>{const r=(e=>re(e)||!j(e.target)||j(e.target)&&!e.type?e:x(e.target.value)?e.target.checked:e.target.value)(e);return B(r),L.current=r,r},M=r.useCallback(()=>{k.current[t]?k.current[t]=Object.assign({ref:k.current[t].ref},n):(b(Object.defineProperty({name:t,focus:N.current},"value",{set(e){B(e),L.current=e},get:()=>L.current}),n),E&&!w(f.current,t)&&B(D()))},[n,t,b]);r.useEffect(()=>()=>{!ae(C.current,t)&&g(t)},[g,t,C]),r.useEffect(()=>{M()},[M]),r.useEffect(()=>{k.current[t]||(M(),E&&B(D()))});const P=()=>{F.current.touched&&!w(O,t)&&(V(O,t,!0),R({touched:O})),T(!0)&&y(t)},$=(...e)=>d(t,W(e),{shouldValidate:T(),shouldDirty:!0}),H=Object.assign(Object.assign({},o),{onChange:$,onBlur:P,name:t,value:A});return s?r.isValidElement(s)?r.cloneElement(s,H):r.createElement(s,H):c?c({onChange:$,onBlur:P,value:A,name:t}):null},e.FormProvider=e=>{var{children:t}=e,n=ge(e,["children"]);return r.createElement(ye.Provider,{value:Object.assign({},n)},t)},e.appendErrors=Z,e.get=w,e.transformToNestObject=k,e.useFieldArray=({control:e,name:t,keyName:n="id"})=>{const s=he(),c=r.useRef(-1),{isWatchAllRef:u,resetFieldArrayFunctionRef:i,fieldArrayNamesRef:a,fieldsRef:o,defaultValuesRef:l,removeFieldEventListener:f,formStateRef:{current:{dirtyFields:d,touched:b,errors:g}},updateFormState:y,readFormStateRef:h,watchFieldsRef:m,validFieldsRef:v,fieldsWithValidationRef:O,fieldArrayDefaultValues:j,validateResolver:R,renderWatchedInputs:F,getValues:k}=e||s.control,S=ce(t),E=r.useRef([...w(j.current,S)?w(j.current,t,[]):w(l.current,t,[])]),[D,A]=r.useState(Ce(E.current,n)),B=r.useRef(D),L=()=>w(k()||{},t,B.current).map((e,r)=>Object.assign(Object.assign({},B.current[r]),e));B.current=D,a.current.add(t),w(j.current,S)||V(j.current,S,w(l.current,S));const N=e=>e.map(e=>xe(e,n)),T=e=>{if(A(e),h.current.isValid&&R){const r={};V(r,t,e),R(r)}},W=e=>(h.current.isDirty||h.current.dirtyFields)&&(x(e)||!ue(e.map((e={})=>{var r=n;e[r];return ge(e,["symbol"==typeof r?r:r+""])}),w(l.current,t))),M=()=>{for(const e in o.current)ie(e,t)&&o.current[e]&&f(o.current[e],!0)},P=()=>{M(),q(j.current,t),E.current=w(l.current,t),A(Ce(E.current,n))};return r.useEffect(()=>{const e=w(j.current,t);if(e&&D.length<e.length&&(e.pop(),V(j.current,t,e)),u.current)y();else if(m){let e=!0;for(const r of m.current)if(r.startsWith(t)){y(),e=!1;break}e&&F(t)}if(c.current>-1)for(const e in o.current){const r=o.current[e];if(e.startsWith(`${t}[${c.current}]`)&&r.ref.focus){r.ref.focus();break}}c.current=-1},[D,t,j,o,m,u]),r.useEffect(()=>{const e=i.current;return e[t]=P,()=>{M(),delete e[t],a.current.delete(t)}},[]),{swap:r.useCallback((e,r)=>{const n=L();Oe(n,e,r),M(),T([...n]),p(w(g,t))&&Oe(w(g,t),e,r),h.current.touched&&w(b,t)&&Oe(w(b,t),e,r),(h.current.dirtyFields||h.current.isDirty)&&w(d,t)&&Oe(w(d,t),e,r),y({dirtyFields:d,errors:g,touched:b,isDirty:W(n)}),F(t)},[t,g]),move:r.useCallback((e,r)=>{const n=L();pe(n,e,r),M(),T([...n]),p(w(g,t))&&pe(w(g,t),e,r),h.current.touched&&w(b,t)&&pe(w(b,t),e,r),(h.current.dirtyFields||h.current.isDirty)&&w(d,t)&&pe(w(d,t),e,r),y({dirtyFields:d,errors:g,touched:b,isDirty:W(n)}),F(t)},[t,g]),prepend:r.useCallback((e,r=!0)=>{const s=Fe(e);T(je(L(),p(e)?N(e):[xe(e,n)])),M(),w(j.current,t)&&V(j.current,t,je(w(j.current,t),s)),p(w(g,t))&&V(g,t,je(w(g,t),s)),h.current.touched&&w(b,t)&&V(b,t,je(w(b,t),s)),(h.current.dirtyFields||h.current.isDirty)&&V(d,t,je(w(d,t)||[],ke(e))),y({errors:g,dirtyFields:d,isDirty:!0,touched:b}),F(t),c.current=r?0:-1},[t,g]),append:r.useCallback((e,r=!0)=>{T([...B.current,...p(e)?N(e):[xe(e,n)]]),(h.current.dirtyFields||h.current.isDirty)&&(V(d,t,[...w(d,t,Fe(D)),...ke(e)]),y({isDirty:!0,dirtyFields:d})),c.current=r?B.current.length:-1,F(t)},[t,g,D]),remove:r.useCallback(e=>{const r=L();T(ve(r,e)),M(),p(w(g,t))&&(V(g,t,ve(w(g,t),e)),C(w(g,t,[])).length||q(g,t)),h.current.touched&&w(b,t)&&V(b,t,ve(w(b,t),e)),(h.current.dirtyFields||h.current.isDirty)&&w(d,t)&&(V(d,t,ve(w(d,t),e)),C(w(d,t,[])).length||q(d,t)),h.current.isValid&&!R&&(V(v.current,t,ve(w(v.current,t,[]),e)),C(w(v.current,t,[])).length||q(v.current,t),V(O.current,t,ve(w(O.current,t,[]),e)),C(w(O.current,t,[])).length||q(O.current,t)),y({dirtyFields:d,errors:g,touched:b,isDirty:W(ve(r,e))}),F(t)},[D,t,g]),insert:r.useCallback((e,r,s=!0)=>{const u=Fe(r),i=L();T(Re(i,e,p(r)?N(r):[xe(r,n)])),M(),p(w(g,t))&&V(g,t,Re(w(g,t),e,u)),h.current.touched&&w(b,t)&&V(b,t,Re(w(b,t),e,u)),(h.current.dirtyFields||h.current.isDirty)&&w(d,t)&&V(d,t,Re(w(d,t),e,ke(r))),y({dirtyFields:d,errors:g,touched:b,isDirty:W(Re(i,e))}),F(t),c.current=s?e:-1},[t,g]),fields:D}},e.useForm=function({mode:e=a,reValidateMode:u=i,resolver:o,context:f,defaultValues:d={},shouldFocusError:b=!0,shouldUnregister:g=!0,criteriaMode:y}={}){const h=r.useRef({}),m=r.useRef({}),O=r.useRef(new Set),R=r.useRef({}),F=r.useRef({}),E=r.useRef({}),D=r.useRef({}),T=r.useRef(d),W=r.useRef({}),M=r.useRef(!1),I=r.useRef(!1),J=r.useRef(),K=r.useRef({}),X=r.useRef({}),Y=r.useRef(f),Z=r.useRef(o),ie=r.useRef(new Set),ge=r.useRef(oe(e)),{current:{isOnSubmit:ye,isOnTouch:he}}=ge,me=y===l,[ve,pe]=r.useState({isDirty:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touched:{},isSubmitting:!1,isValid:!ye,errors:{}}),Oe=r.useRef({isDirty:!be,dirtyFields:!be,isSubmitted:ye,submitCount:!be,touched:!be||he,isSubmitting:!be,isValid:!be,errors:!be}),je=r.useRef(ve),Re=r.useRef(),{current:{isOnBlur:Fe,isOnChange:Ve}}=r.useRef(oe(u));Y.current=f,Z.current=o,je.current=ve;const ke=r.useCallback((e={})=>!M.current&&pe(Object.assign(Object.assign({},je.current),e)),[]),xe=r.useCallback((e,r,t=!1,n={},s)=>{let c=t||function({errors:e,name:r,error:t,validFields:n,fieldsWithValidation:s}){const c=H(t),u=H(e),i=w(t,r),a=w(e,r);return(!c||!w(n,r))&&(!!(u!==c||!u&&!a||c&&w(s,r)&&!w(n,r))||i&&!G(a,i))}({errors:je.current.errors,error:r,name:e,validFields:D.current,fieldsWithValidation:E.current});const u=w(je.current.errors,e);H(r)?((w(E.current,e)||Z.current)&&(V(D.current,e,!0),c=c||u),q(je.current.errors,e)):(q(D.current,e),c=c||!u||!G(u,r[e]),V(je.current.errors,e,r[e])),!c&&H(n)||ke(Object.assign(Object.assign(Object.assign({},n),{errors:je.current.errors}),Z.current?{isValid:!!s}:{}))},[]),Ce=r.useCallback(({ref:e,options:r},n)=>{const s=de&&t(e)&&v(n)?"":n;A(e)&&r?r.forEach(({ref:e})=>e.checked=e.value===s):B(e)&&!U(s)?e.files=s:N(e)?[...e.options].forEach(e=>e.selected=s.includes(e.value)):L(e)&&r?r.length>1?r.forEach(({ref:e})=>e.checked=String(s).includes(e.value)):r[0].ref.checked=!!s:e.value=s},[]),we=r.useCallback((e,r=!0)=>{if(!h.current[e]||!Oe.current.isDirty&&!Oe.current.dirtyFields)return{};const t=W.current[e]!==P(h,e,K),n=w(je.current.dirtyFields,e),s=ae(ie.current,e),c=je.current.isDirty;t?V(je.current.dirtyFields,e,!0):q(je.current.dirtyFields,e);const u={isDirty:s&&!ue(w(Te(),ce(e)),w(T.current,ce(e)))||!H(je.current.dirtyFields),dirtyFields:je.current.dirtyFields},i=Oe.current.isDirty&&c!==u.isDirty||Oe.current.dirtyFields&&n!==w(je.current.dirtyFields,e);return i&&r&&(je.current=Object.assign(Object.assign({},je.current),u),ke(Object.assign({},u))),i?u:{}},[]),Se=r.useCallback(async(e,r)=>{if(h.current[e]){const t=await ee(h,me,h.current[e],K);return xe(e,t,r),H(t)}return!1},[xe,me]),Ee=r.useCallback(async e=>{const{errors:r}=await Z.current(Te(),Y.current,me),t=je.current.isValid;if(p(e)){const t=e.map(e=>{const t=w(r,e);return t?V(ve.errors,e,t):q(ve.errors,e),!t}).every(Boolean);return ke({isValid:H(r),errors:ve.errors}),t}{const n=w(r,e);return xe(e,n?{[e]:n}:{},t!==H(r),{},H(r)),!n}},[xe,me]),De=r.useCallback(async e=>{const r=e||Object.keys(h.current);if(Z.current)return Ee(r);if(p(r)){return(await Promise.all(r.map(async e=>await Se(e,!0)))).every(Boolean)}return await Se(r)},[Ee,Se]),Ae=r.useCallback((e,r,{shouldDirty:t,shouldValidate:n})=>{te(e,r).forEach(s=>{const c={},u=h.current[s];u&&(V(c,e,r),Ce(u,w(c,s)),t&&we(s),n&&De(s))})},[De,Ce,we]),Be=r.useCallback((e,r,t)=>{h.current[e]?(Ce(h.current[e],r),t.shouldDirty&&we(e)):re(r)||Ae(e,r,t),V(K.current,e,r)},[we,Ce,Ae]),Le=e=>I.current||O.current.has(e)||O.current.has((e.match(/\w+/)||[])[0]),Ne=(e,r=!0)=>{if(!H(R.current))for(const t in R.current)e&&!R.current[t].has(e)&&!R.current[t].has(ce(e))&&R.current[t].size||(F.current[t](),r=!1);return r};function Te(e){if(U(e))return P(h,e,K);if(p(e)){const r={};for(const t of e)V(r,t,P(h,t,K));return r}return z(h,K)}J.current=J.current?J.current:async({type:e,target:r})=>{const t=r.name,s=h.current[t];let c,u;if(s){const r=e===n,i=se(Object.assign({isBlurEvent:r,isReValidateOnChange:Ve,isReValidateOnBlur:Fe,isTouched:!!w(je.current.touched,t),isSubmitted:je.current.isSubmitted},ge.current));let a=we(t,!1),o=!H(a)||Le(t);if(r&&!w(je.current.touched,t)&&Oe.current.touched&&(V(je.current.touched,t,!0),a=Object.assign(Object.assign({},a),{touched:je.current.touched})),i)return Ne(t),(!H(a)||o&&H(a))&&ke(a);if(Z.current){const{errors:e}=await Z.current(Te(),Y.current,me),r=je.current.isValid;c=w(e,t)?{[t]:w(e,t)}:{},u=H(e),r!==u&&(o=!0)}else c=await ee(h,me,s,K);Ne(t),xe(t,c,o,a,u)}};const We=r.useCallback(async(e={})=>{const{errors:r}=await Z.current(Object.assign(Object.assign(Object.assign({},T.current),Te()),e),Y.current,me),t=je.current.isValid,n=H(r);t!==n&&ke({isValid:n})},[me]),Me=r.useCallback((e,r)=>function(e,r,t,n,s,c){const{ref:u,ref:{name:i,type:a}}=t,o=e.current[i];if(!s){const r=P(e,i,n);x(r)||V(n.current,i,r)}if(a)if((A(u)||L(u))&&o){const{options:t}=o;p(t)&&t.length?(C(t).forEach((e,n)=>{const{ref:s}=e;(s&&$(s)&&_(e,s)||c)&&(S(s,r),q(t,`[${n}]`))}),t&&!C(t).length&&delete e.current[i]):delete e.current[i]}else($(u)&&_(o,u)||c)&&(S(u,r),delete e.current[i]);else delete e.current[i]}(h,J.current,e,K,g,r),[g]),Pe=r.useCallback((e,r)=>{e&&(Me(e,r),g&&(q(D.current,e.ref.name),q(E.current,e.ref.name),q(W.current,e.ref.name),q(ve.errors,e.ref.name),q(je.current.dirtyFields,e.ref.name),q(je.current.touched,e.ref.name),ke({errors:ve.errors,isDirty:!H(je.current.dirtyFields),dirtyFields:je.current.dirtyFields,touched:je.current.touched}),Z.current&&We()))},[We,Me]),$e=r.useCallback((e,r,t)=>{const n=t?R.current[t]:O.current,s=x(r)?T.current:r,c=z(h,K,e);return U(e)?ne(c,e,n,x(r)?w(s,e):r,!0):p(e)?e.reduce((e,r)=>Object.assign(Object.assign({},e),{[r]:ne(c,r,n,s)}),{}):(x(t)&&(I.current=!0),k(!H(c)&&c||s))},[]);function He(e,r={}){const{name:u,type:i,value:a}=e,l=Object.assign({ref:e},r),f=h.current,d=le(e),b=r=>de&&(!t(e)||r===e);let g,y,m=f[u],v=!0;if(m&&(d?p(m.options)&&C(m.options).find(e=>a===e.ref.value&&b(e.ref)):b(m.ref)))return void(f[u]=Object.assign(Object.assign({},m),r));m=i?d?Object.assign({options:[...C(m&&m.options||[]),{ref:e}],ref:{type:i,name:u}},r):Object.assign({},l):l,f[u]=m;const O=x(w(K.current,u));if(H(T.current)&&O||(y=w(O?T.current:K.current,u),v=x(y),g=ae(ie.current,u),v||g||Ce(m,y)),o&&!g&&Oe.current.isValid?We():H(r)||(V(E.current,u,!0),!ye&&Oe.current.isValid&&ee(h,me,m,K).then(e=>{const r=je.current.isValid;H(e)?V(D.current,u,!0):q(D.current,u),r!==H(e)&&ke()})),!(W.current[u]||g&&v)){const e=P(h,u,K);W.current[u]=v?j(e)?Object.assign({},e):e:y}i&&function({ref:e},r,u){t(e)&&u&&(e.addEventListener(r?s:c,u),e.addEventListener(n,u))}(d&&m.options?m.options[m.options.length-1]:m,d||"select-one"===e.type,J.current)}const Ie=r.useCallback((e,r)=>async t=>{t&&t.preventDefault&&(t.preventDefault(),t.persist());let n={},s=z(h,K);Oe.current.isSubmitting&&ke({isSubmitting:!0});try{if(Z.current){const{errors:e,values:r}=await Z.current(s,Y.current,me);ve.errors=e,n=e,s=r}else for(const e of Object.values(h.current))if(e){const{ref:{name:r}}=e,t=await ee(h,me,e,K);t[r]?(V(n,r,t[r]),q(D.current,r)):w(E.current,r)&&(q(ve.errors,r),V(D.current,r,!0))}H(n)&&Object.keys(ve.errors).every(e=>Object.keys(h.current).includes(e))?(ke({errors:{}}),await e(s,t)):(ve.errors=Object.assign(Object.assign({},ve.errors),n),r&&await r(n,t),b&&((e,r)=>{for(const t in e)if(w(r,t)){const r=e[t];if(r){if(r.ref.focus){r.ref.focus();break}if(r.options){r.options[0].ref.focus();break}}}})(h.current,n))}finally{ke({isSubmitted:!0,isSubmitting:!1,errors:ve.errors,submitCount:je.current.submitCount+1})}},[b,me]);Re.current=Re.current||!de?Re.current:function(e,r){const t=new MutationObserver(()=>{for(const t of Object.values(e.current))if(t&&t.options)for(const e of t.options)e&&e.ref&&$(e.ref)&&r(t);else t&&$(t.ref)&&r(t)});return t.observe(window.document,{childList:!0,subtree:!0}),t}(h,Pe),r.useEffect(()=>(M.current=!1,()=>{M.current=!0,Re.current&&Re.current.disconnect(),h.current&&Object.values(h.current).forEach(e=>Pe(e,!0))}),[Pe]),!o&&Oe.current.isValid&&(ve.isValid=ue(D.current,E.current)&&H(ve.errors));const qe={trigger:De,setValue:r.useCallback((function(e,r,t={}){Be(e,r,t),Le(e)&&ke(),Ne(e),t.shouldValidate&&De(e)}),[Be,De]),getValues:r.useCallback(Te,[]),register:r.useCallback((function(e,r){if(!fe)if(U(e))He({name:e},r);else{if(!j(e)||!("name"in e))return r=>r&&He(r,e);He(e,r)}}),[T.current]),unregister:r.useCallback((function(e){(p(e)?e:[e]).forEach(e=>Pe(h.current[e],!0))}),[])},_e=Object.assign({removeFieldEventListener:Me,renderWatchedInputs:Ne,watchInternal:$e,mode:ge.current,reValidateMode:{isReValidateOnBlur:Fe,isReValidateOnChange:Ve},fieldsRef:h,isWatchAllRef:I,watchFieldsRef:O,resetFieldArrayFunctionRef:X,watchFieldsHookRef:R,watchFieldsHookRenderRef:F,fieldArrayDefaultValues:m,validFieldsRef:D,fieldsWithValidationRef:E,fieldArrayNamesRef:ie,readFormStateRef:Oe,formStateRef:je,defaultValuesRef:T,unmountFieldsStateRef:K,updateFormState:ke,validateResolver:o?We:void 0},qe);return Object.assign({watch:function(e,r){return $e(e,r)},control:_e,formState:be?new Proxy(ve,{get:(e,r)=>{if(r in e)return Oe.current[r]=!0,e[r]}}):ve,handleSubmit:Ie,reset:r.useCallback((e,r={})=>{if(de)for(const e of Object.values(h.current))if(e){const{ref:r,options:n}=e,s=le(r)&&p(n)?n[0].ref:r;if(t(s))try{s.closest("form").reset();break}catch(e){}}h.current={},T.current=e||Object.assign({},T.current),e&&Ne(""),K.current=g?{}:e||{},Object.values(X.current).forEach(e=>Q(e)&&e()),(({errors:e,isDirty:r,isSubmitted:t,touched:n,isValid:s,submitCount:c,dirtyFields:u})=>{s||(D.current=new Set,E.current=new Set),W.current={},m.current={},O.current=new Set,I.current=!1,ke({isDirty:!!r&&je.current.isDirty,isSubmitted:!!t&&je.current.isSubmitted,submitCount:c?je.current.submitCount:0,isValid:!s||je.current.isValid,dirtyFields:u?je.current.dirtyFields:{},touched:n?je.current.touched:{},errors:e?ve.errors:{}})})(r)},[]),clearErrors:r.useCallback((function(e){e&&(p(e)?e:[e]).forEach(e=>q(ve.errors,e)),ke({errors:e?ve.errors:{}})}),[]),setError:r.useCallback((function(e,r){V(ve.errors,e,Object.assign(Object.assign({},r),{ref:(h.current[e]||{}).ref})),ke({isValid:!1,errors:ve.errors})}),[]),errors:ve.errors},qe)},e.useFormContext=he,e.useWatch=function({control:e,name:t,defaultValue:n}){const s=he(),{watchFieldsHookRef:c,watchFieldsHookRenderRef:u,watchInternal:i,defaultValuesRef:a}=e||s.control,[o,l]=r.useState(x(n)?U(t)?w(a.current,t):p(t)?t.reduce((e,r)=>Object.assign(Object.assign({},e),{[r]:w(a.current,r)}),{}):a.current:n),f=r.useRef(),d=r.useRef(n),b=r.useCallback(()=>{const e=i(t,d.current,f.current);l(j(e)?Object.assign({},e):p(e)?[...e]:e)},[l,i,d,t,f]);return r.useEffect(()=>{const e=f.current=me(),r=u.current,n=c.current;return n[e]=new Set,r[e]=b,i(t,d.current,e),()=>{delete n[e],delete r[e]}},[t,b,u,c,i,d]),x(o)?n:o},Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=index.umd.production.min.js.map