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.

3 lines
24 KiB

2 years ago
!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.touchedF
//# sourceMappingURL=index.umd.js.map