niceform-hook
Version:
Dynamic workhorse for form in react
2 lines (1 loc) • 11.9 kB
JavaScript
;var e=require("react/jsx-runtime"),t=require("react"),n=require("react-hook-form");const r=Object.prototype.hasOwnProperty;function o(e,t){let n,i;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((i=e.length)===t.length)for(;i--&&o(e[i],t[i]););return-1===i}if(!n||"object"==typeof e){for(n in i=0,e){if(r.call(e,n)&&++i&&!r.call(t,n))return!1;if(!(n in t)||!o(e[n],t[n]))return!1}return Object.keys(t).length===i}}return e!=e&&t!=t}const i=function(n){const r=t.createContext(n);var o;return delete r.Consumer,r.Provider=(o=r.Provider,({value:n,children:r})=>{const i=t.useRef(n),u=t.useRef(new Set),s=t.useRef({value:i,registerListener:e=>(u.current.add(e),()=>u.current.delete(e))});return t.useEffect((()=>{i.current=n,u.current.forEach((e=>{e(n)}))}),[n]),e.jsx(o,{value:s.current,children:r})}),r}(null);function u(e,n=!1){return function(e,n,r=!1){const{value:i,registerListener:u}=t.useContext(e),s=t.useRef(n),[l,c]=t.useState((()=>n(i.current))),a=t.useRef(l);return a.current=l,t.useEffect((()=>{s.current=n})),t.useEffect((()=>u((e=>{const t=s.current(e);(r?o:Object.is)(a.current,t)||c((()=>t))}))),[u,i,r]),l}(i,e,n)}function s(e){const n=t.useRef();return u((t=>(n.current=e(t),n)))}function l(e){const t={};return function e(n,r){var o;for(const i in n){const u=r?`${r}.${i}`:i;(null===(o=n[i])||void 0===o?void 0:o.constructor)==={}.constructor||Array.isArray(n[i])?e(n[i],u):t[u]=n[i]}}(e,""),t}function c({fields:e,values:t}){const n=l(t),r=[...e.values()].reduce(((e,t)=>{if(!t.name||!t.output||!1===t.active)return e;const r=n[t.name];return e[t.name]=t.output(r),e}),{});return function(e){const t={};for(const n in e){const r=n.split(".");let o=t;for(let e=0;e<r.length-1;e++){const t=r[e];o[t]||(o[t]=isNaN(Number(r[e+1]))?{}:[]),o=o[t]}o[r.at(-1)]=e[n]}return t}({...n,...r})}const a=e=>"object"==typeof e?!1!==e.active:!1!==e;const d=e=>!(e=>null==e)(e)&&!Array.isArray(e)&&(e=>"object"==typeof e)(e)&&!(e=>e instanceof Date)(e),f=e=>d(e)&&e.target?"checkbox"===e.target.type?e.target.checked:e.target.value:e;function m(e,n=400,r=!0){const[o,i]=t.useState(e.field.value),s=u((e=>e.form.control.debounceSubmitDefinitions)),l=t.useMemo((()=>function(e,t){let n=0;return function(...r){clearTimeout(n),n=setTimeout(e.bind(this,...r),t||0)}}(e.field.onChange,n)),[n,e.field.onChange]),c=t.useCallback((e=>{const t=f(e);i(t),l(t),s.set(n)}),[i,l,n]);if(!r)return;const a=e.field.value;t.useEffect((()=>{Object.is(o,a)||i(a)}),[a]),e.field.onChange=c,e.field.value=o}function g(e){const{control:t}=u((e=>e.form.methods));return n.useController({control:t,...e})}const h={};function b(e,n){var r;n||(n=h);const o=u((e=>e.form.methods)),i=u((e=>e.form.control.errorsControl)),l=u((e=>e.form.control.fieldsRegistered)),c=(i||[]).reduce(((t,n,r)=>{const i=l.get(e);return i?(t["errors-control-"+r]=(e,t)=>n({value:e,formValues:t,fieldsRegistered:l,methods:o,field:i}),t):t}),{}),d=s((t=>{var r,o,i,u;const s=t.form.getField(e);return s?{input:null!==(r=s.input)&&void 0!==r?r:n.input,onBlur:null!==(o=s.onBlur)&&void 0!==o?o:n.onBlur,onChange:null!==(i=s.onChange)&&void 0!==i?i:n.onChange,validate:null!==(u=s.validate)&&void 0!==u?u:n.validate}:{}})),{rules:f,others:b}=u((t=>{var r,o,i,u,s,l,c,a,d,f,m,g,h,b,v,p,y,C,S,R;const D=t.form.getField(e);if(!D)return{rules:{},others:{}};const x=null!==(s=null!==(u=null!==(o=null!==(r=D.debounceTime)&&void 0!==r?r:n.debounceTime)&&void 0!==o?o:null===(i=t.form.control.parameters)||void 0===i?void 0:i.debounceTime)&&void 0!==u?u:t.form.control.config.debounceTime)&&void 0!==s?s:400,w=null!==(f=null!==(d=null!==(c=null!==(l=D.enableDebounce)&&void 0!==l?l:n.enableDebounce)&&void 0!==c?c:null===(a=t.form.control.parameters)||void 0===a?void 0:a.enableDebounce)&&void 0!==d?d:t.form.control.config.enableDebounce)&&void 0!==f&&f&&x>0;return{rules:{min:null!==(m=D.min)&&void 0!==m?m:n.min,max:null!==(g=D.max)&&void 0!==g?g:n.max,deps:D.deps,maxLength:null!==(h=D.maxLength)&&void 0!==h?h:n.maxLength,minLength:null!==(b=D.minLength)&&void 0!==b?b:n.minLength,pattern:null!==(v=D.pattern)&&void 0!==v?v:n.pattern,required:null!==(p=D.required)&&void 0!==p?p:n.required,value:null!==(y=D.value)&&void 0!==y?y:n.value},others:{shouldUnregister:null!==(C=D.shouldUnregister)&&void 0!==C?C:n.shouldUnregister,isActive:!1!==(null!==(S=D.active)&&void 0!==S?S:n.active),disabled:null!==(R=D.disabled)&&void 0!==R?R:n.disabled,debounceTime:x,enableDebounce:w}}}),!0),v=g({name:e,shouldUnregister:b.shouldUnregister,rules:b.isActive?{onBlur:(...e)=>{var t,n;return null===(n=null===(t=d.current)||void 0===t?void 0:t.onBlur)||void 0===n?void 0:n.call(t,...e)},onChange:(...e)=>{var t,n;return null===(n=null===(t=d.current)||void 0===t?void 0:t.onChange)||void 0===n?void 0:n.call(t,...e)},shouldUnregister:b.shouldUnregister,deps:f.deps,max:f.max,min:f.min,maxLength:f.maxLength,minLength:f.minLength,pattern:f.pattern,required:f.required,validate:{...c,...null===(r=d.current)||void 0===r?void 0:r.validate},value:f.value}:{},disabled:b.disabled});return function(e){const n=e.field.name,r=s((e=>{var t;return null===(t=e.form.getField(n))||void 0===t?void 0:t.input})),o=u((e=>e.form.methods.setValue)),i=u((e=>e.form.control.fieldsInputedCalled)),l=i.has(n),c=t=>{const o=r.current;let u=e.field.value;!o||l||e.fieldState.isDirty||(u=o(u),void 0!==u&&(i.add(n),e.field.value=u,t(u)))};c((e=>setTimeout(o,100,n,e)));const a=e.field.value;t.useEffect((()=>{c((e=>o(n,e)))}),[a])}(v),m(v,b.debounceTime,b.enableDebounce),function(e){const n=u((t=>{const n=t.form.getField(e.field.name);return a(null==n?void 0:n.active)})),[r,o]=t.useState();n||(e.field={...e.field,onBlur:()=>null,onChange:e=>o(e.target.value),value:r,ref:()=>null})}(v),v}const v=t.memo((function(e){const t=b(e.name);return e.render(t)}));function p(e){const n=t.useRef(e);return n.current=e,n}const y=Symbol("null");let C=0;class S extends Map{constructor(){super(),this._objectHashes=new WeakMap,this._symbolHashes=new Map,this._publicKeys=new Map;const[e]=arguments;if(null!=e){if("function"!=typeof e[Symbol.iterator])throw new TypeError(typeof e+" is not iterable (cannot read property Symbol(Symbol.iterator))");for(const[t,n]of e)this.set(t,n)}}_getPublicKeys(e,t=!1){if(!Array.isArray(e))throw new TypeError("The keys parameter must be an array");const n=this._getPrivateKey(e,t);let r;return n&&this._publicKeys.has(n)?r=this._publicKeys.get(n):t&&(r=[...e],this._publicKeys.set(n,r)),{privateKey:n,publicKey:r}}_getPrivateKey(e,t=!1){const n=[];for(let r of e){null===r&&(r=y);const e="object"==typeof r||"function"==typeof r?"_objectHashes":"symbol"==typeof r&&"_symbolHashes";if(e)if(this[e].has(r))n.push(this[e].get(r));else{if(!t)return!1;{const t=`@@mkm-ref-${C++}@@`;this[e].set(r,t),n.push(t)}}else n.push(r)}return JSON.stringify(n)}set(e,t){const{publicKey:n}=this._getPublicKeys(e,!0);return super.set(n,t)}get(e){const{publicKey:t}=this._getPublicKeys(e);return super.get(t)}has(e){const{publicKey:t}=this._getPublicKeys(e);return super.has(t)}delete(e){const{publicKey:t,privateKey:n}=this._getPublicKeys(e);return Boolean(t&&super.delete(t)&&this._publicKeys.delete(n))}clear(){super.clear(),this._symbolHashes.clear(),this._publicKeys.clear()}get[Symbol.toStringTag](){return"ManyKeysMap"}get size(){return super.size}}exports.NiceformHookProvider=function({children:t,...n}){return e.jsx(i.Provider,{value:n,children:t})},exports.create=function(r){const o=new Map;for(const e in r.components)o.set(e,r.components[e]);return function(i){const u=t.useRef({fieldsRegistered:new Map,componentsDefinitions:new Map(o),errorsControl:r.errorsControl,debounceSubmitDefinitions:{debounceRegistry:{registeredAt:0,time:0},set(e){this.debounceRegistry.registeredAt=Date.now(),this.debounceRegistry.time=e},getRemainingTime(){const e=Date.now()-this.debounceRegistry.registeredAt;return Math.max(0,this.debounceRegistry.time-e+50)},isActiveDebounce(){return Date.now()-this.debounceRegistry.registeredAt<this.debounceRegistry.time}},fieldsInputedCalled:new Set}),s=t.useMemo((()=>function(){let e=0;return function(t,n,...r){clearTimeout(e),e=setTimeout(t.bind(this,r),n||0)}}()),[]),a=function(){const e=t.useRef(new S);return t.useCallback((function(t,n){let r=e.current.get(n);return r||("function"==typeof t?e.current.set(n,t()):e.current.set(n,t),r=e.current.get(n)),r}),[])}(),d=function(){const e=t.useRef(new S);return t.useCallback((function(t,n){let r=e.current.get(n);return r||(e.current.set(n,t),r=e.current.get(n)),r}),[])}(),f=n.useForm(i),m=t.useCallback((e=>u.current.fieldsRegistered.get(e)),[]);!function({getField:e,methods:n,onErrorDuringSubmit:r}){t.useEffect((()=>{const t=n.formState.errors;Object.keys(t).length&&n.formState.submitCount&&(null==r||r(n.formState.errors,{methods:n,getField:e}))}),[n.formState.submitCount])}({getField:m,methods:f,onErrorDuringSubmit:r.onErrorDuringSubmit}),function(e){const t=l(e.initialValues);for(const n in t){const r=t[n];void 0!==r&&void 0===e.methods.getValues(n)&&(e.methods.register(n),e.methods.setValue(n,r))}}({methods:f,initialValues:(null==i?void 0:i.initialValues)||{}}),function(e){const{methods:n,optionsWhenCleaning:r}=e,o=p({methods:n,optionsWhenCleaning:r}),i=n.watch;t.useEffect((()=>{const t=i(((t,{name:n,type:r})=>{if(void 0===n)return;const i=o.current.methods,u=o.current.optionsWhenCleaning,s=e.fields,l=i.getValues(n),c=null==u?void 0:u.shouldCleanChain;if(void 0===l&&!c)return;const a=function(e,t){const n=[...t.values()],r=t.get(e);return r?n.reduce(((e,t)=>(r.name&&t.dependsOnToClear&&[t.dependsOnToClear].flat().includes(r.name)&&t.name!==r.name&&e.push(t),e)),[]):[]}(n,s||[]);a.forEach((e=>{const t=i.getValues(e.name);e.name&&void 0!==t&&i.setValue(e.name,null,u)}))}));return()=>t.unsubscribe()}),[i,o])}({methods:f,get fields(){return u.current.fieldsRegistered}}),function(e){const n=t.useRef(null),r=p(e.onChangeField),o=e.methods.watch,i=e.methods;n.current||(n.current=new Map),t.useEffect((()=>{const t=o(((t,{name:o,type:u})=>{if(void 0===o||!r.current)return;const s=e.fields.get(o);if(!s)return;const l=i.getValues(o);Object.is(n.current.get(o),l)||(n.current.set(o,l),r.current(s,l))}));return()=>t.unsubscribe()}),[o,r,i])}({methods:f,get fields(){return u.current.fieldsRegistered},onChangeField:null==i?void 0:i.onChangeField});const g=t.useCallback((t=>{const{render:n,...r}=function(e,t){return"function"!=typeof e?e:e(t)}(t,{getField:m,methods:f}),o=r.type,i=r.name;if(!i)return null;if(u.current.fieldsRegistered.set(i,r),"node"===o&&n)return s={name:i,render:n},e.jsx(v,{...s});var s;const l=u.current.componentsDefinitions.get(o);return null==l?void 0:l.render(r)}),[f,m]),h=t.useCallback((e=>e.filter(Boolean).map(g)),[g]);if(!f.handleSubmit.prototype){const e=f.handleSubmit;f.handleSubmit=function(t,n){const o=e((async(e,n)=>{const r=c({fields:u.current.fieldsRegistered,values:e});t(r,n)}),n);return async function(e){var t;if(!((null!==(t=null==i?void 0:i.enableDebounceOnSubmit)&&void 0!==t?t:r.enableDebounceOnSubmit)&&u.current.debounceSubmitDefinitions.isActiveDebounce()))return o(e);const n=u.current.debounceSubmitDefinitions.getRemainingTime();null==e||e.preventDefault(),s((()=>{o(e)}),n)}}}const b=t.useRef({methods:f,renderField:g,renderFields:h,getField:m,memoize:a,memoizeCallback:d,control:{get errorsControl(){return u.current.errorsControl},get fieldsRegistered(){return u.current.fieldsRegistered},get fieldsInputedCalled(){return u.current.fieldsInputedCalled},get debounceSubmitDefinitions(){return u.current.debounceSubmitDefinitions},get parameters(){return i},get config(){return r}}});return b.current.renderField=g,b.current.renderFields=h,b.current}},exports.useField=b,exports.useNiceformContext=function(){return u((e=>e.form))},exports.useNiceformHookContext=u;//# sourceMappingURL=index.cjs.js.map