@visa/nova-react
Version:
Visa Product Design System Nova React library
18 lines (17 loc) • 2.05 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.
*
**/
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");const t={autoProgressComplete:!0,autoProgressError:!1,defaultActiveStep:0,isExclusiveSets:!0},r=r=>{const{autoProgressComplete:s,autoProgressError:o,isExclusiveSets:a,length:u,defaultActiveStep:S}={...t,...r},[i,n]=e.useState((()=>new Set)),[p,l]=e.useState(S),[d,c]=e.useState((()=>new Set)),[v,P]=e.useState(S),g=e=>{let t=e;e<0?t=0:e>=u&&(t=u-1),P((e=>Math.max(e,t))),l(t)},w=()=>{g(p+1)};return{currentStep:p,hasError:()=>d.size>0,isStepAvailable:e=>e<=v,isFirstStep:e=>0===e,isLastStep:e=>e===u-1,isStepComplete:e=>i.has(e),isStepError:e=>d.has(e),isWizardComplete:()=>i.size===u,maxStep:v,onStepChange:g,onStepNext:w,onStepPrevious:()=>{g(p-1)},onStepComplete:(e,{autoProgress:t}={})=>{n((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&&n((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})),n((t=>{const r=new Set(t);return r.delete(e),r}))},onWizardReset:(e=S)=>{n((t=>new Set([...t].filter((t=>t<e))))),c((t=>new Set([...t].filter((t=>t<e))))),l(e),P(e)}}};r.displayName="useWizard",r.defaultProps={autoProgressComplete:!0,autoProgressError:!1,defaultActiveStep:0,isExclusiveSets:!0},exports.default=r,exports.useWizard=r;