UNPKG

react-use-wizard

Version:

React wizard (stepper) builder without the hassle, powered by hooks.

3 lines (2 loc) 1.23 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");const t=e.createContext(null);exports.Wizard=e.memo(({header:r,footer:n,children:u,onStepChange:s,wrapper:c,startIndex:l=0})=>{const[a,o]=e.useState(l),[i,p]=e.useState(!1),d=e.useRef(!0),h=e.useRef(!1),C=e.useRef(()=>{}),S=e.Children.toArray(u).length;d.current=a<S-1,h.current=a>0;const f=e.useCallback(()=>{if(d.current){const e=a+1;o(e),null==s||s(e)}},[a,s]),m=e.useCallback(()=>{if(h.current){C.current=null;const e=a-1;o(e),null==s||s(e)}},[a,s]),x=e.useCallback(e=>{e>=0&&e<S&&(C.current=null,o(e),null==s||s(e))},[S,s]),v=e.useRef(e=>{C.current=e}),b=e.useCallback(async()=>{if(d.current&&C.current)try{p(!0),await C.current(),p(!1),C.current=null,f()}catch(e){throw p(!1),e}else f()},[f]),y=e.useMemo(()=>({nextStep:b,previousStep:m,handleStep:v.current,isLoading:i,activeStep:a,stepCount:S,isFirstStep:!h.current,isLastStep:!d.current,goToStep:x}),[b,m,i,a,S,x]),g=e.useMemo(()=>e.Children.toArray(u)[a],[a,u,r,n]),k=e.useMemo(()=>c?e.cloneElement(c,{children:g}):g,[c,g]);return e.createElement(t.Provider,{value:y},r,k,n)}),exports.useWizard=()=>e.useContext(t); //# sourceMappingURL=react-use-wizard.cjs.production.min.js.map