All files / src/shared/HFInputs/HFGiveInput HFGiveInput.tsx

90% Statements 9/10
80% Branches 8/10
100% Functions 4/4
90% Lines 9/10

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                                      156x                     6256x   6256x           7181x                           582x 582x   582x                 582x     54x                        
import * as React from "react";
import { useFormContext, Controller } from "react-hook-form";
import { capitalizeEachWord } from "@utils/index";
import { GiveInput, InputProps } from "@shared/GiveInputs/GiveInput";
 
type NormalizeInputFunction = (VAL: string) => string;
export type HFGiveInputProps = InputProps & {
  name: string;
  helper?: string | JSX.Element | any;
  shouldCapitalizeEachWord?: boolean;
  focusViewColor?: string;
  handleNormalizeInput?: NormalizeInputFunction;
  maxLength?: number;
  displayCounter?: boolean;
  onChangeCustom?: any;
  onValidate?: () => void;
  hidePlaceholder?: boolean;
};
 
export const HFGiveInput: React.FC<HFGiveInputProps> = ({
  name,
  helper,
  shouldCapitalizeEachWord,
  handleNormalizeInput,
  maxLength,
  displayCounter,
  onChangeCustom,
  onValidate,
  ...props
}) => {
  const { control } = useFormContext();
 
  return (
    <Controller
      name={name}
      key={name}
      control={control}
      render={({ field: { ref, value, ...rest }, fieldState: { error } }) => {
        return (
          <>
            <GiveInput
              disabled
              inputRef={ref}
              {...rest}
              displayCounter={displayCounter}
              error={!!error}
              maxLength={maxLength}
              value={
                shouldCapitalizeEachWord ? capitalizeEachWord(value) : value
              }
              helperText={helper || error?.message}
              onChange={(e) => {
                const value = e.target.value;
                const newValue = maxLength ? value?.slice(0, maxLength) : value;
 
                rest?.onChange({
                  ...e,
                  target: {
                    ...e.target,
                    value: handleNormalizeInput
                      ? handleNormalizeInput(newValue)
                      : newValue,
                  },
                });
                onValidate?.();
              }}
              onBlur={() => {
                Iif (onChangeCustom) {
                  onChangeCustom(value);
                }
              }}
              {...props}
            />
          </>
        );
      }}
    />
  );
};