All files / src/components/common/BusinessProfileInputs OwnershipTypeSelect.tsx

56.52% Statements 13/23
43.75% Branches 7/16
100% Functions 4/4
56.52% Lines 13/23

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                            19x               38x     38x 38x   38x 5x                 5x   5x 5x 5x                             38x         76x             41x                  
import React from "react";
import { RHFSelect } from "@common/Select";
import { useFormContext } from "react-hook-form";
import { ArrowDownChevron } from "@assets/icons";
import { palette } from "@palette";
type OwnershipTypeSelectProps = {
  name: string;
  businessTypeName?: string;
  label: string;
  disabled?: boolean;
  initialValue?: "private" | "public";
  hasArrowDownIcon?: boolean;
};
 
const OwnershipTypeSelect = ({
  name,
  label,
  disabled,
  businessTypeName = "businessDetails.businessType",
  initialValue,
  hasArrowDownIcon = false,
}: OwnershipTypeSelectProps) => {
  const [allowedOwnershipTypes, setAllowedOwnershipTypes] = React.useState<
    string[]
  >(["Private", "Public"]);
  const { watch, setValue } = useFormContext();
  const isMount = React.useRef(true);
 
  React.useEffect(() => {
    switch (watch(businessTypeName)) {
      case "individual_sole_proprietorship":
        setAllowedOwnershipTypes(["Private"]);
        break;
      case "tax_exempt_organization":
      case "government_agency":
        setAllowedOwnershipTypes(["Public"]);
        break;
      default:
        setAllowedOwnershipTypes(["Public", "Private"]);
    }
    Eif (isMount.current) {
      isMount.current = false;
      return;
    }
    switch (watch(businessTypeName)) {
      case "individual_sole_proprietorship":
        setValue(name, "private");
        break;
      case "tax_exempt_organization":
      case "government_agency":
        setValue(name, "public");
        break;
      default:
        setValue(name, "private");
    }
  }, [watch(businessTypeName)]);
 
  return (
    <RHFSelect
      name={name}
      label={label}
      disabled={disabled || allowedOwnershipTypes.length === 1}
      options={allowedOwnershipTypes.map((item) => ({
        value: item.toLowerCase(),
        label: item,
      }))}
      SelectProps={{
        ...(hasArrowDownIcon && {
          IconComponent: () => (
            <ArrowDownChevron width={16} height={9} color={palette.gray[300]} />
          ),
        }),
      }}
    />
  );
};
 
export default OwnershipTypeSelect;