All files / src/components/common/Button LoadingButton.tsx

100% Statements 8/8
60% Branches 6/10
100% Functions 4/4
100% Lines 8/8

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                  12x           169x                                                         12x 350x               169x             12x           12x 3x                                  
import { Box, CircularProgress, Collapse, styled } from "@mui/material";
import { palette } from "@palette";
import { RebrandedButtonProps } from "@common/Button/Button_V2";
import { Button } from "@common/Button";
 
export type LoadingButtonProps = RebrandedButtonProps & {
  isLoading?: boolean;
  isShowTextWhileLoading?: boolean;
};
const LoadingButton = ({
  isLoading,
  isShowTextWhileLoading = false,
  children,
  ...props
}: LoadingButtonProps) => {
  return (
    <Button {...props} data-testid="popup-submit-button">
      <StyledContainer isShowTextWhileLoading={isShowTextWhileLoading}>
        <Collapse
          component={StyledBox}
          orientation="horizontal"
          in={isLoading}
          unmountOnExit
          sx={{
            "& .MuiCollapse-wrapper": {
              height: "auto",
            },
          }}
        >
          <LoadingIcon size={isShowTextWhileLoading ? 16 : 24} />
        </Collapse>
        <Collapse
          component={StyledBox}
          orientation="horizontal"
          in={isShowTextWhileLoading || !isLoading}
          unmountOnExit
        >
          <Box sx={{ whiteSpace: "nowrap" }}>{children}</Box>
        </Collapse>
      </StyledContainer>
    </Button>
  );
};
 
const StyledContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isShowTextWhileLoading",
})(
  ({
    theme,
    isShowTextWhileLoading,
  }: {
    theme: any;
    isShowTextWhileLoading: boolean;
  }) => ({
    display: "flex",
    flexDirection: isShowTextWhileLoading ? "row-reverse" : "row",
    gap: isShowTextWhileLoading ? 8 : 0,
  }),
);
 
const StyledBox = styled(Box)({
  gridArea: "1/1",
  display: "flex",
  alignItems: "center",
});
 
const LoadingIcon = ({ size = 24 }: { size?: number }) => {
  return (
    <Box
      sx={{
        maxHeight: `${size}px`,
      }}
    >
      <CircularProgress
        size={size}
        sx={{
          color: palette.neutral.black,
        }}
      />
    </Box>
  );
};
 
export default LoadingButton;