UNPKG

react-use-wizard

Version:

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

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