-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { type Dispatch, type SetStateAction, useCallback, useState } from "react"; | ||
|
||
type UseStepActions = { | ||
goToNextStep: () => void; | ||
goToPrevStep: () => void; | ||
reset: () => void; | ||
canGoToNextStep: boolean; | ||
canGoToPrevStep: boolean; | ||
setStep: Dispatch<SetStateAction<number>>; | ||
}; | ||
|
||
type SetStepCallbackType = (step: number | ((step: number) => number)) => void; | ||
|
||
export function useStep(maxStep: number): [number, UseStepActions] { | ||
const [currentStep, setCurrentStep] = useState(1); | ||
|
||
const canGoToNextStep = currentStep + 1 <= maxStep; | ||
const canGoToPrevStep = currentStep - 1 > 0; | ||
|
||
const setStep = useCallback<SetStepCallbackType>( | ||
(step) => { | ||
// Allow value to be a function so we have the same API as useState | ||
const newStep = step instanceof Function ? step(currentStep) : step; | ||
|
||
if (newStep >= 1 && newStep <= maxStep) { | ||
setCurrentStep(newStep); | ||
return; | ||
} | ||
|
||
throw new Error("Step not valid"); | ||
}, | ||
[maxStep, currentStep], | ||
); | ||
|
||
const goToNextStep = useCallback(() => { | ||
if (canGoToNextStep) { | ||
setCurrentStep((step) => step + 1); | ||
} | ||
}, [canGoToNextStep]); | ||
|
||
const goToPrevStep = useCallback(() => { | ||
if (canGoToPrevStep) { | ||
setCurrentStep((step) => step - 1); | ||
} | ||
}, [canGoToPrevStep]); | ||
|
||
const reset = useCallback(() => { | ||
setCurrentStep(1); | ||
}, []); | ||
|
||
return [ | ||
currentStep, | ||
{ | ||
goToNextStep, | ||
goToPrevStep, | ||
canGoToNextStep, | ||
canGoToPrevStep, | ||
setStep, | ||
reset, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters