All files / src/sections/PayBuilder/components Stepper.tsx

100% Statements 29/29
85.18% Branches 23/27
100% Functions 13/13
100% Lines 28/28

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                13x                     269x     4x                                   13x       531x 531x 531x   531x   531x   4x               531x       2655x   2655x                                                                 1062x             13x   820x   269x                                       13x 1358x     269x                             13x 551x   269x     269x 61x   208x 15x   193x                                        
import { Box, Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { styled } from "@theme/v2/Provider";
import { usePayBuilderContext } from "../provider/PayBuilderContext";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { usePayBuilderForm } from "../provider/PayBuilderFormProvider";
import { memo, useCallback } from "react";
 
const Item = memo(
  ({
    label,
    isActive,
    isMobileView,
    isLaunchVisible,
    isCompleted,
    width,
    onClick,
    index,
  }: any) => {
    return (
      <ItemBox
        onClick={() => {
          onClick(index);
        }}
        isActive={isActive}
        isMobileView={isMobileView}
        sx={{
          maxWidth: width,
          minWidth: width,
        }}
      >
        <CustomDivider isCompleted={isCompleted} isActive={isActive} />
        <CustomText isActive={isActive} isLaunchVisible={isLaunchVisible}>
          {label}
        </CustomText>
      </ItemBox>
    );
  },
);
 
Item.displayName = "ItemStepper";
 
function Stepper({ isLaunchVisible }: { isLaunchVisible?: boolean }) {
  const { activeStepIndex, goToStep, lastStepCompletedIndex, stepsArray } =
    usePayBuilderContext();
  const { isMobileView } = useCustomThemeV2();
  const { methods } = usePayBuilderForm();
 
  const isEditAndHeadingCompleted = methods.formState.isValid;
 
  const onClick = useCallback(
    (index: number) => {
      goToStep({
        index,
        free: isEditAndHeadingCompleted,
      });
    },
    [isEditAndHeadingCompleted],
  );
 
  return (
    <Container flexDirection="row">
      <Container width="98%">
        {stepsArray.map(({ label, id }, index) => {
          const isActive = index === activeStepIndex;
 
          return (
            <Item
              key={id}
              label={label}
              isActive={isActive}
              isMobileView={isMobileView}
              isLaunchVisible={isLaunchVisible}
              isCompleted={index <= lastStepCompletedIndex}
              width={`${100 / stepsArray.length}%`}
              onClick={onClick}
              index={index}
            />
          );
        })}
      </Container>
 
      <GiveText
        fontSize="12px"
        display={{
          xs: "block",
          sm: "none",
        }}
        color="primary"
        fontWeight={400}
      >
        {activeStepIndex + 1}/{stepsArray?.length}
      </GiveText>
    </Container>
  );
}
 
export default memo(Stepper);
 
const Container = styled(Stack)<{ width?: string }>(({ theme, width }) => ({
  width: width || "100%",
  flexDirection: "row",
  alignItems: "center",
  overflowX: "auto",
}));
 
const CustomText = styled(Box, {
  shouldForwardProp: (prop) =>
    prop !== "isActive" && prop !== "isLaunchVisible",
})<{ isActive?: boolean; isLaunchVisible?: boolean }>(
  ({ theme, isActive = false, isLaunchVisible }) => ({
    display: "block",
    fontSize: "12px",
    color: isActive ? theme.palette.text.primary : theme.palette.text.secondary,
    visibility: "visible",
    width: "100%",
    whiteSpace: "nowrap",
    overflow: "hidden",
    textOverflow: "ellipsis",
    [theme.breakpoints.down("sm")]: {
      display: "none",
      textOverflow: "ellipsis",
      whiteSpace: "nowrap",
    },
    ...(!isLaunchVisible && {
      fontSize: "12px",
    }),
  }),
);
 
const ItemBox = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isActive" && prop !== "isMobileView",
})<{ isActive?: boolean; isMobileView: boolean }>(
  ({ theme, isActive, isMobileView }) => {
    return {
      borderRadius: "8px",
      padding: "10px",
      backgroundColor: isActive
        ? isMobileView
          ? "transparent"
          : theme.palette.primitive?.transparent["darken-5"]
        : "transparent",
      flexGrow: 1,
      flexBasis: 0,
      cursor: "pointer",
    };
  },
);
 
const CustomDivider = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isActive" && prop !== "isCompleted",
})<{ isActive?: boolean; isCompleted?: boolean }>(
  ({ theme, isActive = false, isCompleted = false }) => ({
    marginBottom: "9px",
    backgroundColor: (function () {
      if (isActive) {
        return theme.palette.icon?.["icon-primary"];
      }
      if (isCompleted) {
        return theme.palette.icon?.["icon-secondary"];
      }
      return theme.palette.primitive?.transparent["darken-25"];
    })(),
    borderRadius: "2px",
    height: "2px",
    position: "relative",
    width: "100%",
    "&::after": {
      content: '""',
      position: "absolute",
      top: 0,
      left: 0,
      backgroundColor: isActive
        ? theme.palette.icon?.["icon-primary"]
        : theme.palette.primitive?.transparent["darken-2"],
      height: "100%",
      width: isCompleted ? "100%" : 0,
      borderRadius: "2px ",
    },
  }),
);