UNPKG

@fddayan/react-use-wizard

Version:

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

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