All files / src/components/common/RHFStreetAddress RHFStreetAddress.tsx

93.33% Statements 14/15
83.33% Branches 5/6
100% Functions 5/5
100% Lines 12/12

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        20x         38x 38x   38x 44x 43x 41x 41x     38x         44x       2x 1x                  
import * as React from "react";
import { useFormContext, Controller } from "react-hook-form";
import { Input } from "@common/Input";
import { RHFInputProps } from "@common/Input/Input";
export const RHFStreetAddress: React.FC<RHFInputProps> = ({
  name,
  helperText,
  ...props
}) => {
  const { control } = useFormContext();
  const [isFocused, setIsFocused] = React.useState(false);
 
  const getHelperText = (error: any) => {
    if (error) return error.message;
    if (isFocused) return "PO Boxes or PMBs are not allowed";
    Iif (helperText) return helperText;
    return undefined;
  };
 
  return (
    <Controller
      name={name}
      control={control}
      render={({ field: { ref, value, ...rest }, fieldState: { error } }) => (
        <Input
          inputRef={ref}
          {...rest}
          value={value}
          onFocus={() => setIsFocused(true)}
          onBlur={() => setIsFocused(false)}
          {...props}
          error={!!error}
          helperText={getHelperText(error)}
        />
      )}
    />
  );
};