react-hook-form 7.4.3-beta.4 → 7.5.2

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