Skip to content

Commit

Permalink
fix: unbounded warning for use steps
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Feb 24, 2024
1 parent 2ac836e commit 67d9e29
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions packages/components/src/stepper/use-steps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react"

export type UseStepsProps = {
export interface UseStepsProps {
index?: number
count?: number
}
Expand All @@ -15,32 +15,34 @@ export function useSteps(props: UseStepsProps = {}) {
const maxStep = typeof count === "number" ? count - 1 : 0
const activeStepPercent = activeStep / maxStep

const isActiveStep = (step: number) => step === activeStep
const isCompleteStep = (step: number) => step < activeStep
const isIncompleteStep = (step: number) => step > activeStep

const getStatus = (step: number): StepStatus => {
if (step < activeStep) return "complete"
if (step > activeStep) return "incomplete"
return "active"
}

const goToNext = () => {
setActiveStep((step) => Math.min(maxStep, step + 1))
}

const goToPrevious = () => {
setActiveStep((step) => Math.max(0, step - 1))
}

return {
activeStep,
setActiveStep,
activeStepPercent,
isActiveStep(step: number) {
return step === activeStep
},
isCompleteStep(step: number) {
return step < activeStep
},
isIncompleteStep(step: number) {
return step > activeStep
},
getStatus(step: number): StepStatus {
if (step < activeStep) return "complete"
if (step > activeStep) return "incomplete"
return "active"
},
goToNext() {
setActiveStep((step) => {
return typeof count === "number" ? Math.min(count, step + 1) : step + 1
})
},
goToPrevious() {
setActiveStep((step) => Math.max(0, step - 1))
},
isActiveStep,
isCompleteStep,
isIncompleteStep,
getStatus,
goToNext,
goToPrevious,
}
}

Expand Down

0 comments on commit 67d9e29

Please sign in to comment.