All files / react-app/src/components/RHF dependencyWrapper.tsx

94.54% Statements 52/55
92.42% Branches 61/66
90.9% Functions 10/11
96% Lines 48/50

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133        107x 67x     67x     107x 67x   53x 1x   52x     3x 1x         2x     3x 1x         2x     4x         4x             107x   280x             107x             51x 51x 51x 51x 51x 51x           6x 6x 4x 2x   2x 10x   2x   2x   2x 5x   2x     2x   6x 45x           51x             2x 2x   2x         51x   5x 2x   3x     33x          
import { PropsWithChildren, useEffect, useState } from "react";
import { useFormContext } from "react-hook-form";
import { Condition, DependencyRule, DependencyWrapperProps } from "shared-types";
 
export const checkTriggeringValue = (dependentValue: unknown[], dependency?: DependencyRule) => {
  Iif (dependency?.looseConditions) {
    return !!dependency?.conditions?.some((d, i) => triggerCheckSwitch(dependentValue, d, i));
  }
  return !!dependency?.conditions?.every((d, i) => triggerCheckSwitch(dependentValue, d, i));
};
 
const triggerCheckSwitch = (dependentValue: unknown[], d: Condition, i: number) => {
  switch (d.type) {
    case "expectedValue":
      if (Array.isArray(dependentValue[i])) {
        return (dependentValue[i] as unknown[]).includes(d.expectedValue);
      }
      return dependentValue[i] === d?.expectedValue;
 
    case "notBadValue":
      if (Array.isArray(dependentValue[i])) {
        return (
          (dependentValue[i] as unknown[]).length > 0 &&
          !(dependentValue[i] as unknown[]).includes(d.expectedValue)
        );
      }
      return !!dependentValue[i] && !(dependentValue[i] === d?.expectedValue);
 
    case "notOnlyBadValue":
      if (Array.isArray(dependentValue[i])) {
        return !(
          (dependentValue[i] as unknown[]).length === 1 &&
          (dependentValue[i] as unknown[]).includes(d.expectedValue)
        );
      }
      return !!dependentValue[i] && !(dependentValue[i] === d?.expectedValue);
 
    case "valueExists":
      return (
        (Array.isArray(dependentValue[i]) && (dependentValue[i] as unknown[]).length > 0) ||
        (!Array.isArray(dependentValue[i]) && !!dependentValue[i])
      );
    case "valueNotExist":
      return (
        (Array.isArray(dependentValue[i]) && (dependentValue[i] as unknown[]).length === 0) ||
        !dependentValue[i]
      );
  }
};
 
export const DependencyWrapper = (props: PropsWithChildren<DependencyWrapperProps>) => {
  // Check for dependencies which won't exist outside of forms
  if (!props.dependency || !props.dependency.conditions || !props.dependency.effect) {
    return <>{props.children}</>;
  }
 
  return <DependencyWrapperHandler {...props} />;
};
 
const DependencyWrapperHandler = ({
  name,
  dependency,
  children,
  parentValue,
  changeMethod,
}: PropsWithChildren<DependencyWrapperProps>) => {
  const { watch, setValue, getValues } = useFormContext();
  const [wasSetLast, setWasSetLast] = useState(false);
  const dependentValues = watch(dependency?.conditions?.map((c) => c.name) ?? []);
  const isTriggered = dependency && checkTriggeringValue(dependentValues, dependency);
  useEffect(() => {
    if (
      !wasSetLast &&
      dependency?.effect.type === "setValue" &&
      isTriggered &&
      !!dependency?.effect.fieldName
    ) {
      const value = getValues(dependency.effect.fieldName);
      if (Array.isArray(value)) {
        if (Array.isArray(dependency.effect.newValue)) {
          let newValArr = [...value, ...dependency.effect.newValue];
 
          Eif (dependency.effect.checkUnique)
            newValArr = newValArr.filter((v, i, a) => a.indexOf(v) === i);
 
          setValue(dependency.effect.fieldName, newValArr);
        } else {
          let newValArr = [...value, dependency.effect.newValue];
 
          Eif (dependency.effect.checkUnique)
            newValArr = newValArr.filter((v, i, a) => a.indexOf(v) === i);
 
          setValue(dependency.effect.fieldName, newValArr);
        }
      } else {
        setValue(dependency.effect.fieldName, dependency.effect.newValue);
      }
      setWasSetLast(true);
    } else Iif (!isTriggered && wasSetLast) {
      setWasSetLast(false);
    }
 
    // This logic is to give the ability for checkboxes (and eventually radio groups) the ability to have show/hide logic based on UI form logic
    // We are grabbing the parent value (checkbox group array) and remove the value of the child being hidden and filtering it out
    if (
      isTriggered &&
      dependency?.effect.type === "hide" &&
      name &&
      parentValue?.includes(name) &&
      changeMethod
    ) {
      const filteredArray = parentValue.filter((value) => {
        return value !== name;
      });
      changeMethod(filteredArray);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dependentValues, parentValue, changeMethod, dependency]);
 
  switch (dependency?.effect.type) {
    case "hide":
      if (isTriggered) {
        return null;
      }
      break;
    case "show":
      if (isTriggered) return <>{children}</>;
      return null;
  }
 
  return <>{children}</>;
};