All files / src/sections/Checkout/Payment/inputs ZipCodeInput.tsx

80% Statements 8/10
50% Branches 3/6
100% Functions 3/3
77.77% Lines 7/9

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                1x 2x   2x 1x           2x 1x     2x                                          
import React from "react";
import { RHFInput } from "@common/Input";
import { useFormContext } from "react-hook-form";
 
type ZipCodeInputProps = {
  allowAnything?: boolean;
};
 
const ZipCodeInput = ({ allowAnything }: ZipCodeInputProps) => {
  const { setValue, watch } = useFormContext();
 
  const normalizeInput = (value: string) => {
    Eif (!value || allowAnything) return value;
    const currentValue = value.replace(/[^\d\s-]/g, "");
 
    return currentValue;
  };
 
  React.useEffect(() => {
    setValue("zipCode", normalizeInput(watch("zipCode")));
  }, [watch("zipCode")]);
 
  return (
    <RHFInput
      name="zipCode"
      label="Zip Code"
      fullWidth
      placeholder="•••••"
      sx={{
        letterSpacing: "6px",
        "& .MuiFormHelperText-root": {
          letterSpacing: "0px",
        },
      }}
      inputProps={{
        // I am not sure what is the longest possible zip code but 50 should suffice
        maxLength: allowAnything ? "50" : "10",
      }}
    />
  );
};
 
export default ZipCodeInput;