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

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