All files / src/components/common/SignUp KotoStepper.tsx

72% Statements 36/50
63.63% Branches 35/55
68.42% Functions 13/19
72.09% Lines 31/43

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                          25x                                             144x 144x 144x   429x 144x   144x 22x 22x     144x 37x         8x       144x 3x     144x       240x                             144x 33x     144x 33x                       144x     22x               144x       429x   429x   240x       163x     240x       240x                                                                                                   25x               25x      
import { Button } from "@common/Button";
import { ITextProps } from "@common/Text/Text";
import {
  Box,
  Step,
  Stepper,
  StepperProps,
  useMediaQuery,
  useTheme,
} from "@mui/material";
import React, {
  forwardRef,
  useEffect,
  useImperativeHandle,
  useState,
} from "react";
import KotoLinearProgress from "./LinearProgress";
 
interface IStepperProps extends StepperProps {
  customSteps: Array<{ label: string; barValue: number; hidden?: boolean }>;
  isClickable?: boolean;
  defaultStep?: string;
  setIsLoaded?: React.Dispatch<React.SetStateAction<boolean>>;
  actionOnClick?: React.Dispatch<React.SetStateAction<number>>;
  setStep?: React.Dispatch<React.SetStateAction<any>>;
  LinearProgressProps?: ITextProps;
  svgProps?: { width?: number; height?: number };
  automateLast?: boolean;
  disableLastClick?: boolean;
  overriderActiveStep?: number;
  disableWithoutStyle?: boolean;
  alwaysEnable?: boolean;
  kotoStepClickHandler?: () => void;
  isCompletedColor?: boolean;
  showCompletedView?: boolean;
}
 
const KotoStepper = forwardRef(
  (
    {
      customSteps: steps,
      isClickable = true,
      setIsLoaded,
      actionOnClick,
      setStep,
      defaultStep,
      LinearProgressProps,
      svgProps,
      automateLast,
      disableLastClick,
      overriderActiveStep,
      disableWithoutStyle,
      alwaysEnable = false,
      kotoStepClickHandler,
      isCompletedColor,
      showCompletedView = false,
      ...rest
    }: IStepperProps,
    ref,
  ) => {
    const theme = useTheme();
    const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
    const [activeStep, setActiveStep] = useState(0);
 
    const customSteps = steps.filter(({ hidden }) => !hidden);
    const [lastStepValue, setLastStepValue] = useState(0);
 
    useEffect(() => {
      setIsLoaded?.(true);
      setStep && setStep(defaultStep);
    }, []);
 
    useEffect(() => {
      if (
        overriderActiveStep &&
        overriderActiveStep > -1 &&
        overriderActiveStep !== activeStep
      ) {
        setActiveStep(overriderActiveStep);
      }
    }, [overriderActiveStep]);
 
    const handleNext = () => {
      setActiveStep((prev) => prev + 1);
    };
 
    const handleBack = () => {
      setActiveStep((prevActiveStep) => prevActiveStep - 1);
    };
 
    const handleStep = (step: number, isPrevCompleted: boolean) => () => {
      if (
        disableWithoutStyle ||
        !isClickable ||
        !isPrevCompleted ||
        (customSteps.length - 1 === step && disableLastClick)
      )
        return;
 
      setActiveStep(step);
      actionOnClick && actionOnClick(step + 1);
      setStep && setStep(customSteps[step]?.label);
      kotoStepClickHandler && kotoStepClickHandler();
    };
 
    useEffect(() => {
      Eif (setStep) setStep(customSteps[activeStep]?.label);
    }, [activeStep]);
 
    useEffect(() => {
      Iif (automateLast)
        if (activeStep === customSteps.length - 1) {
          const interval = setInterval(() => {
            setLastStepValue((prevValue) =>
              prevValue < 100 ? prevValue + 1 : prevValue,
            );
          }, 50);
 
          return () => clearInterval(interval);
        }
    }, [activeStep, automateLast]);
 
    useImperativeHandle(
      ref,
      (): { handleNext: () => void; handleBack: () => void } => {
        return {
          handleNext,
          handleBack,
        };
      },
      [],
    );
 
    return (
      <Box sx={{ ...rest.sx }}>
        <Stepper nonLinear activeStep={activeStep} sx={{ ...KotoStepperStyle }}>
          {customSteps
            .filter(({ hidden }) => !hidden)
            .map((step, index) => {
              if (!isDesktop && index !== activeStep) return;
              const isPrevCompleted =
                index === 0 ||
                alwaysEnable ||
                customSteps
                  .slice(0, index)
                  .every((step) => step.barValue === 100);
 
              const value =
                index === customSteps.length - 1 && automateLast
                  ? lastStepValue
                  : customSteps[index].barValue;
 
              return (
                <Step
                  sx={{ width: "100%", height: "100%", alignItems: "start" }}
                  key={step?.label}
                >
                  <Button
                    background="tertiary"
                    data-testid={`stepper-${step?.label}-button`}
                    sx={{
                      padding: 0,
                      "&:hover, &:active": { textDecoration: "none" },
                      cursor:
                        isPrevCompleted &&
                        !(
                          customSteps.length - 1 === index && disableLastClick
                        ) &&
                        !disableWithoutStyle
                          ? "pointer"
                          : "default",
                      height: "fit-content",
                    }}
                    fullWidth
                    onClick={handleStep(index, isPrevCompleted)}
                    disabled={
                      !isClickable ||
                      !isPrevCompleted ||
                      (customSteps.length - 1 === index && disableLastClick)
                    }
                  >
                    <Box width={"100%"}>
                      <KotoLinearProgress
                        hasDescription
                        value={value}
                        label={step?.label}
                        isActive={index === activeStep && !showCompletedView}
                        LinearProgressProps={LinearProgressProps}
                        svgProps={svgProps}
                        isCompletedColor={isCompletedColor}
                      />
                    </Box>
                  </Button>
                </Step>
              );
            })}
        </Stepper>
      </Box>
    );
  },
);
 
const KotoStepperStyle = {
  justifyContent: "space-between",
  alignItems: "flex-start",
  ".MuiStepLabel-iconContainer, .MuiStepConnector-root": {
    display: "none",
  },
};
 
KotoStepper.displayName = "KotoStepper";
 
export default KotoStepper;