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