All files / src/components/common/AddressInputs ZipCodeInput.tsx

100% Statements 5/5
50% Branches 3/6
100% Functions 2/2
100% Lines 5/5

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                                      66x                 30x   30x 4x     30x                                                  
import React from "react";
import { RHFInput } from "@common/Input";
import { useFormContext } from "react-hook-form";
import { MAX_LENGTH_ZIP_CODE_DIGITS } from "@constants/constants";
import { normalizeZip } from "@utils/helpers";
 
type ZipCodeInputProps = {
  id?: string;
  name: string;
  placeholder?: string;
  label?: string;
  letterSpacing?: string;
  disabled?: boolean;
  focusViewColor?: string;
  helperText?: string;
  isUSResident?: boolean;
  sx?: any;
};
 
const ZipCodeInput = ({
  name,
  placeholder,
  label,
  letterSpacing = "6px",
  sx,
  isUSResident = true,
  ...props
}: ZipCodeInputProps) => {
  const { setValue, watch } = useFormContext();
 
  React.useEffect(() => {
    setValue(name, normalizeZip(watch(name), isUSResident));
  }, [watch(name)]);
 
  return (
    <RHFInput
      name={name}
      label={label}
      fullWidth
      placeholder={placeholder}
      {...props}
      sx={{
        letterSpacing,
        "& .MuiFormHelperText-root": {
          letterSpacing: "0px",
        },
        "& input::placeholder": {
          letterSpacing: letterSpacing || "0px",
        },
        ...sx,
      }}
      inputProps={{
        maxLength: MAX_LENGTH_ZIP_CODE_DIGITS[isUSResident ? "US" : "NON_US"],
      }}
    />
  );
};
 
export default ZipCodeInput;