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/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Changelog
2
2
 
3
- ## [7.4.0] - 2021-05-4
3
+ ## [7.5.0] - 2021-05-09
4
+
5
+ ### Changed
6
+
7
+ - `isSubmitSuccessful` will return false when `handleSubmit` callback failed with `Error` or `Promise` reject.
8
+ - unmounted input will no longer get validated even with `shouldUnregister: false`
9
+
10
+ ## [7.4.0] - 2021-05-04
4
11
 
5
12
  ### Added
6
13
 
@@ -8,8 +15,8 @@
8
15
 
9
16
  ```ts
10
17
  useFormState({
11
- name: 'inputName' // optional and can be array of inputs' name as well
12
- })
18
+ name: 'inputName', // optional and can be array of inputs' name as well
19
+ });
13
20
  ```
14
21
 
15
22
  ## [7.2.2] - 2021-04-21
@@ -30,7 +37,7 @@ useForm({
30
37
  shouldUnregister: true // default to false
31
38
  })
32
39
 
33
- // Component/Hook level config (can not overwrites global cocnfig)
40
+ // Component/Hook level config (can not overwrites global config)
34
41
  register('test', {
35
42
  shouldUnregister: true // default to false
36
43
  })
package/README.md CHANGED
@@ -264,6 +264,12 @@ Thanks go to these kind and lovely sponsors (companies and individuals)!
264
264
  width="45"
265
265
  height="45"
266
266
  /></a>
267
+ <a href="https://github.com/artischockee"
268
+ ><img
269
+ src="https://avatars.githubusercontent.com/u/22125223?v=4"
270
+ width="45"
271
+ height="45"
272
+ /></a>
267
273
  </p>
268
274
 
269
275
  ## 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 _=(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,S=e=>c(e)&&!Object.keys(e).length,w=(e,r,t)=>{const n=V(e,"name");return S(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()},[]),_(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 t};var W=(e,r)=>{const t=U(e,r);return r&&r.current?Object.assign(Object.assign({},r.current),t):t},L=()=>{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)})},q=(e=[],r)=>e.map(e=>Object.assign({[r]:e&&e[r]||L()},e)),H=e=>n(e)||!s(e);function z(e,t,n){if(H(e)||H(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))?!z(r,e,n):r!==e)return!1}}}return!0}function G(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]=[],G(e[c][n],o(r[c]||{},n,[]),t[c][n],t[c],n)):z(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 J=(e,r,t)=>function e(r,t){if(H(r)||H(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}(G(e,r,t.slice(0,e.length)),G(r,e,t.slice(0,e.length)));function K(e,r){return[...e,...C(r)]}var Q=e=>Array.isArray(e)?Array(e.length).fill(void 0):void 0;function X(e,r,t){return[...e.slice(0,r),...C(t),...e.slice(r)]}var Y=e=>"string"==typeof e,Z=(e,r,t)=>Array.isArray(e)?(l(e[t])&&(e[t]=void 0),e.splice(t,0,e.splice(r,1)[0]),e):[];function ee(e,r){return[...C(r),...e]}var re=(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)),te=(e,r,t)=>{e[r]=[e[t],e[t]=e[r]][0]},ne=e=>"boolean"==typeof e;function se(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)&&S(n)||Array.isArray(n)&&!n.filter(e=>c(e)&&!S(e)||ne(e)).length)&&(u?delete u[r]:delete e[r]),u=n}}return e}function ce(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 ue=e=>"file"===e.type,ae=e=>"select-multiple"===e.type,ie=e=>"radio"===e.type;const le={value:!1,isValid:!1},oe={value:!0,isValid:!0};var fe=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?oe:{value:e[0].value,isValid:!0}:oe:le}return le},de=(e,{valueAsNumber:r,valueAsDate:t,setValueAs:n})=>r?""===e?NaN:+e:t?new Date(e):n?n(e):e;const be={isValid:!1,value:null};var ge=e=>Array.isArray(e)?e.reduce((e,r)=>r&&r.checked&&!r.disabled?{isValid:!0,value:r.value}:e,be):be;function ye(e){if(e&&e._f){const n=e._f.ref;if(n.disabled)return;return ue(n)?n.files:ie(n)?ge(e._f.refs).value:ae(n)?(r=n.options,[...r].filter(({selected:e})=>e).map(({value:e})=>e)):t(n)?fe(e._f.refs).value:de(l(n.value)?e._f.ref.value:n.value,e._f)}var r}var me=(e,r)=>e&&r&&(e.required||e.min||e.max||e.maxLength||e.minLength||e.pattern||e.validate),he=e=>"function"==typeof e,ve=e=>Y(e)||r.isValidElement(e),xe=e=>e instanceof RegExp;function pe(e,r,t="validate"){if(ve(e)||Array.isArray(e)&&e.every(ve)||ne(e)&&!e)return{type:t,message:ve(e)?e:"",ref:r}}var Oe=e=>c(e)&&!xe(e)?e:{value:e,message:""},je=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=ie(e),R=t(e),F=V||R,_=(g||ue(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&&(_||n(b))||ne(b)&&!b||R&&!fe(r).isValid||V&&!ge(r).isValid)){const{value:t,message:n}=ve(s)?{value:!!s,message:s}:Oe(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=Oe(l),c=Oe(i);if(isNaN(b)){const n=e.valueAsDate||new Date(b);Y(s.value)&&(r=n>new Date(s.value)),Y(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(Y(b)&&!_&&(u||a)){const e=Oe(u),r=Oe(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(Y(b)&&o&&!_){const{value:r,message:t}=Oe(o);if(xe(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(he(f)){const e=pe(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(!S(e)&&!m)break;const s=pe(await n(b),t,r);s&&(e=Object.assign(Object.assign({},s),w(r,s.message)),m&&(h[d]=e))}if(!S(e)&&(h[d]=Object.assign({ref:t},e),!m))return h}}return h},Ae=e=>({isOnSubmit:!e||e===y,isOnBlur:e===b,isOnChange:e===g,isOnAll:e===h,isOnTouch:e===m}),ke=e=>e instanceof HTMLElement;class Ve{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 Fe{constructor(){this.observers=[]}next(e){for(const r of this.observers)r.next(e)}subscribe(e){const r=new Ve,t=new Re(e,r);return this.observers.push(t),r}unsubscribe(){this.observers=[]}}const _e="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:_}=e||c.control,[S,w]=r.useState(q(o(h.current,t)?o(W(h),t):o(k.current,u(t))?o(k.current,t,[]):o(v.current,t,[]),n));P(k.current,t,[...S]),m.current.add(t);const D=e=>e.map(e=>V(e||{},n)),E=()=>{const e=o(W(h),t,[]);return q(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(q(e,n)),$=e=>!i(o(e,t,[])).length&&se(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,J(D(n),o(v.current,t,[]),o(x.current.dirtyFields,t,[]))),(e=>{e&&P(x.current.dirtyFields,t,J(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)=>!H(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(W(h),t,[])}),a.current&&T(h.current,e=>Y(e)&&e.startsWith(a.current)),a.current="",y.current.next({name:t,fields:D([...S])})},[S,t]),r.useEffect(()=>{const e=y.current.subscribe({next({name:e,fields:r,isReset:n}){n&&(se(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)&&R(t)}},[]),{swap:r.useCallback((e,r)=>{const t=E();te(t,e,r),M(te,{argA:e,argB:r},t,!1),N(t)},[t]),move:r.useCallback((e,r)=>{const t=E();Z(t,e,r),N(t),M(Z,{argA:e,argB:r},t,!1)},[t]),prepend:r.useCallback((e,r)=>{const t=C(e),n=ee(E(),t);N(n),M(ee,{argA:Q(e)},n),I(t),a.current=B(0,r)},[t]),append:r.useCallback((e,r)=>{const t=C(e),n=K(E(),t),s=n.length-t.length;N(n),M(K,{argA:Q(e)},n,!1),I(t,s),a.current=B(s,r)},[t]),remove:r.useCallback(e=>{const r=re(E(),e);(e=>{C(e).forEach(e=>P(h.current,`${t}${l(e)?"":"."+e}`,l(e)?[]:void 0))})(e),N(r),M(re,{argA:e},r)},[t]),insert:r.useCallback((e,r,t)=>{const n=C(r),s=X(E(),e,n);N(s),M(X,{argA:e,argB:Q(r)},s),I(n,e),a.current=B(e,t)},[t]),fields:S}},exports.useForm=function({mode:e=y,reValidateMode:s=g,resolver:d,context:b,defaultValues:m={},shouldFocusError:v=!0,shouldUnregister:x,criteriaMode:p}={}){const O=r.useRef({}),j=r.useRef(new Set),A=r.useRef(new Fe),k=r.useRef(new Set),R=r.useRef(new Fe),F=r.useRef(new Fe),B=r.useRef(new Fe),N=r.useRef({}),$=r.useRef(new Set),M=r.useRef(!1),I=r.useRef({}),U=r.useRef({}),L=r.useRef(m),q=r.useRef(!1),G=r.useRef(b),K=r.useRef(d),Q=r.useRef(new Set),X=Ae(e),Z=p===h,[ee,re]=r.useState({isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touchedFields:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!X.isOnSubmit,errors:{}}),te=r.useRef({isDirty:!E,dirtyFields:!E,touchedFields:!E,isValidating:!E,isValid:!E,errors:!E}),ne=r.useRef(ee);G.current=b,K.current=d;const le=()=>ne.current.isValid=z(U.current,I.current)&&S(ne.current.errors),oe=r.useCallback((e,r,t=!1,s={},c,u)=>{const a=o(ne.current.errors,e);let i=t||!z(a,r,!0)||te.current.isValid&&l(r)&&o(I.current,e)&&!o(U.current,e);if(r?(se(U.current,e),i=i||!a||!z(a,r,!0),P(ne.current.errors,e,r)):((o(I.current,e)||K.current)&&(P(U.current,e,!0),i=i||a),se(ne.current.errors,e)),i&&!n(t)||!S(s)||u){const r=Object.assign(Object.assign({},s),{isValid:K.current?!!c:le(),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})},[]),fe=r.useCallback((e,r,s={},c,u)=>{u&&Ne(e);const a=o(O.current,e,{})._f;if(a){const u=D&&ke(a.ref)&&n(r)?"":r;if(a.value=de(r,a),ie(a.ref)?(a.refs||[]).forEach(e=>e.checked=e.value===u):ue(a.ref)&&!Y(u)?a.ref.files=u:ae(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=W(O);P(t,e,r),F.current.next({values:Object.assign(Object.assign({},L.current),t),name:e})}s.shouldDirty&&ge(e,u),s.shouldValidate&&Oe(e)}},[]),be=r.useCallback((e,r)=>{const t=W(O);return e&&r&&P(t,e,r),!z(t,L.current)},[]),ge=r.useCallback((e,r,t=!0)=>{if(te.current.isDirty||te.current.dirtyFields){const n=!z(o(L.current,e),r),s=o(ne.current.dirtyFields,e),c=ne.current.isDirty;n?P(ne.current.dirtyFields,e,!0):se(ne.current.dirtyFields,e),ne.current.isDirty=be();const u={isDirty:ne.current.isDirty,dirtyFields:ne.current.dirtyFields,name:e},a=te.current.isDirty&&c!==u.isDirty||te.current.dirtyFields&&s!==o(ne.current.dirtyFields,e);return a&&t&&A.current.next(u),a?u:{}}return{}},[]),ve=r.useCallback(async(e,r)=>{const t=(await je(o(O.current,e),Z))[e];return oe(e,t,r),l(t)},[Z]),xe=r.useCallback(async(e,r=[])=>{const{errors:t}=await K.current(W(O),G.current,{criteriaMode:p,names:r,fields:ce(j.current,O.current)});for(const r of e){const e=o(t,r);e?P(ne.current.errors,r,e):se(ne.current.errors,r)}return t},[p]),pe=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 je(n,Z);t[e.name]?(r=!1,P(ne.current.errors,e.name,t[e.name]),se(U.current,e.name)):o(I.current,e.name)&&(P(U.current,e.name,!0),se(ne.current.errors,e.name))}t&&await pe(t)}}return r},Oe=r.useCallback(async e=>{const r=l(e)?Object.keys(O.current):C(e);let t,n={};return A.current.next({isValidating:!0}),K.current?(n=await xe(r,l(e)?void 0:r),t=r.every(e=>!o(n,e))):t=l(e)?await pe(O.current):(await Promise.all(r.filter(e=>o(O.current,e)).map(async e=>await ve(e,null)))).every(Boolean),A.current.next(Object.assign(Object.assign({},Y(e)?{name:e}:{}),{errors:ne.current.errors,isValidating:!1,isValid:K.current?S(n):le()})),t},[xe,ve]),Ve=r.useCallback((e,r,t)=>Object.entries(r).forEach(([r,n])=>{const s=`${e}.${r}`,c=o(O.current,s);Q.current.has(e)||c&&!c._f?Ve(s,n,t):fe(s,n,t,!0,!c)}),[Oe]),Re=e=>q.current||$.current.has(e)||$.current.has((e.match(/\w+/)||[])[0]),Se=(e,r,t,n)=>{const s=o(O.current,e),c=l(s._f.value)?o(L.current,e):s._f.value;return s&&!l(c)&&(t&&t.defaultChecked?s._f.value=ye(s):a(Q.current,e)?s._f.value=c:fe(e,c)),(!l(c)||n)&&me(r,s._f.mount)&&!X.isOnSubmit&&s&&te.current.isValid&&je(s,Z).then(r=>{S(r)?P(U.current,e,!0):se(U.current,e),ne.current.isValid!==le()&&re(Object.assign(Object.assign({},ne.current),{isValid:le()}))}),c},we=r.useCallback(async({type:e,target:r,target:{value:n,type:c}})=>{let a,i,d=r.name;const b=o(O.current,d);if(b){let g=c?ye(b):void 0;g=l(g)?n:g;const y=e===f,{isOnBlur:m,isOnChange:h}=Ae(s),v=!me(b._f,b._f.mount)&&!K.current&&!o(ne.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(ne.current.touchedFields,d),isSubmitted:ne.current.isSubmitted,isReValidateOnBlur:m,isReValidateOnChange:h},X)),x=!y&&Re(d);l(g)||(b._f.value=g);const j=ge(d,b._f.value,!1);y&&!o(ne.current.touchedFields,d)&&(P(ne.current.touchedFields,d,!0),te.current.touchedFields&&(j.touchedFields=ne.current.touchedFields));let k=!S(j)||x;if(v)return!y&&R.current.next({name:d,type:e,value:g}),k&&A.current.next(x?{name:d}:Object.assign(Object.assign({},j),{name:d}));if(A.current.next({isValidating:!0}),K.current){const{errors:e}=await K.current(W(O),G.current,{criteriaMode:p,fields:ce([d],O.current),names:[d]}),n=ne.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(ne.current.errors,r))&&(d=r)}i=S(e),n!==i&&(k=!0)}else a=(await je(b,Z))[d];!y&&R.current.next({name:d,type:e,value:g}),oe(d,a,k,j,i,x)}},[]),Ce=r.useCallback(async(e={})=>{const r=ne.current.isValid;if(d){const{errors:r}=await K.current(Object.assign(Object.assign({},W(O)),e),G.current,{criteriaMode:p,fields:ce(j.current,O.current)});ne.current.isValid=S(r)}else le();r!==ne.current.isValid&&A.current.next({isValid:ne.current.isValid})},[p]),De=r.useCallback((e,r,t)=>{const n=Array.isArray(e),s=M.current?W(O,L):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]},[]),Ee=(e,r={})=>{for(const t of e?C(e):Object.keys(j.current))j.current.delete(t),Q.current.delete(t),o(O.current,t)&&(r.keepIsValid||(se(I.current,t),se(U.current,t)),!r.keepError&&se(ne.current.errors,t),!r.keepValue&&se(O.current,t),!r.keepDirty&&se(ne.current.dirtyFields,t),!r.keepTouched&&se(ne.current.touchedFields,t),!x&&!r.keepDefaultValue&&se(L.current,t),R.current.next({name:t}));A.current.next(Object.assign(Object.assign(Object.assign({},ne.current),r.keepDirty?{isDirty:be()}:{}),K.current?{}:{isValid:le()})),!r.keepIsValid&&Ce()},Be=(e,r,n)=>{Ne(e,n);let s=o(O.current,e);const c=(e=>ie(e)||t(e))(r);if(r===s._f.ref||D&&ke(s._f.ref)&&!ke(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=>ke(e)&&document.contains(e)),r],ref:{type:r.type,name:e}}):Object.assign(Object.assign({},s._f),{ref:r})},P(O.current,e,s);const u=Se(e,n,r,!0);(c&&Array.isArray(u)?!z(o(O.current,e)._f.value,u):l(o(O.current,e)._f.value))&&(o(O.current,e)._f.value=ye(o(O.current,e)))},Ne=r.useCallback((e,r)=>{const t=!o(O.current,e);return P(O.current,e,{_f:Object.assign(Object.assign(Object.assign({},t?{ref:{name:e}}:Object.assign({ref:(o(O.current,e)._f||{}).ref},o(O.current,e)._f)),{name:e,mount:!0}),r)}),me(r,!0)&&P(I.current,e,!0),j.current.add(e),t&&Se(e,r),_e?{name:e}:{name:e,onChange:we,onBlur:we,ref:t=>{if(t)Be(e,t,r);else{const t=o(O.current,e);t&&(t._f.mount=!1),D&&(x||r&&r.shouldUnregister)&&k.current.add(e)}}}},[L.current]),$e=r.useCallback((e,r)=>async t=>{t&&(t.preventDefault&&t.preventDefault(),t.persist&&t.persist());let n=!0,s=W(O);A.current.next({isSubmitting:!0});try{if(K.current){const{errors:e,values:r}=await K.current(s,G.current,{criteriaMode:p,fields:ce(j.current,O.current)});ne.current.errors=e,s=r}else await pe(O.current);S(ne.current.errors)&&Object.keys(ne.current.errors).every(e=>o(s,e))?(A.current.next({errors:{},isSubmitting:!0}),await e(s,t)):(r&&await r(ne.current.errors,t),v&&T(O.current,e=>o(ne.current.errors,e),j.current))}catch(e){n=!1}finally{ne.current.isSubmitted=!0,A.current.next({isSubmitted:!0,isSubmitting:!1,isSubmitSuccessful:S(ne.current.errors)&&n,submitCount:ne.current.submitCount+1,errors:ne.current.errors})}},[v,Z,p]),Me=r.useCallback(({keepErrors:e,keepDirty:r,keepIsSubmitted:t,keepTouched:n,keepDefaultValues:s,keepIsValid:c,keepSubmitCount:u},a)=>{c||(U.current={},I.current={}),$.current=new Set,q.current=!1,A.current.next({submitCount:u?ne.current.submitCount:0,isDirty:r?ne.current.isDirty:!!s&&z(a,L.current),isSubmitted:!!t&&ne.current.isSubmitted,isValid:c?ne.current.isValid:!!Ce(a),dirtyFields:r?ne.current.dirtyFields:{},touchedFields:n?ne.current.touchedFields:{},errors:e?ne.current.errors:{},isSubmitting:!1,isSubmitSuccessful:!1})},[]),Ie=(e,r="")=>{if(!o(O.current,r)&&(H(e)||D&&(e instanceof File||e instanceof Date||ke(e)))&&P(O.current,r,{_f:{ref:{name:r,value:e},value:e,name:r}}),Array.isArray(e)||c(e)){r&&!o(O.current,r)&&P(O.current,r,Array.isArray(e)?[]:{});for(const t in e)Ie(e[t],r+(r?".":"")+t)}};return r.useEffect(()=>{Ie(L.current);const e=A.current.subscribe({next(e){w(e,te.current,!0)&&(ne.current=Object.assign(Object.assign({},ne.current),e),re(ne.current))}}),r=B.current.subscribe({next(e){if(e.fields&&e.name&&te.current.isValid){const r=W(O);P(r,e.name,e.fields),Ce(r)}}});return K.current&&te.current.isValid&&Ce(),()=>{R.current.unsubscribe(),e.unsubscribe(),r.unsubscribe()}},[]),r.useEffect(()=>{const e=e=>!ke(e)||!document.contains(e);M.current=!0,k.current.forEach(r=>{const t=o(O.current,r);t&&(t._f.refs?t._f.refs.every(e):e(t._f.ref))&&Ee(r)}),k.current=new Set}),{control:r.useMemo(()=>({register:Ne,isWatchAllRef:q,watchFieldsRef:$,getIsDirty:be,formStateSubjectRef:A,fieldArraySubjectRef:B,controllerSubjectRef:F,watchSubjectRef:R,watchInternal:De,fieldsRef:O,validFieldsRef:U,fieldsWithValidationRef:I,fieldArrayNamesRef:Q,readFormStateRef:te,formStateRef:ne,defaultValuesRef:L,fieldArrayDefaultValuesRef:N,unregister:Ee,shouldUnmountUnregister:x}),[]),formState:_(E,ee,te),trigger:Oe,register:Ne,handleSubmit:$e,watch:r.useCallback((e,r)=>he(e)?R.current.subscribe({next:t=>e(De(void 0,r),t)}):De(e,r,!0),[]),setValue:r.useCallback((e,r,t={})=>{const n=o(O.current,e),s=Q.current.has(e);s&&(B.current.next({fields:r,name:e,isReset:!0}),(te.current.isDirty||te.current.dirtyFields)&&t.shouldDirty&&(P(ne.current.dirtyFields,e,J(r,o(L.current,e,[]),o(ne.current.dirtyFields,e,[]))),A.current.next({name:e,dirtyFields:ne.current.dirtyFields,isDirty:be(e,r)})),!r.length&&P(O.current,e,[])&&P(N.current,e,[])),n&&!n._f||s?Ve(e,r,s?{}:t):fe(e,r,t,!0,!n),Re(e)&&A.current.next({}),R.current.next({name:e,value:r})},[Ve]),getValues:r.useCallback(e=>{const r=W(O,L);return l(e)?r:Y(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 j.current){const r=o(O.current,e);if(r&&r._f){const e=Array.isArray(r._f.refs)?r._f.refs[0]:r._f.ref;if(ke(e))try{e.closest("form").reset();break}catch(e){}}}r.keepDefaultValues||(L.current=Object.assign({},t),Ie(t)),r.keepValues||(O.current={},F.current.next({values:Object.assign({},t)}),R.current.next({value:Object.assign({},t)}),B.current.next({fields:Object.assign({},t),isReset:!0})),Me(r,e),M.current=!1},[]),clearErrors:r.useCallback(e=>{e&&C(e).forEach(e=>se(ne.current.errors,e)),A.current.next({errors:e?ne.current.errors:{}})},[]),unregister:r.useCallback(Ee,[]),setError:r.useCallback((e,r,t)=>{const n=((o(O.current,e)||{_f:{}})._f||{}).ref;P(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:r.useCallback(e=>o(O.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(Y(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,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};
2
2
  //# sourceMappingURL=index.cjs.js.map