You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2 lines
24 KiB

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactHookForm={},e.React)}(this,(function(e,t){"use strict";function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=r(t),a=e=>"checkbox"===e.type,n=e=>e instanceof Date,i=e=>null==e;const o=e=>"object"==typeof e;var u=e=>!i(e)&&!Array.isArray(e)&&o(e)&&!n(e),l=e=>u(e)&&e.target?a(e.target)?e.target.checked:e.target.value:e,c=(e,t)=>e.has((e=>e.substring(0,e.search(/\.\d+(\.|$)/))||e)(t)),d=e=>Array.isArray(e)?e.filter(Boolean):[],f=e=>void 0===e,m=(e,t,r)=>{if(!t||!u(e))return r;const s=d(t.split(/[,[\].]+?/)).reduce((e,t)=>i(e)?e:e[t],e);return f(s)||s===e?f(e[t])?r:e[t]:s};const y="blur",g="focusout",h="change",p="onBlur",_="onChange",v="onSubmit",b="onTouched",A="all",x="max",F="min",V="maxLength",w="minLength",S="pattern",k="required",D="validate",C=s.default.createContext(null),j=()=>s.default.useContext(C);var E=(e,t,r,s=!0)=>{const a={};for(const n in e)Object.defineProperty(a,n,{get:()=>{const a=n;return t[a]!==A&&(t[a]=!s||A),r&&(r[a]=!0),e[a]}});return a},O=e=>u(e)&&!Object.keys(e).length,T=(e,t,r)=>{const{name:s,...a}=e;return O(a)||Object.keys(a).length>=Object.keys(t).length||Object.keys(a).find(e=>t[e]===(!r||A))},B=e=>Array.isArray(e)?e:[e],U=(e,t,r)=>r&&t?e===t:!e||!t||e===t||B(e).some(e=>e&&(e.startsWith(t)||t.startsWith(e)));function M(e){const t=s.default.useRef(e);t.current=e,s.default.useEffect(()=>{const r=!e.disabled&&t.current.subject.subscribe({next:t.current.callback});return()=>(e=>{e&&e.unsubscribe()})(r)},[e.disabled])}function N(e){const t=j(),{control:r=t.control,disabled:a,name:n,exact:i}=e||{},[o,u]=s.default.useState(r._formState),l=s.default.useRef({isDirty:!1,dirtyFields:!1,touchedFields:!1,isValidating:!1,isValid:!1,errors:!1}),c=s.default.useRef(n),d=s.default.useRef(!0);c.current=n;return M({disabled:a,callback:s.default.useCallback(e=>d.current&&U(c.current,e.name,i)&&T(e,l.current)&&u({...r._formState,...e}),[r,i]),subject:r._subjects.state}),s.default.useEffect(()=>(d.current=!0,()=>{d.current=!1}),[]),E(o,r._proxyFormState,l.current,!1)}var L=e=>"string"==typeof e,R=(e,t,r,s)=>{const a=Array.isArray(e);return L(e)?(s&&t.watch.add(e),m(r,e)):a?e.map(e=>(s&&t.watch.add(e),m(r,e))):(s&&(t.watchAll=!0),r)},q=e=>"function"==typeof e,W=e=>{for(const t in e)if(q(e[t]))return!0;return!1};function P(e){const t=j(),{control:r=t.control,name:a,defaultValue:n,disabled:i,exact:o}=e||{},l=s.default.useRef(a);l.current=a;const c=s.default.useCallback(e=>{if(U(l.current,e.name,o)){const t=R(l.current,r._names,e.values||r._formValues);m(f(l.current)||u(t)&&!W(t)?{...t}:Array.isArray(t)?[...t]:f(t)?n:t)}},[r,o,n]);M({disabled:i,subject:r._subjects.watch,callback:c});const[d,m]=s.default.useState(f(n)?r._getWatch(a):n);return s.default.useEffect(()=>{r._removeUnmounted()}),d}function $(e){const t=j(),{name:r,control:a=t.control,shouldUnregister:n}=e,i=c(a._names.array,r),o=P({control:a,name:r,defaultValue:m(a._formValues,r,m(a._defaultValues,r,e.defaultValue)),exact:!0}),u=N({control:a,name:r}),d=s.default.useRef(a.register(r,{...e.rules,value:o}));return s.default.useEffect(()=>{const e=(e,t)=>{const r=m(a._fields,e);r&&(r._f.mount=t)};return e(r,!0),()=>{const t=a._options.shouldUnregister||n;(i?t&&!a._stateFlags.action:t)?a.unregister(r):e(r,!1)}},[r,a,i,n]),{field:{name:r,value:o,onChange:s.default.useCallback(e=>{d.current.onChange({target:{value:l(e),name:r},type:h})},[r]),onBlur:s.default.useCallback(()=>{d.current.onBlur({target:{value:m(a._formValues,r),name:r},type:y})},[r,a]),ref:s.default.useCallback(e=>{const t=m(a._fields,r);e&&t&&e.focus&&(t._f.ref={focus:()=>e.focus(),select:()=>e.select(),setCustomValidity:t=>e.setCustomValidity(t),reportValidity:()=>e.reportValidity()})},[r,a._fields])},formState:u,fieldState:Object.defineProperties({},{invalid:{get:()=>!!m(u.errors,r)},isDirty:{get:()=>!!m(u.dirtyFields,r)},isTouched:{get:()=>!!m(u.touchedFields,r)},error:{get:()=>m(u.errors,r)}})}}var I=(e,t,r,s,a)=>t?{...r[e],types:{...r[e]&&r[e].types?r[e].types:{},[s]:a||!0}}:{},H=e=>/^\w*$/.test(e),z=e=>d(e.replace(/["|']|\]/g,"").split(/\.|\[/));function G(e,t,r){let s=-1;const a=H(t)?[t]:z(t),n=a.length,i=n-1;for(;++s<n;){const t=a[s];let n=r;if(s!==i){const r=e[t];n=u(r)||Array.isArray(r)?r:isNaN(+a[s+1])?{}:[]}e[t]=n,e=e[t]}return e}const J=(e,t,r)=>{for(const s of r||Object.keys(e)){const r=m(e,s);if(r){const{_f:e,...s}=r;if(e&&t(e.name)){if(e.ref.focus&&f(e.ref.focus()))break;if(e.refs){e.refs[0].focus();break}}else u(s)&&J(s,t)}}};var K=()=>{const e="undefined"==typeof performance?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,t=>{const r=(16*Math.random()+e)%16|0;return("x"==t?r:3&r|8).toString(16)})},Q=(e,t,r={})=>r.shouldFocus||f(r.shouldFocus)?r.focusName||`${e}.${f(r.focusIndex)?t:r.focusIndex}.`:"",X=(e,t,r)=>!r&&(t.watchAll||t.watch.has(e)||[...t.watch].some(t=>e.startsWith(t)&&/^\.\w+/.test(e.slice(t.length))));function Y(e,t){return[...e,...B(t)]}var Z="undefined"!=typeof window&&void 0!==window.HTMLElement&&"undefined"!=typeof document;function ee(e){let t;const r=Array.isArray(e);if(e instanceof Date)t=new Date(e);else if(e instanceof Set)t=new Set(e);else{if(Z&&(e instanceof Blob||e instanceof FileList)||!r&&!u(e))return e;t=r?[]:{};for(const r in e){if(q(e[r])){t=e;break}t[r]=ee(e[r])}}return t}var te=e=>Array.isArray(e)?e.map(()=>{}):void 0;function re(e,t,r){return[...e.slice(0,t),...B(r),...e.slice(t)]}var se=(e,t,r)=>Array.isArray(e)?(f(e[r])&&(e[r]=void 0),e.splice(r,0,e.splice(t,1)[0]),e):[];function ae(e,t){return[...B(t),...B(e)]}var ne=(e,t)=>f(t)?[]:function(e,t){let r=0;const s=[...e];for(const e of t)s.splice(e-r,1),r++;return d(s).length?s:[]}(e,B(t).sort((e,t)=>e-t)),ie=(e,t,r)=>{e[t]=[e[r],e[r]=e[t]][0]};function oe(e,t){const r=H(t)?[t]:z(t),s=1==r.length?e:function(e,t){const r=t.slice(0,-1).length;let s=0;for(;s<r;)e=f(e)?s++:e[t[s++]];return e}(e,r),a=r[r.length-1];let n;s&&delete s[a];for(let t=0;t<r.slice(0,-1).length;t++){let s,a=-1;const i=r.slice(0,-(t+1)),o=i.length-1;for(t>0&&(n=e);++a<i.length;){const t=i[a];s=s?s[t]:e[t],o===a&&(u(s)&&O(s)||Array.isArray(s)&&!s.filter(e=>!f(e)).length)&&(n?delete n[t]:delete e[t]),n=s}}return e}var ue=(e,t,r)=>(e[t]=r,e);function le(){let e=[];return{get observers(){return e},next:t=>{for(const r of e)r.next(t)},subscribe:t=>(e.push(t),{unsubscribe:()=>{e=e.filter(e=>e!==t)}}),unsubscribe:()=>{e=[]}}}var ce=e=>i(e)||!o(e);function de(e,t){if(ce(e)||ce(t))return e===t;if(n(e)&&n(t))return e.getTime()===t.getTime();const r=Object.keys(e),s=Object.keys(t);if(r.length!==s.length)return!1;for(const a of r){const r=e[a];if(!s.includes(a))return!1;if("ref"!==a){const e=t[a];if(n(r)&&n(e)||u(r)&&u(e)||Array.isArray(r)&&Array.isArray(e)?!de(r,e):r!==e)return!1}}return!0}var fe=e=>({isOnSubmit:!e||e===v,isOnBlur:e===p,isOnChange:e===_,isOnAll:e===A,isOnTouch:e===b}),me=e=>"boolean"==typeof e,ye=e=>"file"===e.type,ge=e=>{const t=e?e.ownerDocument:0;return e instanceof(t&&t.defaultView?t.defaultView.HTMLElement:HTMLElement)},he=e=>"select-multiple"===e.type,pe=e=>"radio"===e.type,_e=e=>ge(e)&&e.isConnected;function ve(e,t={}){const r=Array.isArray(e);if(u(e)||r)for(const r in e)Array.isArray(e[r])||u(e[r])&&!W(e[r])?(t[r]=Array.isArray(e[r])?[]:{},ve(e[r],t[r])):i(e[r])||(t[r]=!0);return t}var be=(e,t)=>function e(t,r,s){const a=Array.isArray(t);if(u(t)||a)for(const a in t)Array.isArray(t[a])||u(t[a])&&!W(t[a])?f(r)||ce(s[a])?s[a]=Array.isArray(t[a])?ve(t[a],[]):{...ve(t[a])}:e(t[a],i(r)?{}:r[a],s[a]):s[a]=!de(t[a],r[a]);return s}(e,t,ve(t));const Ae={value:!1,isValid:!1},xe={value:!0,isValid:!0};var Fe=e=>{if(Array.isArray(e)){if(e.length>1){const t=e.filter(e=>e&&e.checked&&!e.disabled).map(e=>e.value);return{value:t,isValid:!!t.length}}return e[0].checked&&!e[0].disabled?e[0].attributes&&!f(e[0].attributes.value)?f(e[0].value)||""===e[0].value?xe:{value:e[0].value,isValid:!0}:xe:Ae}return Ae},Ve=(e,{valueAsNumber:t,valueAsDate:r,setValueAs:s})=>f(e)?e:t?""===e||i(e)?NaN:+e:r&&L(e)?new Date(e):s?s(e):e;const we={isValid:!1,value:null};var Se=e=>Array.isArray(e)?e.reduce((e,t)=>t&&t.checked&&!t.disabled?{isValid:!0,value:t.value}:e,we):we;function ke(e){const t=e.ref;if(!(e.refs?e.refs.every(e=>e.disabled):t.disabled))return ye(t)?t.files:pe(t)?Se(e.refs).value:he(t)?[...t.selectedOptions].map(({value:e})=>e):a(t)?Fe(e.refs).value:Ve(f(t.value)?e.ref.value:t.value,e)}var De=e=>e instanceof RegExp,Ce=e=>f(e)?void 0:De(e)?e.source:u(e)?De(e.value)?e.value.source:e.value:e;function je(e,t,r){const s=m(e,r);if(s||H(r))return{error:s,name:r};const a=r.split(".");for(;a.length;){const s=a.join("."),n=m(t,s),i=m(e,s);if(n&&!Array.isArray(n)&&r!==s)return{name:r};if(i&&i.type)return{name:s,error:i};a.pop()}return{name:r}}var Ee=e=>L(e)||s.default.isValidElement(e);function Oe(e,t,r="validate"){if(Ee(e)||Array.isArray(e)&&e.every(Ee)||me(e)&&!e)return{type:r,message:Ee(e)?e:"",ref:t}}var Te=e=>u(e)&&!De(e)?e:{value:e,message:""},Be=async(e,t,r,s)=>{const{ref:n,refs:o,required:l,maxLength:c,minLength:d,min:f,max:m,pattern:y,validate:g,name:h,valueAsNumber:p,mount:_,disabled:v}=e._f;if(!_||v)return{};const b=o?o[0]:n,A=e=>{s&&b.reportValidity&&(b.setCustomValidity(me(e)?"":e||" "),b.reportValidity())},C={},j=pe(n),E=a(n),T=j||E,B=(p||ye(n))&&!n.value||""===t||Array.isArray(t)&&!t.length,U=I.bind(null,h,r,C),M=(e,t,r,s=V,a=w)=>{const i=e?t:r;C[h]={type:e?s:a,message:i,ref:n,...U(e?s:a,i)}};if(l&&(!T&&(B||i(t))||me(t)&&!t||E&&!Fe(o).isValid||j&&!Se(o).isValid)){const{value:e,message:t}=Ee(l)?{value:!!l,message:l}:Te(l);if(e&&(C[h]={type:k,message:t,ref:b,...U(k,t)},!r))return A(t),C}if(!(B||i(f)&&i(m))){let e,s;const a=Te(m),o=Te(f);if(i(t)||isNaN(t)){const r=n.valueAsDate||new Date(t);L(a.value)&&(e=r>new Date(a.value)),L(o.value)&&(s=r<new Date(o.value))}else{const r=n.valueAsNumber||+t;i(a.value)||(e=r>a.value),i(o.value)||(s=r<o.value)}if((e||s)&&(M(!!e,a.message,o.message,x,F),!r))return A(C[h].message),C}if((c||d)&&!B&&L(t)){const e=Te(c),s=Te(d),a=!i(e.value)&&t.length>e.value,n=!i(s.value)&&t.length<s.value;if((a||n)&&(M(a,e.message,s.message),!r))return A(C[h].message),C}if(y&&!B&&L(t)){const{value:e,message:s}=Te(y);if(De(e)&&!t.match(e)&&(C[h]={type:S,message:s,ref:n,...U(S,s)},!r))return A(s),C}if(g)if(q(g)){const e=Oe(await g(t),b);if(e&&(C[h]={...e,...U(D,e.message)},!r))return A(e.message),C}else if(u(g)){let e={};for(const s in g){if(!O(e)&&!r)break;const a=Oe(await g[s](t),b,s);a&&(e={...a,...U(s,a.message)},A(a.message),r&&(C[h]=e))}if(!O(e)&&(C[h]={ref:b,...e},!r))return C}return A(!0),C};const Ue={mode:v,reValidateMode:_,shouldFocusError:!0};function Me(e={}){let t,r={...Ue,...e},s={isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touchedFields:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!1,errors:{}},o={},u=ee(r.defaultValues)||{},h=r.shouldUnregister?{}:ee(u),p={action:!1,mount:!1,watch:!1},_={mount:new Set,unMount:new Set,array:new Set,watch:new Set},v=0,b={};const x={isDirty:!1,dirtyFields:!1,touchedFields:!1,isValidating:!1,isValid:!1,errors:!1},F={watch:le(),array:le(),state:le()},V=fe(r.mode),w=fe(r.reValidateMode),S=r.criteriaMode===A,k=async e=>{let t=!1;return x.isValid&&(t=r.resolver?O((await E()).errors):await T(o,!0),e||t===s.isValid||(s.isValid=t,F.state.next({isValid:t}))),t},D=(e,t,r,s)=>{const a=m(o,e);if(a){const n=m(h,e,f(r)?m(u,e):r);f(n)||s&&s.defaultChecked||t?G(h,e,t?n:ke(a._f)):N(e,n),p.mount&&k()}},C=(e,t,r,a,n)=>{let i=!1;const o={name:e},l=m(s.touchedFields,e);if(x.isDirty){const e=s.isDirty;s.isDirty=o.isDirty=U(),i=e!==o.isDirty}if(x.dirtyFields&&(!r||a)){const r=m(s.dirtyFields,e);de(m(u,e),t)?oe(s.dirtyFields,e):G(s.dirtyFields,e,!0),o.dirtyFields=s.dirtyFields,i=i||r!==m(s.dirtyFields,e)}return r&&!l&&(G(s.touchedFields,e,r),o.touchedFields=s.touchedFields,i=i||x.touchedFields&&l!==r),i&&n&&F.state.next(o),i?o:{}},j=async(r,a,n,i)=>{const o=m(s.errors,r),u=x.isValid&&s.isValid!==a;var l;if(e.delayError&&n?(l=()=>((e,t)=>{G(s.errors,e,t),F.state.next({errors:s.errors})})(r,n),t=e=>{clearTimeout(v),v=window.setTimeout(l,e)},t(e.delayError)):(clearTimeout(v),t=null,n?G(s.errors,r,n):oe(s.errors,r)),(n?!de(o,n):o)||!O(i)||u){const e={...i,...u?{isValid:a}:{},errors:s.errors,name:r};s={...s,...e},F.state.next(e)}b[r]--,x.isValidating&&!Object.values(b).some(e=>e)&&(F.state.next({isValidating:!1}),b={})},E=async e=>r.resolver?await r.resolver({...h},r.context,((e,t,r,s)=>{const a={};for(const r of e){const e=m(t,r);e&&G(a,r,e._f)}return{criteriaMode:r,names:[...e],fields:a,shouldUseNativeValidation:s}})(e||_.mount,o,r.criteriaMode,r.shouldUseNativeValidation)):{},T=async(e,t,a={valid:!0})=>{for(const n in e){const i=e[n];if(i){const{_f:e,...n}=i;if(e){const n=await Be(i,m(h,e.name),S,r.shouldUseNativeValidation);if(n[e.name]&&(a.valid=!1,t))break;t||(n[e.name]?G(s.errors,e.name,n[e.name]):oe(s.errors,e.name))}n&&await T(n,t,a)}}return a.valid},U=(e,t)=>(e&&t&&G(h,e,t),!de(H(),u)),M=(e,t,r)=>{const s={...p.mount?h:f(t)?u:L(e)?{[e]:t}:t};return R(e,_,s,r)},N=(e,t,r={})=>{const s=m(o,e);let n=t;if(s){const r=s._f;r&&(!r.disabled&&G(h,e,Ve(t,r)),n=Z&&ge(r.ref)&&i(t)?"":t,he(r.ref)?[...r.ref.options].forEach(e=>e.selected=n.includes(e.value)):r.refs?a(r.ref)?r.refs.length>1?r.refs.forEach(e=>!e.disabled&&(e.checked=Array.isArray(n)?!!n.find(t=>t===e.value):n===e.value)):r.refs[0]&&(r.refs[0].checked=!!n):r.refs.forEach(e=>e.checked=e.value===n):ye(r.ref)?r.ref.value="":(r.ref.value=n,r.ref.type||F.watch.next({name:e})))}(r.shouldDirty||r.shouldTouch)&&C(e,n,r.shouldTouch,r.shouldDirty,!0),r.shouldValidate&&I(e)},W=(e,t,r)=>{for(const s in t){const a=t[s],i=`${e}.${s}`,u=m(o,i);!_.array.has(e)&&ce(a)&&(!u||u._f)||n(a)?N(i,a,r):W(i,a,r)}},P=(e,t,r={})=>{const a=m(o,e),n=_.array.has(e),l=ee(t);G(h,e,l),n?(F.array.next({name:e,values:h}),(x.isDirty||x.dirtyFields)&&r.shouldDirty&&(s.dirtyFields=be(u,h),F.state.next({name:e,dirtyFields:s.dirtyFields,isDirty:U(e,l)}))):!a||a._f||i(l)?N(e,l,r):W(e,l,r),X(e,_)&&F.state.next({}),F.watch.next({name:e})},$=async e=>{const a=e.target;let n=a.name;const i=m(o,n);if(i){let c,d;const f=a.type?ke(i._f):l(e),p=e.type===y||e.type===g,v=!((u=i._f).mount&&(u.required||u.min||u.max||u.maxLength||u.minLength||u.pattern||u.validate)||r.resolver||m(s.errors,n)||i._f.deps)||((e,t,r,s,a)=>!a.isOnAll&&(!r&&a.isOnTouch?!(t||e):(r?s.isOnBlur:a.isOnBlur)?!e:!(r?s.isOnChange:a.isOnChange)||e))(p,m(s.touchedFields,n),s.isSubmitted,w,V),A=X(n,_,p);G(h,n,f),p?(i._f.onBlur&&i._f.onBlur(e),t&&t(0)):i._f.onChange&&i._f.onChange(e);const x=C(n,f,p,!1),D=!O(x)||A;if(!p&&F.watch.next({name:n,type:e.type}),v)return D&&F.state.next({name:n,...A?{}:x});if(!p&&A&&F.state.next({}),b[n]=(b[n],1),F.state.next({isValidating:!0}),r.resolver){const{errors:e}=await E([n]),t=je(s.errors,o,n),r=je(e,o,t.name||n);c=r.error,n=r.name,d=O(e)}else c=(await Be(i,m(h,n),S,r.shouldUseNativeValidation))[n],d=await k(!0);i._f.deps&&I(i._f.deps),j(n,d,c,x)}var u},I=async(e,t={})=>{let a,n;const i=B(e);if(F.state.next({isValidating:!0}),r.resolver){const t=await(async e=>{const{errors:t}=await E();if(e)for(const r of e){const e=m(t,r);e?G(s.errors,r,e):oe(s.errors,r)}else s.errors=t;return t})(f(e)?e:i);a=O(t),n=e?!i.some(e=>m(t,e)):a}else e?(n=(await Promise.all(i.map(async e=>{const t=m(o,e);return await T(t&&t._f?{[e]:t}:t)}))).every(Boolean),(n||s.isValid)&&k()):n=a=await T(o);return F.state.next({...!L(e)||x.isValid&&a!==s.isValid?{}:{name:e},...r.resolver?{isValid:a}:{},errors:s.errors,isValidating:!1}),t.shouldFocus&&!n&&J(o,e=>m(s.errors,e),e?i:_.mount),n},H=e=>{const t={...u,...p.mount?h:{}};return f(e)?t:L(e)?m(t,e):e.map(e=>m(t,e))},z=(e,t)=>({invalid:!!m((t||s).errors,e),isDirty:!!m((t||s).dirtyFields,e),isTouched:!!m((t||s).touchedFields,e),error:m((t||s).errors,e)}),K=(e,t={})=>{for(const a of e?B(e):_.mount)_.mount.delete(a),_.array.delete(a),m(o,a)&&(t.keepValue||(oe(o,a),oe(h,a)),!t.keepError&&oe(s.errors,a),!t.keepDirty&&oe(s.dirtyFields,a),!t.keepTouched&&oe(s.touchedFields,a),!r.shouldUnregister&&!t.keepDefaultValue&&oe(u,a));F.watch.next({}),F.state.next({...s,...t.keepDirty?{isDirty:U()}:{}}),!t.keepIsValid&&k()},Q=(e,t={})=>{let s=m(o,e);const n=me(t.disabled);return G(o,e,{_f:{...s&&s._f?s._f:{ref:{name:e}},name:e,mount:!0,...t}}),_.mount.add(e),s?n&&G(h,e,t.disabled?void 0:m(h,e,ke(s._f))):D(e,!0,t.value),{...n?{disabled:t.disabled}:{},...r.shouldUseNativeValidation?{required:!!t.required,min:Ce(t.min),max:Ce(t.max),minLength:Ce(t.minLength),maxLength:Ce(t.maxLength),pattern:Ce(t.pattern)}:{},name:e,onChange:$,onBlur:$,ref:n=>{if(n){Q(e,t),s=m(o,e);const r=f(n.value)&&n.querySelectorAll&&n.querySelectorAll("input,select,textarea")[0]||n,i=(e=>pe(e)||a(e))(r),l=s._f.refs||[];if(i?l.find(e=>e===r):r===s._f.ref)return;G(o,e,{_f:{...s._f,...i?{refs:[...l.filter(_e),r,...Array.isArray(m(u,e))?[{}]:[]],ref:{type:r.type,name:e}}:{ref:r}}}),D(e,!1,void 0,r)}else s=m(o,e,{}),s._f&&(s._f.mount=!1),(r.shouldUnregister||t.shouldUnregister)&&(!c(_.array,e)||!p.action)&&_.unMount.add(e)}}};return{control:{register:Q,unregister:K,getFieldState:z,_executeSchema:E,_getWatch:M,_getDirty:U,_updateValid:k,_removeUnmounted:()=>{for(const e of _.unMount){const t=m(o,e);t&&(t._f.refs?t._f.refs.every(e=>!_e(e)):!_e(t._f.ref))&&K(e)}_.unMount=new Set},_updateFieldArray:(e,t=[],r,a,n=!0,i=!0)=>{if(a&&r){if(p.action=!0,i&&Array.isArray(m(o,e))){const t=r(m(o,e),a.argA,a.argB);n&&G(o,e,t)}if(x.errors&&i&&Array.isArray(m(s.errors,e))){const t=r(m(s.errors,e),a.argA,a.argB);n&&G(s.errors,e,t),((e,t)=>{!d(m(e,t)).length&&oe(e,t)})(s.errors,e)}if(x.touchedFields&&i&&Array.isArray(m(s.touchedFields,e))){const t=r(m(s.touchedFields,e),a.argA,a.argB);n&&G(s.touchedFields,e,t)}x.dirtyFields&&(s.dirtyFields=be(u,h)),F.state.next({isDirty:U(e,t),dirtyFields:s.dirtyFields,errors:s.errors,isValid:s.isValid})}else G(h,e,t)},_getFieldArray:t=>d(m(p.mount?h:u,t,e.shouldUnregister?m(u,t,[]):[])),_subjects:F,_proxyFormState:x,get _fields(){return o},get _formValues(){return h},get _stateFlags(){return p},set _stateFlags(e){p=e},get _defaultValues(){return u},get _names(){return _},set _names(e){_=e},get _formState(){return s},set _formState(e){s=e},get _options(){return r},set _options(e){r={...r,...e}}},trigger:I,register:Q,handleSubmit:(e,t)=>async a=>{a&&(a.preventDefault&&a.preventDefault(),a.persist&&a.persist());let n=!0,i=ee(h);F.state.next({isSubmitting:!0});try{if(r.resolver){const{errors:e,values:t}=await E();s.errors=e,i=t}else await T(o);O(s.errors)?(F.state.next({errors:{},isSubmitting:!0}),await e(i,a)):(t&&await t({...s.errors},a),r.shouldFocusError&&J(o,e=>m(s.errors,e),_.mount))}catch(e){throw n=!1,e}finally{s.isSubmitted=!0,F.state.next({isSubmitted:!0,isSubmitting:!1,isSubmitSuccessful:O(s.errors)&&n,submitCount:s.submitCount+1,errors:s.errors})}},watch:(e,t)=>q(e)?F.watch.subscribe({next:r=>e(M(void 0,t),r)}):M(e,t,!0),setValue:P,getValues:H,reset:(t,r={})=>{const a=t||u,n=ee(a),i=t&&!O(t)?n:u;if(r.keepDefaultValues||(u=a),!r.keepValues){if(r.keepDirtyValues)for(const e of _.mount)m(s.dirtyFields,e)?G(i,e,m(h,e)):P(e,m(i,e));else{if(Z&&f(t))for(const e of _.mount){const t=m(o,e);if(t&&t._f){const e=Array.isArray(t._f.refs)?t._f.refs[0]:t._f.ref;try{ge(e)&&e.closest("form").reset();break}catch(e){}}}o={}}h=e.shouldUnregister?r.keepDefaultValues?ee(u):{}:n,F.array.next({values:i}),F.watch.next({values:i})}_={mount:new Set,unMount:new Set,array:new Set,watch:new Set,watchAll:!1,focus:""},p.mount=!x.isValid||!!r.keepIsValid,p.watch=!!e.shouldUnregister,F.state.next({submitCount:r.keepSubmitCount?s.submitCount:0,isDirty:r.keepDirty||r.keepDirtyValues?s.isDirty:!(!r.keepDefaultValues||de(t,u)),isSubmitted:!!r.keepIsSubmitted&&s.isSubmitted,dirtyFields:r.keepDirty||r.keepDirtyValues?s.dirtyFields:r.keepDefaultValues&&t?be(u,t):{},touchedFields:r.keepTouched?s.touchedFields:{},errors:r.keepErrors?s.errors:{},isSubmitting:!1,isSubmitSuccessful:!1})},resetField:(e,t={})=>{m(o,e)&&(f(t.defaultValue)?P(e,m(u,e)):(P(e,t.defaultValue),G(u,e,t.defaultValue)),t.keepTouched||oe(s.touchedFields,e),t.keepDirty||(oe(s.dirtyFields,e),s.isDirty=t.defaultValue?U(e,m(u,e)):U()),t.keepError||(oe(s.errors,e),x.isValid&&k()),F.state.next({...s}))},clearErrors:e=>{e?B(e).forEach(e=>oe(s.errors,e)):s.errors={},F.state.next({errors:s.errors})},unregister:K,setError:(e,t,r)=>{const a=(m(o,e,{_f:{}})._f||{}).ref;G(s.errors,e,{...t,ref:a}),F.state.next({name:e,errors:s.errors,isValid:!1}),r&&r.shouldFocus&&a&&a.focus&&a.focus()},setFocus:(e,t={})=>{const r=m(o,e)._f,s=r.refs?r.refs[0]:r.ref;s.focus(),t.shouldSelect&&s.select()},getFieldState:z}}e.Controller=e=>e.render($(e)),e.FormProvider=e=>{const{children:t,...r}=e;return s.default.createElement(C.Provider,{value:r},t)},e.appendErrors=I,e.get=m,e.set=G,e.useController=$,e.useFieldArray=function(e){const t=j(),{control:r=t.control,name:a,keyName:n="id",shouldUnregister:i}=e,[o,u]=s.default.useState(r._getFieldArray(a)),l=s.default.useRef(r._getFieldArray(a).map(K)),c=s.default.useRef(o),d=s.default.useRef(a),f=s.default.useRef(!1);d.current=a,c.current=o,r._names.array.add(a),M({callback:s.default.useCallback(({values:e,name:t})=>{if(t===d.current||!t){const t=m(e,d.current,[]);u(t),l.current=t.map(K)}},[]),subject:r._subjects.array});const y=s.default.useCallback(e=>{f.current=!0,r._updateFieldArray(a,e)},[r,a]);return s.default.useEffect(()=>{r._stateFlags.action=!1,X(a,r._names)&&r._subjects.state.next({}),f.current&&r._executeSchema([a]).then(e=>{const t=m(e.errors,a),s=m(r._formState.errors,a);(s?!t&&s.type:t&&t.type)&&(t?G(r._formState.errors,a,t):oe(r._formState.errors,a),r._subjects.state.next({errors:r._formState.errors}))}),r._subjects.watch.next({name:a,values:r._formValues}),r._names.focus&&J(r._fields,e=>e.startsWith(r._names.focus)),r._names.focus="",r._proxyFormState.isValid&&r._updateValid()},[o,a,r]),s.default.useEffect(()=>(!m(r._formValues,a)&&r._updateFieldArray(a),()=>{(r._options.shouldUnregister||i)&&r.unregister(a)}),[a,r,n,i]),{swap:s.default.useCallback((e,t)=>{const s=r._getFieldArray(a);ie(s,e,t),ie(l.current,e,t),y(s),u(s),r._updateFieldArray(a,s,ie,{argA:e,argB:t},!1)},[y,a,r]),move:s.default.useCallback((e,t)=>{const s=r._getFieldArray(a);se(s,e,t),se(l.current,e,t),y(s),u(s),r._updateFieldArray(a,s,se,{argA:e,argB:t},!1)},[y,a,r]),prepend:s.default.useCallback((e,t)=>{const s=B(ee(e)),n=ae(r._getFieldArray(a),s);r._names.focus=Q(a,0,t),l.current=ae(l.current,s.map(K)),y(n),u(n),r._updateFieldArray(a,n,ae,{argA:te(e)})},[y,a,r]),append:s.default.useCallback((e,t)=>{const s=B(ee(e)),n=Y(r._getFieldArray(a),s);r._names.focus=Q(a,n.length-1,t),l.current=Y(l.current,s.map(K)),y(n),u(n),r._updateFieldArray(a,n,Y,{argA:te(e)})},[y,a,r]),remove:s.default.useCallback(e=>{const t=ne(r._getFieldArray(a),e);l.current=ne(l.current,e),y(t),u(t),r._updateFieldArray(a,t,ne,{argA:e})},[y,a,r]),insert:s.default.useCallback((e,t,s)=>{const n=B(ee(t)),i=re(r._getFieldArray(a),e,n);r._names.focus=Q(a,e,s),l.current=re(l.current,e,n.map(K)),y(i),u(i),r._updateFieldArray(a,i,re,{argA:e,argB:te(t)})},[y,a,r]),update:s.default.useCallback((e,t)=>{const s=ee(t),n=ue(r._getFieldArray(a),e,s);l.current=[...n].map((t,r)=>t&&r!==e?l.current[r]:K()),y(n),u([...n]),r._updateFieldArray(a,n,ue,{argA:e,argB:s},!0,!1)},[y,a,r]),replace:s.default.useCallback(e=>{const t=B(ee(e));l.current=t.map(K),y([...t]),u([...t]),r._updateFieldArray(a,[...t],e=>e,{},!0,!1)},[y,a,r]),fields:s.default.useMemo(()=>o.map((e,t)=>({...e,[n]:l.current[t]||K()})),[o,n])}},e.useForm=function(e={}){const t=s.default.useRef(),[r,a]=s.default.useState({isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touchedFields:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!1,errors:{}});t.current?t.current.control._options=e:t.current={...Me(e),formState:r};const n=t.current.control,i=s.default.useCallback(e=>{T(e,n._proxyFormState,!0)&&(n._formState={...n._formState,...e},a({...n._formState}))},[n]);return M({subject:n._subjects.state,callback:i}),s.default.useEffect(()=>{n._stateFlags.mount||(n._proxyFormState.isValid&&n._updateValid(),n._stateFlags.mount=!0),n._stateFlags.watch&&(n._stateFlags.watch=!1,n._subjects.state.next({})),n._removeUnmounted()}),t.current.formState=E(r,n._proxyFormState),t.current},e.useFormContext=j,e.useFormState=N,e.useWatch=P,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map