@visa/nova-react
Version:
Visa Product Design System Nova React library
18 lines (17 loc) • 1.95 kB
JavaScript
/**
* Copyright (c) 2025 Visa, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
import{useState as e}from"react";const t={autoProgressComplete:!0,autoProgressError:!1,defaultActiveStep:0,isExclusiveSets:!0},r=r=>{const{autoProgressComplete:s,autoProgressError:o,isExclusiveSets:a,length:n,defaultActiveStep:i}={...t,...r},[S,p]=e((()=>new Set)),[l,u]=e(i),[d,c]=e((()=>new Set)),[m,g]=e(i),v=e=>{let t=e;e<0?t=0:e>=n&&(t=n-1),g((e=>Math.max(e,t))),u(t)},w=()=>{v(l+1)};return{currentStep:l,hasError:()=>d.size>0,isStepAvailable:e=>e<=m,isFirstStep:e=>0===e,isLastStep:e=>e===n-1,isStepComplete:e=>S.has(e),isStepError:e=>d.has(e),isWizardComplete:()=>S.size===n,maxStep:m,onStepChange:v,onStepNext:w,onStepPrevious:()=>{v(l-1)},onStepComplete:(e,{autoProgress:t}={})=>{p((t=>new Set(t).add(e))),a&&c((t=>{const r=new Set(t);return r.delete(e),r})),(void 0===t&&s||t)&&w()},onStepError:(e,{autoProgress:t}={})=>{c((t=>new Set(t).add(e))),a&&p((t=>{const r=new Set(t);return r.delete(e),r})),(void 0===t&&o||t)&&w()},onStepReset:e=>{c((t=>{const r=new Set(t);return r.delete(e),r})),p((t=>{const r=new Set(t);return r.delete(e),r}))},onWizardReset:(e=i)=>{p((t=>new Set([...t].filter((t=>t<e))))),c((t=>new Set([...t].filter((t=>t<e))))),u(e),g(e)}}};r.displayName="useWizard",r.defaultProps={autoProgressComplete:!0,autoProgressError:!1,defaultActiveStep:0,isExclusiveSets:!0};export{r as default,r as useWizard};