All files / src/shared/GiveInputs GivePasswordInput.tsx

71.42% Statements 5/7
100% Branches 5/5
50% Functions 2/4
83.33% Lines 5/6

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                                          27x 27x                           2x             27x 27x                            
import { useState } from "react";
import { InputProps } from "./GiveInput";
import { InputAdornment } from "@mui/material";
import { EyeIcon, EyeSlashIcon } from "@phosphor-icons/react";
import { useAppTheme } from "@theme/v2/Provider";
import {
  HFGiveInput,
  type HFGiveInputProps,
} from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { sanitizePaste } from "@utils/index";
 
type Props = {
  name: string;
  helper?: HFGiveInputProps["helper"];
  defaultVisible?: boolean;
} & InputProps;
 
export default function GivePasswordInput({
  defaultVisible = false,
  ...props
}: Props) {
  const [isVisible, setIsVisible] = useState(defaultVisible);
  return (
    <HFGiveInput
      type={isVisible ? "text" : "password"}
      handleNormalizeInput={sanitizePaste}
      InputProps={{
        endAdornment: (
          <EndAdornment isVisible={isVisible} setIsVisible={setIsVisible} />
        ),
      }}
      {...props}
    />
  );
}
 
const EndAdornment = ({
  isVisible,
  setIsVisible,
}: {
  isVisible: boolean;
  setIsVisible: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
  const { palette } = useAppTheme();
  return (
    <InputAdornment
      position="end"
      sx={{ mr: "2px !important", cursor: "pointer" }}
      onClick={() => setIsVisible((prev) => !prev)}
    >
      {isVisible ? (
        <EyeIcon size={20} color={palette.icon?.["icon-primary"]} />
      ) : (
        <EyeSlashIcon size={20} color={palette.icon?.["icon-primary"]} />
      )}
    </InputAdornment>
  );
};