react-hook-form 7.0.0-rc.8 → 7.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- !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";function t(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var n=t(r),s=e=>void 0===e,c=e=>null==e;const u=e=>"object"==typeof e;var i=e=>!c(e)&&!Array.isArray(e)&&u(e)&&!(e instanceof Date),a=e=>e.filter(Boolean),l=(e={},r,t)=>{const n=a(r.split(/[,[\].]+?/)).reduce((e,r)=>c(e)?e:e[r],e);return s(n)||n===e?s(e[r])?t:e[r]:n},o=e=>/^\w*$/.test(e),f=e=>a(e.replace(/["|']|\]/g,"").split(/\.|\[/));function d(e,r,t){let n=-1;const s=o(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=i(t)||Array.isArray(t)?t:isNaN(+s[n+1])?{}:[]}e[r]=c,e=e[r]}return e}var b=(e,r)=>{const t=Object.assign({},e);return delete t[r],t};const y=(e,r,t)=>{for(const n of t||Object.keys(e)){const t=l(e,n);if(t){const e=t._f,n=b(t,"_f");if(e&&r(e.name)){if(e.ref.focus&&s(e.ref.focus()))break;if(e.refs){e.refs[0].focus();break}}else i(n)&&y(n,r)}}};var g=e=>c(e)||!u(e);function m(e,r,t){if(g(e)||g(r)||e instanceof Date||r instanceof Date)return e===r;if(!n.isValidElement(e)){const n=Object.keys(e),s=Object.keys(r);if(n.length!==s.length)return!1;for(const s of n){const n=e[s];if(!t||"ref"!==s){const e=r[s];if((i(n)||Array.isArray(n))&&(i(e)||Array.isArray(e))?!m(n,e,t):n!==e)return!1}}}return!0}function h(e,r,t,n,s){let c=-1;for(;++c<e.length;){for(const n in e[c])Array.isArray(e[c][n])?(!t[c]&&(t[c]={}),t[c][n]=[],h(e[c][n],l(r[c]||{},n,[]),t[c][n],t[c],n)):m(l(r[c]||{},n),e[c][n])?d(t[c]||{},n):t[c]=Object.assign(Object.assign({},t[c]),{[n]:!0});n&&!t.length&&delete n[s]}return t}var v=(e,r,t)=>function e(r,t){if(g(r)||g(t))return t;for(const n in t){const s=r[n],c=t[n];try{r[n]=i(s)&&i(c)||Array.isArray(s)&&Array.isArray(c)?e(s,c):c}catch(e){}}return r}(h(e,r,t.slice(0,e.length)),h(r,e,t.slice(0,e.length))),p=e=>i(e)&&!Object.keys(e).length;const A="blur",O="change",x="onBlur",j="onChange",k="onSubmit",V="onTouched",R="all",F="undefined",S="max",w="min",_="maxLength",C="minLength",D="pattern",E="required",B="validate";var N=(e,r,t)=>p(e)||Object.keys(e).length>=Object.keys(r).length||Object.keys(e).find(e=>r[e]===(!t||R));const $=(e,r={current:{}},t={})=>{for(const n in e.current){const s=e.current[n];if(s){const e=s._f,c=b(s,"_f");d(t,n,e?e.ref.disabled||e.refs&&e.refs.every(e=>e.disabled)?void 0:e.value:Array.isArray(s)?[]:{}),c&&$({current:c},r,t[n])}}return Object.assign(Object.assign({},r.current),t)},M={isValid:!1,value:null};var T=e=>Array.isArray(e)?e.reduce((e,r)=>r&&r.checked&&!r.disabled?{isValid:!0,value:r.value}:e,M):M,I=e=>"radio"===e.type,P=e=>"file"===e.type,W=e=>"checkbox"===e.type,L=e=>"select-multiple"===e.type;const H={value:!1,isValid:!1},q={value:!0,isValid:!0};var z=e=>{if(Array.isArray(e)){if(e.length>1){const r=e.filter(e=>e&&e.checked&&!e.disabled).map(e=>e.value);return{value:r,isValid:!!r.length}}return e[0].checked&&!e[0].disabled?e[0].attributes&&!s(e[0].attributes.value)?s(e[0].value)?q:{value:e[0].value,isValid:!0}:q:H}return H};function G(e){if(e&&e._f){const t=e._f.ref;if(t.disabled)return;return P(t)?t.files:I(t)?T(e._f.refs).value:L(t)?(r=t.options,[...r].filter(({selected:e})=>e).map(({value:e})=>e)):W(t)?z(e._f.refs).value:((e,{valueAsNumber:r,valueAsDate:t,setValueAs:n})=>r?""===e?NaN:+e:t?new Date(e):n?n(e):e)(s(t.value)?e._f.ref.value:t.value,e._f)}var r}var J=e=>e instanceof RegExp,K=e=>i(e)&&!J(e)?e:{value:e,message:""},Q=e=>"string"==typeof e,U=e=>"function"==typeof e,X=e=>"boolean"==typeof e,Y=e=>Q(e)||n.isValidElement(e);function Z(e,r,t="validate"){if(Y(e)||X(e)&&!e)return{type:t,message:Y(e)?e:"",ref:r}}var ee=(e,r,t,n,s)=>r?Object.assign(Object.assign({},t[e]),{types:Object.assign(Object.assign({},t[e]&&t[e].types?t[e].types:{}),{[n]:s||!0})}):{},re=async({_f:{ref:e,refs:r,required:t,maxLength:n,minLength:s,min:u,max:a,pattern:l,validate:o,name:f,value:d}},b)=>{const y={},g=I(e),m=W(e),h=g||m,v=!d||Array.isArray(d)&&!d.length,A=ee.bind(null,f,b,y),O=(r,t,n,s=_,c=C)=>{const u=r?t:n;y[f]=Object.assign({type:r?s:c,message:u,ref:e},A(r?s:c,u))};if(t&&(!g&&!m&&(v||c(d))||X(d)&&!d||m&&!z(r).isValid||g&&!T(r).isValid)){const{value:n,message:s}=Y(t)?{value:!!t,message:t}:K(t);if(n&&(y[f]=Object.assign({type:E,message:s,ref:h?(r||[])[0]||{}:e},A(E,s)),!b))return y}if(!(c(u)&&c(a)||""===d)){let r,t;const n=K(a),s=K(u);if(isNaN(d)){const c=e.valueAsDate||new Date(d);Q(n.value)&&(r=c>new Date(n.value)),Q(s.value)&&(t=c<new Date(s.value))}else{const u=e.valueAsNumber||parseFloat(d);c(n.value)||(r=u>n.value),c(s.value)||(t=u<s.value)}if((r||t)&&(O(!!r,n.message,s.message,S,w),!b))return y}if(Q(d)&&!v&&(n||s)){const e=K(n),r=K(s),t=!c(e.value)&&d.length>e.value,u=!c(r.value)&&d.length<r.value;if((t||u)&&(O(t,e.message,r.message),!b))return y}if(Q(d)&&l&&!v){const{value:r,message:t}=K(l);if(J(r)&&!r.test(d)&&(y[f]=Object.assign({type:D,message:t,ref:e},A(D,t)),!b))return y}if(o){const t=h&&r?r[0]:e;if(U(o)){const e=Z(await o(d),t);if(e&&(y[f]=Object.assign(Object.assign({},e),A(B,e.message)),!b))return y}else if(i(o)){let e={};for(const[r,n]of Object.entries(o)){if(!p(e)&&!b)break;const s=Z(await n(d),t,r);s&&(e=Object.assign(Object.assign({},s),A(r,s.message)),b&&(y[f]=e))}if(!p(e)&&(y[f]=Object.assign({ref:t},e),!b))return y}}return y},te=e=>e.substring(0,e.search(/.\d/))||e,ne=(e,r,t,n,s=!0)=>e?new Proxy(r,{get:(e,r)=>{if(r in e)return t.current[r]!==R&&(t.current[r]=!s||R),n&&(n.current[r]=!0),e[r]}}):r;class se{constructor(){this.tearDowns=[]}add(e){this.tearDowns.push(e)}unsubscribe(){for(const e of this.tearDowns)e();this.tearDowns=[]}}class ce{constructor(e,r){this.observer=e,this.closed=!1,r.add(()=>this.closed=!0)}next(e){this.closed||this.observer.next(e)}}class ue{constructor(){this.observers=[]}next(e){for(const r of this.observers)r.next(e)}subscribe(e){const r=new se,t=new ce(e,r);return this.observers.push(t),r}unsubscribe(){this.observers=[]}}var ie=typeof window!==F&&typeof window.HTMLElement!==F&&typeof document!==F;const ae=ie?"Proxy"in window:typeof Proxy!==F;function le(e,r){const t=o(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=s(e)?n++:e[r[n++]];return e}(e,t),c=t[t.length-1];let u;n&&delete n[c];for(let r=0;r<t.slice(0,-1).length;r++){let n,s=-1;const c=t.slice(0,-(r+1)),a=c.length-1;for(r>0&&(u=e);++s<c.length;){const r=c[s];n=n?n[r]:e[r],a===s&&(i(n)&&p(n)||Array.isArray(n)&&!n.filter(e=>i(e)&&!p(e)||X(e)).length)&&(u?delete u[r]:delete e[r]),u=n}}return e}var oe=e=>({isOnSubmit:!e||e===k,isOnBlur:e===x,isOnChange:e===j,isOnAll:e===R,isOnTouch:e===V}),fe=e=>e instanceof HTMLElement;function de(e,r){const t={};for(const n of e){const e=l(r,n);e&&(o(n)?t[n]=e._f:d(t,n,e._f))}return t}const be=typeof window===F;const ye=n.createContext(null);ye.displayName="RHFContext";const ge=()=>n.useContext(ye);var me=()=>{const e=typeof performance===F?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{const t=(16*Math.random()+e)%16|0;return("x"==r?t:3&t|8).toString(16)})},he=(e=[],r)=>e.map(e=>Object.assign({[r]:e&&e[r]||me()},e));var ve=(e,r)=>s(r)?[]:function(e,r){let t=0;const n=[...e];for(const e of r)n.splice(e-t,1),t++;return a(n).length?n:[]}(e,(Array.isArray(r)?r:[r]).sort((e,r)=>e-r)),pe=(e,r,t)=>Array.isArray(e)?(s(e[t])&&(e[t]=void 0),e.splice(t,0,e.splice(r,1)[0]),e):[],Ae=(e,r,t)=>{e[r]=[e[t],e[t]=e[r]][0]};function Oe(e,r){return[...Array.isArray(r)?r:[r],...e]}function xe(e,r){return[...e,...Array.isArray(r)?r:[r]]}function je(e,r,t){return[...e.slice(0,r),...Array.isArray(t)?t:[t],...e.slice(r)]}var ke=e=>Array.isArray(e)?Array(e.length).fill(void 0):void 0;function Ve({control:e}={}){const r=ge(),{formStateRef:t,formStateSubjectRef:s,readFormStateRef:c}=e||r.control,[u,i]=n.useState(t.current),a=n.useRef({isDirty:!1,dirtyFields:!1,touchedFields:!1,isValidating:!1,isValid:!1,errors:!1});return n.useEffect(()=>{const e=s.current.subscribe({next:e=>{N(e,a.current)&&i(Object.assign(Object.assign({},t.current),e))}});return()=>e.unsubscribe()},[]),ne(ae,u,c,a,!1)}function Re({name:e,rules:r,defaultValue:t,control:c}){const u=ge(),{defaultValuesRef:a,register:o,fieldsRef:f,fieldArrayNamesRef:d,controllerSubjectRef:b}=c||u.control,{onChange:y,onBlur:g,ref:m}=o(e,r),[h,v]=n.useState(s(l(f.current,e)._f.value)||((e,r)=>[...e].some(e=>te(r)===e))(d.current,e)?s(t)?l(a.current,e):t:l(f.current,e)._f.value),p=Ve({control:c||u.control});return l(f.current,e)._f.value=h,n.useEffect(()=>{const r=b.current.subscribe({next:r=>(!r.name||e===r.name)&&v(l(r.values,e))});return()=>r.unsubscribe()},[e]),{field:{onChange:r=>{const t=(e=>i(e)&&e.target?W(e.target)?e.target.checked:e.target.value:e)(r);v(t),y({target:{value:t,name:e},type:O})},onBlur:()=>{g({target:{name:e},type:A})},name:e,value:h,ref:m},formState:p,fieldState:Object.defineProperties({},{invalid:{get:()=>!!l(p.errors,e)},isDirty:{get:()=>!!l(p.dirtyFields,e)},isTouched:{get:()=>!!l(p.touchedFields,e)},error:{get:()=>l(p.errors,e)}})}}e.Controller=e=>e.render(Re(e)),e.FormProvider=e=>n.createElement(ye.Provider,{value:b(e,"children")},e.children),e.appendErrors=ee,e.get=l,e.set=d,e.useController=Re,e.useFieldArray=({control:e,name:r,keyName:t="id"})=>{const c=ge(),u=n.useRef(""),{isWatchAllRef:i,watchFieldsRef:o,getFormIsDirty:f,watchSubjectRef:g,fieldArraySubjectRef:m,fieldArrayNamesRef:h,fieldsRef:p,defaultValuesRef:A,formStateRef:O,formStateSubjectRef:x,readFormStateRef:j,validFieldsRef:k,fieldsWithValidationRef:V,fieldArrayDefaultValuesRef:R}=e||c.control,[F,S]=n.useState(he(l(R.current,te(r))?l(R.current,r,[]):l(A.current,r,[]),t));d(R.current,r,[...F]),h.current.add(r);const w=e=>e.map(e=>b(e||{},t)),_=()=>{const e=l($(p,A),r,[]);return he(l(R.current,r,[]).map((r,t)=>Object.assign(Object.assign({},r),e[t])),t)},C=(e,t)=>t?s(t.focusIndex)?t.focusName?t.focusName:t.shouldFocus?`${r}.${e}`:"":`${r}.${t.focusIndex}`:`${r}.${e}`,D=(e=[])=>S(he(e,t)),E=e=>!a(l(e,r,[])).length&&le(e,r),B=(e,t,n=[],s=!0)=>{if(l(p.current,r)){const n=e(l(p.current,r),t.argA,t.argB);s&&d(p.current,r,n)}if(Array.isArray(l(O.current.errors,r))){const n=e(l(O.current.errors,r),t.argA,t.argB);s&&d(O.current.errors,r,n),E(O.current.errors)}if(j.current.touchedFields&&l(O.current.touchedFields,r)){const n=e(l(O.current.touchedFields,r),t.argA,t.argB);s&&d(O.current.touchedFields,r,n),E(O.current.touchedFields)}(j.current.dirtyFields||j.current.isDirty)&&(d(O.current.dirtyFields,r,v(w(n),l(A.current,r,[]),l(O.current.dirtyFields,r,[]))),(e=>{e&&d(O.current.dirtyFields,r,v(w(e),l(A.current,r,[]),l(O.current.dirtyFields,r,[])))})(n),E(O.current.dirtyFields)),j.current.isValid&&(d(k.current,r,e(l(k.current,r,[]),t.argA)),E(k.current),d(V.current,r,e(l(V.current,r,[]),t.argA)),E(V.current)),x.current.next({isDirty:f(r,w(n)),errors:O.current.errors,isValid:O.current.isValid})},N=(e,t=0,n="")=>e.forEach((e,s)=>Object.entries(e).forEach(([e,c])=>{const u=`${n||r}.${n?s:t+s}.${e}`;Array.isArray(c)?N(c,s,u):d(p.current,u,{_f:{ref:{name:u},name:u,value:c}})}));return n.useEffect(()=>{if(i.current)x.current.next({});else for(const e of o.current)if(r.startsWith(e)){x.current.next({});break}g.current.next({name:r,value:l($(p,A),r,[])}),u.current&&y(p.current,e=>e.startsWith(u.current)),u.current="",m.current.next({name:r,fields:w([...F])})},[F,r]),n.useEffect(()=>{const e=m.current.subscribe({next({name:e,fields:t,isReset:n}){n&&(le(p.current,e||r),e?d(R.current,e,t):R.current=t,D(l(R.current,r)))}});return!l(p.current,r)&&d(p.current,r,[]),()=>{R.current=$(p),e.unsubscribe()}},[]),{swap:n.useCallback((e,r)=>{const t=_();Ae(t,e,r),B(Ae,{argA:e,argB:r},t,!1),D(t)},[r]),move:n.useCallback((e,r)=>{const t=_();pe(t,e,r),D(t),B(pe,{argA:e,argB:r},t,!1)},[r]),prepend:n.useCallback((e,r)=>{const t=Array.isArray(e)?e:[e],n=Oe(_(),t);D(n),B(Oe,{argA:ke(e)},n),N(t),u.current=C(0,r)},[r]),append:n.useCallback((e,r)=>{const t=Array.isArray(e)?e:[e],n=xe(_(),t),s=n.length-t.length;D(n),B(xe,{argA:ke(e)},n,!1),N(t,s),u.current=C(s,r)},[r]),remove:n.useCallback(e=>{const t=ve(_(),e);(e=>{(Array.isArray(e)?e:[e]).forEach(e=>d(p.current,`${r}${s(e)?"":"."+e}`,s(e)?[]:void 0))})(e),D(t),B(ve,{argA:e},t)},[r]),insert:n.useCallback((e,r,t)=>{const n=Array.isArray(r)?r:[r],s=je(_(),e,n);D(s),B(je,{argA:e,argB:ke(r)},s),N(n,e),u.current=C(e,t)},[r]),fields:F}},e.useForm=function({mode:e=k,reValidateMode:r=j,resolver:t,context:u,defaultValues:i={},shouldFocusError:o=!0,criteriaMode:f}={}){const g=n.useRef({}),h=n.useRef(new Set),O=n.useRef(new ue),x=n.useRef(new ue),V=n.useRef(new ue),F=n.useRef(new ue),S=n.useRef({}),w=n.useRef(new Set),_=n.useRef(!1),C=n.useRef({}),D=n.useRef({}),E=n.useRef(i),B=n.useRef(!1),M=n.useRef(u),T=n.useRef(t),H=n.useRef(new Set),q=oe(e),z=f===R,[J,K]=n.useState({isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touchedFields:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!q.isOnSubmit,errors:{}}),X=n.useRef({isDirty:!ae,dirtyFields:!ae,touchedFields:!ae,isValidating:!ae,isValid:!ae,errors:!ae}),Y=n.useRef(J);M.current=u,T.current=t;const Z=()=>Y.current.isValid=m(D.current,C.current)&&p(Y.current.errors),ee=n.useCallback((e,r,t=!1,n={},u,i)=>{let a=t||(({errors:e,name:r,error:t,validFields:n,fieldsWithValidation:c})=>{const u=s(t),i=l(e,r);return u&&!!i||!u&&!m(i,t,!0)||u&&l(c,r)&&!l(n,r)})({errors:Y.current.errors,error:r,name:e,validFields:D.current,fieldsWithValidation:C.current});const o=l(Y.current.errors,e);if(r?(le(D.current,e),a=a||!o||!m(o,r,!0),d(Y.current.errors,e,r)):((l(C.current,e)||T.current)&&(d(D.current,e,!0),a=a||o),le(Y.current.errors,e)),a&&!c(t)||!p(n)||i){const e=Object.assign(Object.assign({},n),{isValid:T.current?!!u:Z(),errors:Y.current.errors});Y.current=Object.assign(Object.assign({},Y.current),e),O.current.next(i?{}:e)}O.current.next({isValidating:!1})},[]),se=n.useCallback((e,r,t={},n,s)=>{s&&Fe(e);const u=l(g.current,e,{})._f;if(u){const s=ie&&fe(u.ref)&&c(r)?"":r;if(u.value=r,I(u.ref)?(u.refs||[]).forEach(e=>e.checked=e.value===s):P(u.ref)&&!Q(s)?u.ref.files=s:L(u.ref)?[...u.ref.options].forEach(e=>e.selected=s.includes(e.value)):W(u.ref)&&u.refs?u.refs.length>1?u.refs.forEach(e=>e.checked=Array.isArray(s)?!!s.find(r=>r===e.value):s===e.value):u.refs[0].checked=!!s:u.ref.value=s,n){const t=$(g);d(t,e,r),V.current.next({values:Object.assign(Object.assign({},E.current),t),name:e})}t.shouldDirty&&ye(e),t.shouldValidate&&ve(e)}},[]),ce=n.useCallback((e,r)=>{if(X.current.isDirty){const t=$(g);return e&&r&&d(t,e,r),!m(t,E.current)}return!1},[]),ye=n.useCallback((e,r=!0)=>{if(X.current.isDirty||X.current.dirtyFields){const t=!m(l(E.current,e),G(l(g.current,e))),n=l(Y.current.dirtyFields,e),s=Y.current.isDirty;t?d(Y.current.dirtyFields,e,!0):le(Y.current.dirtyFields,e);const c={isDirty:ce(),dirtyFields:Y.current.dirtyFields},u=X.current.isDirty&&s!==c.isDirty||X.current.dirtyFields&&n!==l(Y.current.dirtyFields,e);return u&&r&&O.current.next(c),u?c:{}}return{}},[]),ge=n.useCallback(async(e,r)=>{const t=(await re(l(g.current,e),z))[e];return ee(e,t,r),s(t)},[z]),me=n.useCallback(async(e,r=[])=>{const{errors:t}=await T.current($(g,E),M.current,{criteriaMode:f,names:r,fields:de(h.current,g.current)});for(const r of e){const e=l(t,r);e?d(Y.current.errors,r,e):le(Y.current.errors,r)}return t},[f]),he=async e=>{for(const r in e){const t=e[r];if(t){const e=t._f,r=b(t,"_f");if(e){const r=await re(t,z);r[e.name]?(d(Y.current.errors,e.name,r[e.name]),le(D.current,e.name)):l(C.current,e.name)&&(d(D.current,e.name,!0),le(Y.current.errors,e.name))}r&&await he(r)}}},ve=n.useCallback(async e=>{const r=s(e)?Object.keys(g.current):Array.isArray(e)?e:[e];let t;O.current.next({isValidating:!0}),T.current?t=p(await me(r,s(e)?void 0:r)):s(e)?await he(g.current):await Promise.all(r.map(async e=>await ge(e,null))),O.current.next({errors:Y.current.errors,isValidating:!1,isValid:T.current?t:Z()})},[me,ge]),pe=n.useCallback((e,r,t)=>Object.entries(r).forEach(([r,n])=>{const s=`${e}.${r}`,c=l(g.current,s);c&&!c._f?pe(s,n,t):se(s,n,t,!0,!c)}),[ve]),Ae=e=>B.current||w.current.has(e)||w.current.has((e.match(/\w+/)||[])[0]),Oe=e=>{let r;const t=l(g.current,e);return!t||p(E.current)&&s(t._f.value)||(r=s(t._f.value)?l(E.current,e):t._f.value,s(r)||se(e,r)),r},xe=n.useCallback(async({type:e,target:t,target:{value:n,type:c}})=>{let u,i,a=t.name;const o=l(g.current,a);if(o){const b=c?G(o):n,y=e===A,{isOnBlur:m,isOnChange:h}=oe(r),v=(({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))(Object.assign({isBlurEvent:y,isTouched:!!l(Y.current.touchedFields,a),isSubmitted:Y.current.isSubmitted,isReValidateOnBlur:m,isReValidateOnChange:h},q)),j=!y&&Ae(a);s(b)||(o._f.value=b);const k=ye(a,!1);y&&!l(Y.current.touchedFields,a)&&(d(Y.current.touchedFields,a,!0),k.touchedFields=Y.current.touchedFields);let V=!p(k)||j;if(v)return!y&&x.current.next({name:a,type:e,value:b}),V&&O.current.next(j?{}:k);if(O.current.next({isValidating:!0}),T.current){const{errors:e}=await T.current($(g,E),M.current,{criteriaMode:f,fields:de([a],g.current),names:[a]}),r=Y.current.isValid;if(u=l(e,a),W(t)&&!u){const r=te(a),t=l(e,r,{});t.type&&t.message&&(u=t),(t||l(Y.current.errors,r))&&(a=r)}i=p(e),r!==i&&(V=!0)}else u=(await re(o,z))[a];!y&&x.current.next({name:a,type:e,value:b}),ee(a,u,V,k,i,j)}},[]),je=e=>{const r=_.current?$(g,E):E.current;return s(e)?r:Q(e)?l(r,e):e.map(e=>l(r,e))},ke=n.useCallback(async(e={})=>{const r=Y.current.isValid;if(t){const{errors:r}=await T.current(Object.assign(Object.assign({},$(g,E)),e),M.current,{criteriaMode:f,fields:de(h.current,g.current)});Y.current.isValid=p(r)}else Z();r!==Y.current.isValid&&O.current.next({isValid:Y.current.isValid})},[f]),Ve=n.useCallback((e,r,t)=>{const n=Array.isArray(e),c=_.current?je():s(r)?E.current:n?r||{}:{[e]:r};if(s(e))return t&&(B.current=!0),c;const u=[];for(const r of n?e:[e])t&&w.current.add(r),u.push(l(c,r));return n?u:u[0]},[]),Re=(e,r,t)=>{let n=l(g.current,e);if(n){const c=(e=>I(e)||W(e))(r);if((c?Array.isArray(n._f.refs)&&a(n._f.refs).find(e=>r.value===e.value&&e===r):r===n._f.ref)||!n)return;n={_f:c?Object.assign(Object.assign({},n._f),{refs:[...a(n._f.refs||[]).filter(e=>fe(e)&&document.contains(e)),r],ref:{type:r.type,name:e}}):Object.assign(Object.assign({},n._f),{ref:r})},d(g.current,e,n);const u=Oe(e);(c&&Array.isArray(u)?!m(l(g.current,e)._f.value,u):s(l(g.current,e)._f.value))&&(l(g.current,e)._f.value=G(l(g.current,e))),t&&!q.isOnSubmit&&n&&X.current.isValid&&re(n,z).then(r=>{p(r)?d(D.current,e,!0):le(D.current,e),Y.current.isValid&&!p(r)&&K(Object.assign(Object.assign({},Y.current),{isValid:Z()}))})}},Fe=n.useCallback((e,r)=>(d(g.current,e,{_f:Object.assign(Object.assign(Object.assign({},l(g.current,e)?Object.assign({ref:l(g.current,e)._f.ref},l(g.current,e)._f):{ref:{name:e}}),{name:e}),r)}),r&&d(C.current,e,!0),h.current.add(e),Oe(e),be?{name:e}:{name:e,onChange:xe,onBlur:xe,ref:t=>t&&Re(e,t,r)}),[E.current]),Se=n.useCallback((e,r)=>async t=>{t&&t.preventDefault&&(t.preventDefault(),t.persist());let n=Object.assign(Object.assign({},E.current),$(g,E));O.current.next({isSubmitting:!0});try{if(T.current){const{errors:e,values:r}=await T.current(n,M.current,{criteriaMode:f,fields:de(h.current,g.current)});Y.current.errors=e,n=r}else await he(g.current);p(Y.current.errors)&&Object.keys(Y.current.errors).every(e=>l(n,e))?(O.current.next({errors:{},isSubmitting:!0}),await e(n,t)):(r&&await r(Y.current.errors,t),o&&y(g.current,e=>l(Y.current.errors,e),h.current))}finally{Y.current.isSubmitted=!0,O.current.next({isSubmitted:!0,isSubmitting:!1,isSubmitSuccessful:p(Y.current.errors),submitCount:Y.current.submitCount+1,errors:Y.current.errors})}},[o,z,f]),we=n.useCallback(({keepErrors:e,keepDirty:r,keepIsSubmitted:t,keepTouched:n,keepIsValid:s,keepSubmitCount:c})=>{s||(D.current={},C.current={}),w.current=new Set,B.current=!1,O.current.next({submitCount:c?Y.current.submitCount:0,isDirty:!!r&&Y.current.isDirty,isSubmitted:!!t&&Y.current.isSubmitted,isValid:s?Y.current.isValid:!q.isOnSubmit,dirtyFields:r?Y.current.dirtyFields:{},touchedFields:n?Y.current.touchedFields:{},errors:e?Y.current.errors:{},isSubmitting:!1,isSubmitSuccessful:!1})},[]);return n.useEffect(()=>{_.current=!0;const e=O.current.subscribe({next(e={}){N(e,X.current,!0)&&(Y.current=Object.assign(Object.assign({},Y.current),e),K(Y.current))}}),r=F.current.subscribe({next(e){if(e.fields&&e.name&&X.current.isValid){const r=$(g);d(r,e.name,e.fields),ke(r)}}});return T.current&&X.current.isValid&&ke(),()=>{x.current.unsubscribe(),e.unsubscribe(),r.unsubscribe()}},[]),{control:n.useMemo(()=>({register:Fe,isWatchAllRef:B,watchFieldsRef:w,getFormIsDirty:ce,formStateSubjectRef:O,fieldArraySubjectRef:F,controllerSubjectRef:V,watchSubjectRef:x,watchInternal:Ve,fieldsRef:g,validFieldsRef:D,fieldsWithValidationRef:C,fieldArrayNamesRef:H,readFormStateRef:X,formStateRef:Y,defaultValuesRef:E,fieldArrayDefaultValuesRef:S}),[]),formState:ne(ae,J,X),trigger:ve,register:Fe,handleSubmit:Se,watch:n.useCallback((e,r)=>U(e)?x.current.subscribe({next:t=>e(Ve(void 0,r),t)}):Ve(e,r,!0),[]),setValue:n.useCallback((e,r,t={})=>{_.current=!0;const n=l(g.current,e),s=H.current.has(e);s&&(F.current.next({fields:r,name:e,isReset:!0}),(X.current.isDirty||X.current.dirtyFields)&&t.shouldDirty&&(d(Y.current.dirtyFields,e,v(r,l(E.current,e,[]),l(Y.current.dirtyFields,e,[]))),O.current.next({dirtyFields:Y.current.dirtyFields,isDirty:ce(e,r)}))),n&&!n._f?pe(e,r,s?{}:t):se(e,r,t,!0,!n),Ae(e)&&O.current.next({}),x.current.next({name:e,value:r})},[pe]),getValues:n.useCallback(je,[]),reset:n.useCallback((e,r={})=>{const t=e||E.current;if(ie&&!r.keepValues)for(const e of Object.values(g.current))if(e&&e._f){const r=Array.isArray(e._f.refs)?e._f.refs[0]:e._f.ref;if(fe(r))try{r.closest("form").reset();break}catch(e){}}!r.keepDefaultValues&&(E.current=Object.assign({},t)),r.keepValues||(g.current={},V.current.next({values:Object.assign({},t)}),x.current.next({value:Object.assign({},t)}),F.current.next({fields:Object.assign({},t),isReset:!0})),we(r)},[]),clearErrors:n.useCallback(e=>{e&&(Array.isArray(e)?e:[e]).forEach(e=>le(Y.current.errors,e)),O.current.next({errors:e?Y.current.errors:{}})},[]),unregister:n.useCallback((e,r={})=>{for(const t of e?Array.isArray(e)?e:[e]:Object.keys(h.current))h.current.delete(t),H.current.delete(t),l(g.current,t)&&(r.keepIsValid||(le(C.current,t),le(D.current,t)),!r.keepError&&le(Y.current.errors,t),!r.keepValue&&le(g.current,t),!r.keepDirty&&le(Y.current.dirtyFields,t),!r.keepTouched&&le(Y.current.touchedFields,t),!r.keepDefaultValue&&le(E.current,t),x.current.next({name:t}));O.current.next(Object.assign(Object.assign(Object.assign({},Y.current),r.keepDirty?{isDirty:ce()}:{}),T.current?{}:{isValid:Z()})),r.keepIsValid||ke()},[]),setError:n.useCallback((e,r,t)=>{const n=((l(g.current,e)||{_f:{}})._f||{}).ref;d(Y.current.errors,e,Object.assign(Object.assign({},r),{ref:n})),O.current.next({errors:Y.current.errors,isValid:!1}),t&&t.shouldFocus&&n&&n.focus&&n.focus()},[])}},e.useFormContext=ge,e.useFormState=Ve,e.useWatch=function(e){const{control:r,name:t,defaultValue:c}=e||{},u=ge(),{watchInternal:i,watchSubjectRef:a}=r||u.control,[l,o]=n.useState(s(c)?i(t):c);return n.useEffect(()=>{i(t);const e=a.current.subscribe({next:({name:e,value:r})=>{(!t||!e||(Array.isArray(t)?t:[t]).some(r=>e&&r&&e.startsWith(r)))&&o(Q(e)&&t===e&&!s(r)?r:i(t,c))}});return()=>e.unsubscribe()},[t]),l},Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !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";function t(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var n=t(r),s=e=>"checkbox"===e.type,c=e=>null==e;const u=e=>"object"==typeof e;var i=e=>!c(e)&&!Array.isArray(e)&&u(e)&&!(e instanceof Date),a=e=>e.substring(0,e.search(/.\d/))||e,l=e=>e.filter(Boolean),o=e=>void 0===e,f=(e={},r,t)=>{const n=l(r.split(/[,[\].]+?/)).reduce((e,r)=>c(e)?e:e[r],e);return o(n)||n===e?o(e[r])?t:e[r]:n};const d="blur",b="change",y="onBlur",g="onChange",m="onSubmit",h="onTouched",v="all",p="undefined",A="max",O="min",x="maxLength",j="minLength",k="pattern",V="required",R="validate";var F=(e,r)=>{const t=Object.assign({},e);return delete t[r],t};const S=n.createContext(null);S.displayName="RHFContext";const w=()=>n.useContext(S);var _=(e,r,t,n,s=!0)=>e?new Proxy(r,{get:(e,r)=>{if(r in e)return t.current[r]!==v&&(t.current[r]=!s||v),n&&(n.current[r]=!0),e[r]}}):r,C=e=>i(e)&&!Object.keys(e).length,D=(e,r,t)=>C(e)||Object.keys(e).length>=Object.keys(r).length||Object.keys(e).find(e=>r[e]===(!t||v)),E=typeof window!==p&&typeof window.HTMLElement!==p&&typeof document!==p;const B=E?"Proxy"in window:typeof Proxy!==p;function N(e){const r=w(),{formStateRef:t,formStateSubjectRef:s,readFormStateRef:c}=e&&e.control||r.control,[u,i]=n.useState(t.current),a=n.useRef({isDirty:!1,dirtyFields:!1,touchedFields:!1,isValidating:!1,isValid:!1,errors:!1});return n.useEffect(()=>{const e=s.current.subscribe({next:e=>{D(e,a.current)&&i(Object.assign(Object.assign({},t.current),e))}});return()=>e.unsubscribe()},[]),_(B,u,c,a,!1)}function $({name:e,rules:r,defaultValue:t,control:c}){const u=w(),{defaultValuesRef:l,register:y,fieldsRef:g,fieldArrayNamesRef:m,controllerSubjectRef:h}=c||u.control,{onChange:v,onBlur:p,ref:A}=y(e,r),[O,x]=n.useState(o(f(g.current,e)._f.value)||((e,r)=>[...e].some(e=>a(r)===e))(m.current,e)?o(t)?f(l.current,e):t:f(g.current,e)._f.value),j=N({control:c||u.control});return f(g.current,e)._f.value=O,n.useEffect(()=>{const r=h.current.subscribe({next:r=>(!r.name||e===r.name)&&x(f(r.values,e))});return A({target:O}),()=>r.unsubscribe()},[e]),{field:{onChange:r=>{const t=(e=>i(e)&&e.target?s(e.target)?e.target.checked:e.target.value:e)(r);x(t),v({target:{value:t,name:e},type:b})},onBlur:()=>{p({target:{name:e},type:d})},name:e,value:O,ref:A},formState:j,fieldState:Object.defineProperties({},{invalid:{get:()=>!!f(j.errors,e)},isDirty:{get:()=>!!f(j.dirtyFields,e)},isTouched:{get:()=>!!f(j.touchedFields,e)},error:{get:()=>f(j.errors,e)}})}}var M=(e,r,t,n,s)=>r?Object.assign(Object.assign({},t[e]),{types:Object.assign(Object.assign({},t[e]&&t[e].types?t[e].types:{}),{[n]:s||!0})}):{},T=e=>/^\w*$/.test(e),I=e=>l(e.replace(/["|']|\]/g,"").split(/\.|\[/));function P(e,r,t){let n=-1;const s=T(r)?[r]:I(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=i(t)||Array.isArray(t)?t:isNaN(+s[n+1])?{}:[]}e[r]=c,e=e[r]}return e}const W=(e,r,t)=>{for(const n of t||Object.keys(e)){const t=f(e,n);if(t){const e=t._f,n=F(t,"_f");if(e&&r(e.name)){if(e.ref.focus&&o(e.ref.focus()))break;if(e.refs){e.refs[0].focus();break}}else i(n)&&W(n,r)}}},L=(e,r={current:{}},t={})=>{for(const n in e.current){const s=e.current[n];if(s){const e=s._f,c=F(s,"_f");P(t,n,e?e.ref.disabled||e.refs&&e.refs.every(e=>e.disabled)?void 0:e.value:Array.isArray(s)?[]:{}),c&&L({current:c},r,t[n])}}return Object.assign(Object.assign({},r.current),t)};var H=()=>{const e=typeof performance===p?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{const t=(16*Math.random()+e)%16|0;return("x"==r?t:3&t|8).toString(16)})},q=(e=[],r)=>e.map(e=>Object.assign({[r]:e&&e[r]||H()},e)),z=e=>c(e)||!u(e);function G(e,r,t){if(z(e)||z(r)||e instanceof Date||r instanceof Date)return e===r;if(!n.isValidElement(e)){const n=Object.keys(e),s=Object.keys(r);if(n.length!==s.length)return!1;for(const s of n){const n=e[s];if(!t||"ref"!==s){const e=r[s];if((i(n)||Array.isArray(n))&&(i(e)||Array.isArray(e))?!G(n,e,t):n!==e)return!1}}}return!0}function J(e,r,t,n,s){let c=-1;for(;++c<e.length;){for(const n in e[c])Array.isArray(e[c][n])?(!t[c]&&(t[c]={}),t[c][n]=[],J(e[c][n],f(r[c]||{},n,[]),t[c][n],t[c],n)):G(f(r[c]||{},n),e[c][n])?P(t[c]||{},n):t[c]=Object.assign(Object.assign({},t[c]),{[n]:!0});n&&!t.length&&delete n[s]}return t}var K=(e,r,t)=>function e(r,t){if(z(r)||z(t))return t;for(const n in t){const s=r[n],c=t[n];try{r[n]=i(s)&&i(c)||Array.isArray(s)&&Array.isArray(c)?e(s,c):c}catch(e){}}return r}(J(e,r,t.slice(0,e.length)),J(r,e,t.slice(0,e.length)));function Q(e,r){return[...e,...Array.isArray(r)?r:[r]]}var U=e=>Array.isArray(e)?Array(e.length).fill(void 0):void 0;function X(e,r,t){return[...e.slice(0,r),...Array.isArray(t)?t:[t],...e.slice(r)]}var Y=(e,r,t)=>Array.isArray(e)?(o(e[t])&&(e[t]=void 0),e.splice(t,0,e.splice(r,1)[0]),e):[];function Z(e,r){return[...Array.isArray(r)?r:[r],...e]}var ee=(e,r)=>o(r)?[]:function(e,r){let t=0;const n=[...e];for(const e of r)n.splice(e-t,1),t++;return l(n).length?n:[]}(e,(Array.isArray(r)?r:[r]).sort((e,r)=>e-r)),re=(e,r,t)=>{e[r]=[e[t],e[t]=e[r]][0]},te=e=>"boolean"==typeof e;function ne(e,r){const t=T(r)?[r]:I(r),n=1==t.length?e:function(e,r){const t=r.slice(0,-1).length;let n=0;for(;n<t;)e=o(e)?n++:e[r[n++]];return e}(e,t),s=t[t.length-1];let c;n&&delete n[s];for(let r=0;r<t.slice(0,-1).length;r++){let n,s=-1;const u=t.slice(0,-(r+1)),a=u.length-1;for(r>0&&(c=e);++s<u.length;){const r=u[s];n=n?n[r]:e[r],a===s&&(i(n)&&C(n)||Array.isArray(n)&&!n.filter(e=>i(e)&&!C(e)||te(e)).length)&&(c?delete c[r]:delete e[r]),c=n}}return e}function se(e,r){const t={};for(const n of e){const e=f(r,n);e&&(T(n)?t[n]=e._f:P(t,n,e._f))}return t}var ce=e=>"file"===e.type,ue=e=>"select-multiple"===e.type,ie=e=>"radio"===e.type;const ae={value:!1,isValid:!1},le={value:!0,isValid:!0};var oe=e=>{if(Array.isArray(e)){if(e.length>1){const r=e.filter(e=>e&&e.checked&&!e.disabled).map(e=>e.value);return{value:r,isValid:!!r.length}}return e[0].checked&&!e[0].disabled?e[0].attributes&&!o(e[0].attributes.value)?o(e[0].value)||""===e[0].value?le:{value:e[0].value,isValid:!0}:le:ae}return ae};const fe={isValid:!1,value:null};var de=e=>Array.isArray(e)?e.reduce((e,r)=>r&&r.checked&&!r.disabled?{isValid:!0,value:r.value}:e,fe):fe;function be(e){if(e&&e._f){const t=e._f.ref;if(t.disabled)return;return ce(t)?t.files:ie(t)?de(e._f.refs).value:ue(t)?(r=t.options,[...r].filter(({selected:e})=>e).map(({value:e})=>e)):s(t)?oe(e._f.refs).value:((e,{valueAsNumber:r,valueAsDate:t,setValueAs:n})=>r?""===e?NaN:+e:t?new Date(e):n?n(e):e)(o(t.value)?e._f.ref.value:t.value,e._f)}var r}var ye=e=>"function"==typeof e,ge=e=>"string"==typeof e,me=e=>ge(e)||n.isValidElement(e),he=e=>e instanceof RegExp;function ve(e,r,t="validate"){if(me(e)||te(e)&&!e)return{type:t,message:me(e)?e:"",ref:r}}var pe=e=>i(e)&&!he(e)?e:{value:e,message:""},Ae=async({_f:{ref:e,refs:r,required:t,maxLength:n,minLength:u,min:a,max:l,pattern:o,validate:f,name:d,value:b}},y)=>{const g={},m=ie(e),h=s(e),v=m||h,p=""===b||Array.isArray(b)&&!b.length,F=M.bind(null,d,y,g),S=(r,t,n,s=x,c=j)=>{const u=r?t:n;g[d]=Object.assign({type:r?s:c,message:u,ref:e},F(r?s:c,u))};if(t&&(!m&&!h&&(p||c(b))||te(b)&&!b||h&&!oe(r).isValid||m&&!de(r).isValid)){const{value:n,message:s}=me(t)?{value:!!t,message:t}:pe(t);if(n&&(g[d]=Object.assign({type:V,message:s,ref:v?(r||[])[0]||{}:e},F(V,s)),!y))return g}if(!(c(a)&&c(l)||""===b)){let r,t;const n=pe(l),s=pe(a);if(isNaN(b)){const c=e.valueAsDate||new Date(b);ge(n.value)&&(r=c>new Date(n.value)),ge(s.value)&&(t=c<new Date(s.value))}else{const u=e.valueAsNumber||parseFloat(b);c(n.value)||(r=u>n.value),c(s.value)||(t=u<s.value)}if((r||t)&&(S(!!r,n.message,s.message,A,O),!y))return g}if(ge(b)&&!p&&(n||u)){const e=pe(n),r=pe(u),t=!c(e.value)&&b.length>e.value,s=!c(r.value)&&b.length<r.value;if((t||s)&&(S(t,e.message,r.message),!y))return g}if(ge(b)&&o&&!p){const{value:r,message:t}=pe(o);if(he(r)&&!r.test(b)&&(g[d]=Object.assign({type:k,message:t,ref:e},F(k,t)),!y))return g}if(f){const t=v&&r?r[0]:e;if(ye(f)){const e=ve(await f(b),t);if(e&&(g[d]=Object.assign(Object.assign({},e),F(R,e.message)),!y))return g}else if(i(f)){let e={};for(const[r,n]of Object.entries(f)){if(!C(e)&&!y)break;const s=ve(await n(b),t,r);s&&(e=Object.assign(Object.assign({},s),F(r,s.message)),y&&(g[d]=e))}if(!C(e)&&(g[d]=Object.assign({ref:t},e),!y))return g}}return g},Oe=e=>({isOnSubmit:!e||e===m,isOnBlur:e===y,isOnChange:e===g,isOnAll:e===v,isOnTouch:e===h}),xe=e=>e instanceof HTMLElement;class je{constructor(){this.tearDowns=[]}add(e){this.tearDowns.push(e)}unsubscribe(){for(const e of this.tearDowns)e();this.tearDowns=[]}}class ke{constructor(e,r){this.observer=e,this.closed=!1,r.add(()=>this.closed=!0)}next(e){this.closed||this.observer.next(e)}}class Ve{constructor(){this.observers=[]}next(e){for(const r of this.observers)r.next(e)}subscribe(e){const r=new je,t=new ke(e,r);return this.observers.push(t),r}unsubscribe(){this.observers=[]}}const Re=typeof window===p;e.Controller=e=>e.render($(e)),e.FormProvider=e=>n.createElement(S.Provider,{value:F(e,"children")},e.children),e.appendErrors=M,e.get=f,e.set=P,e.useController=$,e.useFieldArray=({control:e,name:r,keyName:t="id"})=>{const s=w(),c=n.useRef(""),{isWatchAllRef:u,watchFieldsRef:i,getFormIsDirty:d,watchSubjectRef:b,fieldArraySubjectRef:y,fieldArrayNamesRef:g,fieldsRef:m,defaultValuesRef:h,formStateRef:v,formStateSubjectRef:p,readFormStateRef:A,validFieldsRef:O,fieldsWithValidationRef:x,fieldArrayDefaultValuesRef:j}=e||s.control,[k,V]=n.useState(q(f(j.current,a(r))?f(j.current,r,[]):f(h.current,r,[]),t));P(j.current,r,[...k]),g.current.add(r);const R=e=>e.map(e=>F(e||{},t)),S=()=>{const e=f(L(m,h),r,[]);return q(f(j.current,r,[]).map((r,t)=>Object.assign(Object.assign({},r),e[t])),t)},_=(e,t)=>t?o(t.focusIndex)?t.focusName?t.focusName:t.shouldFocus?`${r}.${e}`:"":`${r}.${t.focusIndex}`:`${r}.${e}`,C=(e=[])=>V(q(e,t)),D=e=>!l(f(e,r,[])).length&&ne(e,r),E=(e,t,n=[],s=!0)=>{if(f(m.current,r)){const n=e(f(m.current,r),t.argA,t.argB);s&&P(m.current,r,n)}if(Array.isArray(f(v.current.errors,r))){const n=e(f(v.current.errors,r),t.argA,t.argB);s&&P(v.current.errors,r,n),D(v.current.errors)}if(A.current.touchedFields&&f(v.current.touchedFields,r)){const n=e(f(v.current.touchedFields,r),t.argA,t.argB);s&&P(v.current.touchedFields,r,n),D(v.current.touchedFields)}(A.current.dirtyFields||A.current.isDirty)&&(P(v.current.dirtyFields,r,K(R(n),f(h.current,r,[]),f(v.current.dirtyFields,r,[]))),(e=>{e&&P(v.current.dirtyFields,r,K(R(e),f(h.current,r,[]),f(v.current.dirtyFields,r,[])))})(n),D(v.current.dirtyFields)),A.current.isValid&&(P(O.current,r,e(f(O.current,r,[]),t.argA)),D(O.current),P(x.current,r,e(f(x.current,r,[]),t.argA)),D(x.current)),p.current.next({isDirty:d(r,R(n)),errors:v.current.errors,isValid:v.current.isValid})},B=(e,t=0,n="")=>e.forEach((e,s)=>Object.entries(e).forEach(([e,c])=>{const u=`${n||r}.${n?s:t+s}.${e}`;Array.isArray(c)?B(c,s,u):P(m.current,u,{_f:{ref:{name:u},name:u,value:c}})}));return n.useEffect(()=>{if(u.current)p.current.next({});else for(const e of i.current)if(r.startsWith(e)){p.current.next({});break}b.current.next({name:r,value:f(L(m,h),r,[])}),c.current&&W(m.current,e=>e.startsWith(c.current)),c.current="",y.current.next({name:r,fields:R([...k])})},[k,r]),n.useEffect(()=>{const e=y.current.subscribe({next({name:e,fields:t,isReset:n}){n&&(ne(m.current,e||r),e?P(j.current,e,t):j.current=t,C(f(j.current,r)))}});return!f(m.current,r)&&P(m.current,r,[]),()=>{j.current=L(m),e.unsubscribe()}},[]),{swap:n.useCallback((e,r)=>{const t=S();re(t,e,r),E(re,{argA:e,argB:r},t,!1),C(t)},[r]),move:n.useCallback((e,r)=>{const t=S();Y(t,e,r),C(t),E(Y,{argA:e,argB:r},t,!1)},[r]),prepend:n.useCallback((e,r)=>{const t=Array.isArray(e)?e:[e],n=Z(S(),t);C(n),E(Z,{argA:U(e)},n),B(t),c.current=_(0,r)},[r]),append:n.useCallback((e,r)=>{const t=Array.isArray(e)?e:[e],n=Q(S(),t),s=n.length-t.length;C(n),E(Q,{argA:U(e)},n,!1),B(t,s),c.current=_(s,r)},[r]),remove:n.useCallback(e=>{const t=ee(S(),e);(e=>{(Array.isArray(e)?e:[e]).forEach(e=>P(m.current,`${r}${o(e)?"":"."+e}`,o(e)?[]:void 0))})(e),C(t),E(ee,{argA:e},t)},[r]),insert:n.useCallback((e,r,t)=>{const n=Array.isArray(r)?r:[r],s=X(S(),e,n);C(s),E(X,{argA:e,argB:U(r)},s),B(n,e),c.current=_(e,t)},[r]),fields:k}},e.useForm=function({mode:e=m,reValidateMode:r=g,resolver:t,context:u,defaultValues:i={},shouldFocusError:b=!0,criteriaMode:y}={}){const h=n.useRef({}),p=n.useRef(new Set),A=n.useRef(new Ve),O=n.useRef(new Ve),x=n.useRef(new Ve),j=n.useRef(new Ve),k=n.useRef({}),V=n.useRef(new Set),R=n.useRef(!1),S=n.useRef({}),w=n.useRef({}),N=n.useRef(i),$=n.useRef(!1),M=n.useRef(u),T=n.useRef(t),I=n.useRef(new Set),H=Oe(e),q=y===v,[z,J]=n.useState({isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touchedFields:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!H.isOnSubmit,errors:{}}),Q=n.useRef({isDirty:!B,dirtyFields:!B,touchedFields:!B,isValidating:!B,isValid:!B,errors:!B}),U=n.useRef(z);M.current=u,T.current=t;const X=()=>U.current.isValid=G(w.current,S.current)&&C(U.current.errors),Y=n.useCallback((e,r,t=!1,n={},s,u)=>{let i=t||(({errors:e,name:r,error:t,validFields:n,fieldsWithValidation:s})=>{const c=o(t),u=f(e,r);return c&&!!u||!c&&!G(u,t,!0)||c&&f(s,r)&&!f(n,r)})({errors:U.current.errors,error:r,name:e,validFields:w.current,fieldsWithValidation:S.current});const a=f(U.current.errors,e);if(r?(ne(w.current,e),i=i||!a||!G(a,r,!0),P(U.current.errors,e,r)):((f(S.current,e)||T.current)&&(P(w.current,e,!0),i=i||a),ne(U.current.errors,e)),i&&!c(t)||!C(n)||u){const e=Object.assign(Object.assign({},n),{isValid:T.current?!!s:X(),errors:U.current.errors});U.current=Object.assign(Object.assign({},U.current),e),A.current.next(u?{}:e)}A.current.next({isValidating:!1})},[]),Z=n.useCallback((e,r,t={},n,u)=>{u&&Fe(e);const i=f(h.current,e,{})._f;if(i){const u=E&&xe(i.ref)&&c(r)?"":r;if(i.value=r,ie(i.ref)?(i.refs||[]).forEach(e=>e.checked=e.value===u):ce(i.ref)&&!ge(u)?i.ref.files=u:ue(i.ref)?[...i.ref.options].forEach(e=>e.selected=u.includes(e.value)):s(i.ref)&&i.refs?i.refs.length>1?i.refs.forEach(e=>e.checked=Array.isArray(u)?!!u.find(r=>r===e.value):u===e.value):i.refs[0].checked=!!u:i.ref.value=u,n){const t=L(h);P(t,e,r),x.current.next({values:Object.assign(Object.assign({},N.current),t),name:e})}t.shouldDirty&&re(e,u),t.shouldValidate&&oe(e)}},[]),ee=n.useCallback((e,r)=>{if(Q.current.isDirty){const t=L(h);return e&&r&&P(t,e,r),!G(t,N.current)}return!1},[]),re=n.useCallback((e,r,t=!0)=>{if(Q.current.isDirty||Q.current.dirtyFields){const n=!G(f(N.current,e),r),s=f(U.current.dirtyFields,e),c=U.current.isDirty;n?P(U.current.dirtyFields,e,!0):ne(U.current.dirtyFields,e),U.current.isDirty=ee();const u={isDirty:U.current.isDirty,dirtyFields:U.current.dirtyFields},i=Q.current.isDirty&&c!==u.isDirty||Q.current.dirtyFields&&s!==f(U.current.dirtyFields,e);return i&&t&&A.current.next(u),i?u:{}}return{}},[]),te=n.useCallback(async(e,r)=>{const t=(await Ae(f(h.current,e),q))[e];return Y(e,t,r),o(t)},[q]),ae=n.useCallback(async(e,r=[])=>{const{errors:t}=await T.current(L(h,N),M.current,{criteriaMode:y,names:r,fields:se(p.current,h.current)});for(const r of e){const e=f(t,r);e?P(U.current.errors,r,e):ne(U.current.errors,r)}return t},[y]),le=async e=>{for(const r in e){const t=e[r];if(t){const e=t._f,r=F(t,"_f");if(e){const r=await Ae(t,q);r[e.name]?(P(U.current.errors,e.name,r[e.name]),ne(w.current,e.name)):f(S.current,e.name)&&(P(w.current,e.name,!0),ne(U.current.errors,e.name))}r&&await le(r)}}},oe=n.useCallback(async e=>{const r=o(e)?Object.keys(h.current):Array.isArray(e)?e:[e];let t;A.current.next({isValidating:!0}),T.current?t=C(await ae(r,o(e)?void 0:r)):o(e)?await le(h.current):await Promise.all(r.map(async e=>await te(e,null))),A.current.next({errors:U.current.errors,isValidating:!1,isValid:T.current?t:X()})},[ae,te]),fe=n.useCallback((e,r,t)=>Object.entries(r).forEach(([r,n])=>{const s=`${e}.${r}`,c=f(h.current,s);c&&!c._f?fe(s,n,t):Z(s,n,t,!0,!c)}),[oe]),de=e=>$.current||V.current.has(e)||V.current.has((e.match(/\w+/)||[])[0]),me=e=>{let r;const t=f(h.current,e);return!t||C(N.current)&&o(t._f.value)||(r=o(t._f.value)?f(N.current,e):t._f.value,o(r)||Z(e,r)),r},he=n.useCallback(async({type:e,target:t,target:{value:n,type:c}})=>{let u,i,l=t.name;const b=f(h.current,l);if(b){const g=c?be(b):n,m=e===d,{isOnBlur:v,isOnChange:p}=Oe(r),x=(({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))(Object.assign({isBlurEvent:m,isTouched:!!f(U.current.touchedFields,l),isSubmitted:U.current.isSubmitted,isReValidateOnBlur:v,isReValidateOnChange:p},H)),j=!m&&de(l);o(g)||(b._f.value=g);const k=re(l,b._f.value,!1);m&&!f(U.current.touchedFields,l)&&(P(U.current.touchedFields,l,!0),k.touchedFields=U.current.touchedFields);let V=!C(k)||j;if(x)return!m&&O.current.next({name:l,type:e,value:g}),V&&A.current.next(j?{}:k);if(A.current.next({isValidating:!0}),T.current){const{errors:e}=await T.current(L(h,N),M.current,{criteriaMode:y,fields:se([l],h.current),names:[l]}),r=U.current.isValid;if(u=f(e,l),s(t)&&!u){const r=a(l),t=f(e,r,{});t.type&&t.message&&(u=t),(t||f(U.current.errors,r))&&(l=r)}i=C(e),r!==i&&(V=!0)}else u=(await Ae(b,q))[l];!m&&O.current.next({name:l,type:e,value:g}),Y(l,u,V,k,i,j)}},[]),ve=e=>{const r=R.current?L(h,N):N.current;return o(e)?r:ge(e)?f(r,e):e.map(e=>f(r,e))},pe=n.useCallback(async(e={})=>{const r=U.current.isValid;if(t){const{errors:r}=await T.current(Object.assign(Object.assign({},L(h,N)),e),M.current,{criteriaMode:y,fields:se(p.current,h.current)});U.current.isValid=C(r)}else X();r!==U.current.isValid&&A.current.next({isValid:U.current.isValid})},[y]),je=n.useCallback((e,r,t)=>{const n=Array.isArray(e),s=R.current?ve():o(r)?N.current:n?r||{}:{[e]:r};if(o(e))return t&&($.current=!0),s;const c=[];for(const r of n?e:[e])t&&V.current.add(r),c.push(f(s,r));return n?c:c[0]},[]),ke=(e,r,t)=>{let n=f(h.current,e);if(n){const c=(e=>ie(e)||s(e))(r);if((c?Array.isArray(n._f.refs)&&l(n._f.refs).find(e=>r.value===e.value&&e===r):r===n._f.ref)||!n||E&&xe(n._f.ref)&&!xe(r))return;n={_f:c?Object.assign(Object.assign({},n._f),{refs:[...l(n._f.refs||[]).filter(e=>xe(e)&&document.contains(e)),r],ref:{type:r.type,name:e}}):Object.assign(Object.assign({},n._f),{ref:r})},P(h.current,e,n);const u=me(e);(c&&Array.isArray(u)?!G(f(h.current,e)._f.value,u):o(f(h.current,e)._f.value))&&(f(h.current,e)._f.value=be(f(h.current,e))),t&&!H.isOnSubmit&&n&&Q.current.isValid&&Ae(n,q).then(r=>{C(r)?P(w.current,e,!0):ne(w.current,e),U.current.isValid&&!C(r)&&J(Object.assign(Object.assign({},U.current),{isValid:X()}))})}},Fe=n.useCallback((e,r)=>{const t=!f(h.current,e);return P(h.current,e,{_f:Object.assign(Object.assign(Object.assign({},t?{ref:{name:e}}:Object.assign({ref:(f(h.current,e)._f||{}).ref},f(h.current,e)._f)),{name:e}),r)}),r&&P(S.current,e,!0),p.current.add(e),t&&me(e),Re?{name:e}:{name:e,onChange:he,onBlur:he,ref:t=>t&&ke(e,t,r)}},[N.current]),Se=n.useCallback((e,r)=>async t=>{t&&t.preventDefault&&(t.preventDefault(),t.persist());let n=Object.assign(Object.assign({},N.current),L(h,N));A.current.next({isSubmitting:!0});try{if(T.current){const{errors:e,values:r}=await T.current(n,M.current,{criteriaMode:y,fields:se(p.current,h.current)});U.current.errors=e,n=r}else await le(h.current);C(U.current.errors)&&Object.keys(U.current.errors).every(e=>f(n,e))?(A.current.next({errors:{},isSubmitting:!0}),await e(n,t)):(r&&await r(U.current.errors,t),b&&W(h.current,e=>f(U.current.errors,e),p.current))}finally{U.current.isSubmitted=!0,A.current.next({isSubmitted:!0,isSubmitting:!1,isSubmitSuccessful:C(U.current.errors),submitCount:U.current.submitCount+1,errors:U.current.errors})}},[b,q,y]),we=n.useCallback(({keepErrors:e,keepDirty:r,keepIsSubmitted:t,keepTouched:n,keepIsValid:s,keepSubmitCount:c})=>{s||(w.current={},S.current={}),V.current=new Set,$.current=!1,A.current.next({submitCount:c?U.current.submitCount:0,isDirty:!!r&&U.current.isDirty,isSubmitted:!!t&&U.current.isSubmitted,isValid:s?U.current.isValid:!H.isOnSubmit,dirtyFields:r?U.current.dirtyFields:{},touchedFields:n?U.current.touchedFields:{},errors:e?U.current.errors:{},isSubmitting:!1,isSubmitSuccessful:!1})},[]);return n.useEffect(()=>{R.current=!0;const e=A.current.subscribe({next(e={}){D(e,Q.current,!0)&&(U.current=Object.assign(Object.assign({},U.current),e),J(U.current))}}),r=j.current.subscribe({next(e){if(e.fields&&e.name&&Q.current.isValid){const r=L(h);P(r,e.name,e.fields),pe(r)}}});return T.current&&Q.current.isValid&&pe(),()=>{O.current.unsubscribe(),e.unsubscribe(),r.unsubscribe()}},[]),{control:n.useMemo(()=>({register:Fe,isWatchAllRef:$,watchFieldsRef:V,getFormIsDirty:ee,formStateSubjectRef:A,fieldArraySubjectRef:j,controllerSubjectRef:x,watchSubjectRef:O,watchInternal:je,fieldsRef:h,validFieldsRef:w,fieldsWithValidationRef:S,fieldArrayNamesRef:I,readFormStateRef:Q,formStateRef:U,defaultValuesRef:N,fieldArrayDefaultValuesRef:k}),[]),formState:_(B,z,Q),trigger:oe,register:Fe,handleSubmit:Se,watch:n.useCallback((e,r)=>ye(e)?O.current.subscribe({next:t=>e(je(void 0,r),t)}):je(e,r,!0),[]),setValue:n.useCallback((e,r,t={})=>{R.current=!0;const n=f(h.current,e),s=I.current.has(e);s&&(j.current.next({fields:r,name:e,isReset:!0}),(Q.current.isDirty||Q.current.dirtyFields)&&t.shouldDirty&&(P(U.current.dirtyFields,e,K(r,f(N.current,e,[]),f(U.current.dirtyFields,e,[]))),A.current.next({dirtyFields:U.current.dirtyFields,isDirty:ee(e,r)})),!r.length&&P(h.current,e,[])&&P(k.current,e,[])),n&&!n._f||s?fe(e,r,s?{}:t):Z(e,r,t,!0,!n),de(e)&&A.current.next({}),O.current.next({name:e,value:r})},[fe]),getValues:n.useCallback(ve,[]),reset:n.useCallback((e,r={})=>{const t=e||N.current;if(E&&!r.keepValues)for(const e of Object.values(h.current))if(e&&e._f){const r=Array.isArray(e._f.refs)?e._f.refs[0]:e._f.ref;if(xe(r))try{r.closest("form").reset();break}catch(e){}}!r.keepDefaultValues&&(N.current=Object.assign({},t)),r.keepValues||(h.current={},x.current.next({values:Object.assign({},t)}),O.current.next({value:Object.assign({},t)}),j.current.next({fields:Object.assign({},t),isReset:!0})),we(r)},[]),clearErrors:n.useCallback(e=>{e&&(Array.isArray(e)?e:[e]).forEach(e=>ne(U.current.errors,e)),A.current.next({errors:e?U.current.errors:{}})},[]),unregister:n.useCallback((e,r={})=>{for(const t of e?Array.isArray(e)?e:[e]:Object.keys(p.current))p.current.delete(t),I.current.delete(t),f(h.current,t)&&(r.keepIsValid||(ne(S.current,t),ne(w.current,t)),!r.keepError&&ne(U.current.errors,t),!r.keepValue&&ne(h.current,t),!r.keepDirty&&ne(U.current.dirtyFields,t),!r.keepTouched&&ne(U.current.touchedFields,t),!r.keepDefaultValue&&ne(N.current,t),O.current.next({name:t}));A.current.next(Object.assign(Object.assign(Object.assign({},U.current),r.keepDirty?{isDirty:ee()}:{}),T.current?{}:{isValid:X()})),r.keepIsValid||pe()},[]),setError:n.useCallback((e,r,t)=>{const n=((f(h.current,e)||{_f:{}})._f||{}).ref;P(U.current.errors,e,Object.assign(Object.assign({},r),{ref:n})),A.current.next({errors:U.current.errors,isValid:!1}),t&&t.shouldFocus&&n&&n.focus&&n.focus()},[])}},e.useFormContext=w,e.useFormState=N,e.useWatch=function(e){const{control:r,name:t,defaultValue:s}=e||{},c=w(),{watchInternal:u,watchSubjectRef:i}=r||c.control,[a,l]=n.useState(o(s)?u(t):s);return n.useEffect(()=>{u(t);const e=i.current.subscribe({next:({name:e,value:r})=>{(!t||!e||(Array.isArray(t)?t:[t]).some(r=>e&&r&&e.startsWith(r)))&&l(ge(e)&&t===e&&!o(r)?r:u(t,s))}});return()=>e.unsubscribe()},[t]),a},Object.defineProperty(e,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=index.umd.production.min.js.map