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