Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 41x 78x 41x 41x 41x 41x 41x 41x 6x 6x 41x 41x 28x 28x 41x 8x 8x 1x 2x 1x 41x 3x 41x 41x 246x 208x 246x 246x 41x 41x 6x 41x 123x 41x | import { useEffect, useImperativeHandle, useMemo, useState } from "react";
import { KotoStep } from "../types";
import { useSearchParams } from "react-router-dom";
import { personalDetailsStepIndex } from "../helpers";
export function useKotoStepper({
steps,
defaultStep,
setIsLoaded,
actionOnClick,
setStep,
automateLast,
disableLastClick,
overriderActiveStep,
alwaysEnable = false,
disableWithoutStyle,
isClickable = true,
kotoStepClickHandler,
ref,
}: any) {
const visibleSteps: KotoStep[] = useMemo(
() => steps.filter(({ hidden }: any) => !hidden),
[steps],
);
const [searchParams] = useSearchParams();
const classificationParam = searchParams.get("classification");
const initialParam = Boolean(classificationParam)
? personalDetailsStepIndex
: 0;
const [activeStep, setActiveStep] = useState(initialParam);
const [lastStepValue, setLastStepValue] = useState(0);
useEffect(() => {
setIsLoaded?.(true);
setStep?.(defaultStep);
}, []);
const effectiveActiveStep =
overriderActiveStep !== undefined && overriderActiveStep > -1
? overriderActiveStep
: activeStep;
useEffect(() => {
Iif (visibleSteps.length === 0) return; // guard
setStep?.(visibleSteps[effectiveActiveStep]?.label);
}, [effectiveActiveStep, visibleSteps]);
useEffect(() => {
Iif (!automateLast) return;
if (effectiveActiveStep !== visibleSteps.length - 1) return;
const interval = setInterval(() => {
setLastStepValue((v) => (v < 100 ? v + 1 : v));
}, 50);
return () => clearInterval(interval);
}, [effectiveActiveStep, automateLast]);
const handleNext = () =>
setActiveStep((s) => Math.min(s + 1, visibleSteps.length - 1));
const handleBack = () => setActiveStep((s) => Math.max(s - 1, 0));
const canActivateStep = (index: number) => {
const isPrevCompleted =
index === 0 ||
alwaysEnable ||
visibleSteps.slice(0, index).every((step: any) => step.barValue === 100);
const isLastDisabled =
visibleSteps.length - 1 === index && disableLastClick;
return (
isClickable && isPrevCompleted && !isLastDisabled && !disableWithoutStyle
);
};
const handleStepClick = (index: number) => {
if (!canActivateStep(index)) return;
setActiveStep(index);
actionOnClick?.(index + 1);
setStep?.(visibleSteps[index]?.label);
kotoStepClickHandler?.();
};
useImperativeHandle(
ref,
() => ({
handleNext,
handleBack,
setActiveStep,
}),
[],
);
const getStepValue = (index: number) =>
automateLast && index === visibleSteps.length - 1
? lastStepValue
: visibleSteps[index].barValue;
return {
activeStep: effectiveActiveStep,
visibleSteps,
getStepValue,
canActivateStep,
handleStepClick,
};
}
|