UNPKG

@hudoro/hooks

Version:

Useful react hooks for HSD App

2 lines (1 loc) 4.54 kB
(function(c,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("react"),require("react-dom")):typeof define=="function"&&define.amd?define(["exports","react","react-dom"],r):(c=typeof globalThis<"u"?globalThis:c||self,r(c["@hudoro/hooks"]={},c.React,c.ReactDOM))})(this,function(c,r,I){"use strict";function F(e=!1){if(typeof e!="boolean")throw new Error("defaultValue must be `true` or `false`");const[s,n]=r.useState(e),t=r.useCallback(()=>{n(!0)},[]),f=r.useCallback(()=>{n(!1)},[]),l=r.useCallback(()=>{n(u=>!u)},[]);return{value:s,setValue:n,setTrue:t,setFalse:f,toggle:l}}const L=typeof window<"u"?r.useLayoutEffect:r.useEffect;function N(e){const[s,n]=r.useState({show:!1}),t=d=>{n({data:d,show:!0})},f=()=>{n({show:!1})},l=r.useCallback(()=>{if(e!=null&&e.onCancelAsync){e.onCancelAsync().then(()=>{n({show:!1})}).catch(d=>{e.onError&&e.onError(d)});return}e!=null&&e.onCancel&&e.onCancel(),n({show:!1})},[e]),u=r.useCallback(()=>{if(e!=null&&e.onConfirmAsync){e.onConfirmAsync(s.data).then(()=>{n({show:!1})}).catch(d=>{e.onError&&e.onError(d)});return}e!=null&&e.onConfirm&&e.onConfirm(s.data),n({show:!1})},[e,s.data]);return{...s,open:t,close:f,save:d=>{n(w=>({...w,data:d}))},confirm:u,cancel:l}}function M(e){const[s,n]=r.useState(e??0),t=r.useCallback(()=>{n(u=>u+1)},[]),f=r.useCallback(()=>{n(u=>u-1)},[]),l=r.useCallback(()=>{n(e??0)},[e]);return{count:s,increment:t,decrement:f,reset:l,setCount:n}}function J(e,s){const n=r.useRef(e);L(()=>{n.current=e},[e]),r.useEffect(()=>{if(s===null)return;const t=setInterval(()=>{n.current()},s);return()=>{clearInterval(t)}},[s])}function A({countStart:e,countStop:s=0,intervalMs:n=1e3,isIncrement:t=!1}){const[f,l]=r.useState(!1),{count:u,increment:h,decrement:d,reset:w,setCount:j}=M(e),{value:g,setTrue:E,setFalse:C}=F(!1),y=r.useCallback(()=>{C(),w()},[C,w]),v=r.useCallback(()=>{if(u===s){C();return}t?h():d()},[u,s,d,h,t,C]);return r.useEffect(()=>{l(!u)},[u]),J(v,g?n:null),{count:u,startCountdown:E,stopCountdown:C,resetCountdown:y,completed:f,setCompleted:l,setCount:j}}function Q(e,s){const[n,t]=r.useState(e);return r.useEffect(()=>{const f=setTimeout(()=>{t(e)},s);return()=>{clearTimeout(f)}},[e,s]),n}var T=(e=>(e.sm="40rem",e.md="48rem",e.lg="64rem",e.xl="80rem",e["2xl"]="96rem",e))(T||{});function V(e){const s=T[e];return s?`(min-width: ${s})`:e}function q(e){const s=r.useMemo(()=>V(e),[e]),n=r.useCallback(l=>{const u=window.matchMedia(s);return u.addEventListener("change",l),()=>{u.removeEventListener("change",l)}},[s]),t=()=>window.matchMedia(s).matches,f=()=>{throw Error("useMediaQuery is a client-only hook")};return r.useSyncExternalStore(n,t,f)}function K(e){const s=r.useRef(e);s.current=e,r.useEffect(()=>()=>{s.current()},[])}function P(e){const s=Object.keys(e),n={};return s.forEach(t=>{n[t]=""}),n}function $(e,s){const n=(s==null?void 0:s.validationState)??P(e),[t,f]=r.useState(e),[l,u]=r.useState(n),[h,d]=r.useState(Object.keys(e).reduce((o,a)=>(o[a]=!1,o),{})),[w,j]=r.useState(!1),g=r.useCallback((o,a)=>{if(s!=null&&s.validationCallback){const m={...t,[o]:a},O=s.validationCallback(m,{...n});u(b=>({...b,[o]:O[o]}))}},[s,t,n]),E=e&&JSON.stringify(e),C=t&&JSON.stringify(t),y=n&&JSON.stringify(n),v=Object.values(l).every(o=>!o),G=(o,a)=>{f(m=>({...m,[o]:a})),d(m=>({...m,[o]:!0})),w&&g(o,a)},H=(o,a)=>{u(m=>({...m,[o]:a}))},i=r.useCallback(()=>{u(n),d(Object.keys(e).reduce((o,a)=>(o[a]=!1,o),{}))},[n]),D=()=>{f(e),i()},U=r.useCallback((o=!1)=>{if(j(!0),s!=null&&s.validationCallback){const a={...n},m=s.validationCallback(t,a),O=Object.keys(m).reduce((b,W)=>{const k=W;return o||h[k]?b[k]=m[k]:b[k]="",b},{});return u(O),Object.values(O).every(b=>!b)}return i(),!0},[t,n,s,i,h]),S=r.useRef();return S.current=U,r.useEffect(()=>{var o;(o=S.current)==null||o.call(S)},[C,y]),r.useEffect(()=>{E&&(f(JSON.parse(E)),u(JSON.parse(y)))},[E,y]),{values:t,errors:l,setValue:G,setValues:f,reset:D,setErrors:u,valid:v,setError:H,resetErrors:i,validate:U,resetForm:()=>{I.flushSync(()=>{D()}),i()}}}function z(){return{downloadFile:r.useCallback((s,n,t)=>{const f=new Blob([s],{type:t}),l=window.URL.createObjectURL(f),u=document.createElement("a");u.href=l,u.download=n,document.body.appendChild(u),u.click(),window.URL.revokeObjectURL(l),document.body.removeChild(u)},[])}}c.useBoolean=F,c.useConfirmation=N,c.useCountdown=A,c.useCounter=M,c.useDebounce=Q,c.useDownload=z,c.useForm=$,c.useInterval=J,c.useIsomorphicLayoutEffect=L,c.useMediaQuery=q,c.useUnmount=K,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});