@d3vtool/hooks
Version:
Collection of custom React hooks to simplify tasks in your React projects.
2 lines (1 loc) • 3.96 kB
JavaScript
var S=function(h,o,n,s){function i(r){return r instanceof n?r:new n(function(l){l(r)})}return new(n||(n=Promise))(function(r,l){function d(c){try{e(s.next(c))}catch(g){l(g)}}function b(c){try{e(s.throw(c))}catch(g){l(g)}}function e(c){c.done?r(c.value):i(c.value).then(d,b)}e((s=s.apply(h,o||[])).next())})},j=function(h,o){var n={label:0,sent:function(){if(r[0]&1)throw r[1];return r[1]},trys:[],ops:[]},s,i,r,l=Object.create((typeof Iterator=="function"?Iterator:Object).prototype);return l.next=d(0),l.throw=d(1),l.return=d(2),typeof Symbol=="function"&&(l[Symbol.iterator]=function(){return this}),l;function d(e){return function(c){return b([e,c])}}function b(e){if(s)throw new TypeError("Generator is already executing.");for(;l&&(l=0,e[0]&&(n=0)),n;)try{if(s=1,i&&(r=e[0]&2?i.return:e[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,e[1])).done)return r;switch(i=0,r&&(e=[e[0]&2,r.value]),e[0]){case 0:case 1:r=e;break;case 4:return n.label++,{value:e[1],done:!1};case 5:n.label++,i=e[1],e=[0];continue;case 7:e=n.ops.pop(),n.trys.pop();continue;default:if(r=n.trys,!(r=r.length>0&&r[r.length-1])&&(e[0]===6||e[0]===2)){n=0;continue}if(e[0]===3&&(!r||e[1]>r[0]&&e[1]<r[3])){n.label=e[1];break}if(e[0]===6&&n.label<r[1]){n.label=r[1],r=e;break}if(r&&n.label<r[2]){n.label=r[2],n.ops.push(e);break}r[2]&&n.ops.pop(),n.trys.pop();continue}e=o.call(h,n)}catch(c){e=[6,c],i=0}finally{s=r=0}if(e[0]&5)throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}},C=function(h,o){var n=typeof Symbol=="function"&&h[Symbol.iterator];if(!n)return h;var s=n.call(h),i,r=[],l;try{for(;(o===void 0||o-- >0)&&!(i=s.next()).done;)r.push(i.value)}catch(d){l={error:d}}finally{try{i&&!i.done&&(n=s.return)&&n.call(s)}finally{if(l)throw l.error}}return r};import{useDebounce as E}from"../useDebounce";import{ObjectValidationError as U}from"@d3vtool/utils";import{useCallback as y,useMemo as B,useRef as _,useState as G}from"react";export function useForm(h,o){var n=C(G(!1),2),s=n[0],i=n[1],r=_(h),l=_({}),d=_([]),b=y(function(){for(var t=[],a=0;a<d.current.length;++a){var u=d.current[a]();t.push(u)}for(var v=o.validateSafely(r.current),a=0;a<t.length;++a){var f=t[a];if(console.log(f),f.hideErrorMsg){c.current[f.key]="";continue}c.current[f.key]=v[f.key][0]}i(function(w){return!w}),d.current=[]},[]),e=E(300,b),c=_(Object.keys(h).reduce(function(t,a){return t[a]="",t},{}));function g(t){if(t.type!=="focus"){var a=t.target.name;if(!(c.current[a].length>0)){var u=o.validateSafely(r.current);m(a,u)}}}function m(t,a){var u=a[t];u?.length>0&&u[0]!==c.current[t]?(c.current[t]=u[0],i(function(v){return!v})):u===void 0&&c.current[t].length>0&&(c.current[t]="",i(function(v){return!v}))}function I(t){var a=t.target,u=a.name,v=a.value;r.current[u]=v;var f=o.validateSafely(r.current);m(u,f)}var p=y(function(t){return function(a){return S(this,void 0,void 0,function(){var u,v;return j(this,function(f){switch(f.label){case 0:a.preventDefault(),f.label=1;case 1:return f.trys.push([1,3,,4]),o.validate(r.current),[4,t()];case 2:return f.sent(),[3,4];case 3:return u=f.sent(),u instanceof U&&(((v=u.message)===null||v===void 0?void 0:v.length)>0?(c.current[u.key]=u.message,i(function(w){return!w})):c.current[u.key].length>0&&(c.current[u.key]="",i(function(w){return!w}))),[3,4];case 4:return[2]}})})}},[]),F=y(function(t,a,u){e(),d.current.push(function(){return l.current[t].value=a,r.current[t]=a,{key:t,hideErrorMsg:u??!1}})},[o]),k=y(function(t){return r.current[t]},[o]),x=y(function(t){var a=t.target,u=a.name,v=a.value;r.current[u]=v;var f=o.validateSafely(r.current);m(u,f)},[o]),D=y(function(t){t&&(l.current[t.name]=t)},[o]),O=y(function(t){t===void 0&&(t=!1);for(var a in r.current)l.current[a].value="",r.current[a]="",t||(c.current[a]="");i(function(u){return!u})},[]),R=B(function(){return{onChange:x,onBlur:g,ref:D}},[o]);return{listeners:R,formData:r.current,onSubmit:p,formErrors:c.current,resetFields:O,getFormData:k,setFormData:F}}
;