All files / src/components/common/AddressInputs GiveProvinceInput.tsx

86.11% Statements 31/36
66.66% Branches 18/27
76.92% Functions 10/13
85.71% Lines 30/35

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                        66x 53808x         912x 53808x       66x             476x   476x 66x 66x 66x     476x 476x 476x 476x 476x   476x 11658x         11658x     476x       476x           466x 27384x   27384x                                 476x                       476x 10x                                                       466x 456x                               16x                         10x                        
import React, { useState } from "react";
import { getUSNames } from "@utils/country_dial_codes";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useAppTheme } from "@theme/v2/Provider";
import { useFormContext } from "react-hook-form";
import { CheckIcon, XIcon } from "@phosphor-icons/react";
type GiveProvinceInputProps = {
  id?: string;
  isUS: boolean | null;
  name?: string;
  disabled?: boolean;
  focusViewColor?: string;
  helperText?: string;
  multiChoiseCountry?: {
    [key: string]: string;
  };
};
 
const getFilteredUSStates = (searchValue: string) => {
  const states = getUSNames().map((state) => ({
    value: state,
    label: state,
  }));
 
  return states.filter((state) =>
    state.label.toLowerCase().includes(searchValue.trim().toLowerCase()),
  );
};
 
const GiveProvinceInput = ({
  isUS,
  name = "businessAddress.province",
  disabled,
  multiChoiseCountry,
  ...rest
}: GiveProvinceInputProps) => {
  const isMount = React.useRef(true);
 
  React.useEffect(() => {
    Eif (isMount.current) {
      isMount.current = false;
      return;
    }
  }, [isUS]);
  const theme = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const [searchValue, setSearchValue] = useState("");
  const { watch, setValue } = useFormContext();
  const province = watch(name);
 
  const filteredNonUSStates = Object.values(multiChoiseCountry ?? {})
    .map((value) => ({
      value,
      label: value,
    }))
    .filter((state) =>
      state.label.toLowerCase().includes(searchValue.trim().toLowerCase()),
    );
 
  const clearSearch = () => {
    setSearchValue("");
  };
 
  const getMenuOptions = (
    list: {
      value: string;
      label: string;
    }[],
  ) => {
    return list.map((option) => {
      const isSelected = option?.value === province;
 
      return {
        text: option.label,
        IconRight: isSelected ? XIcon : CheckIcon,
        showRightIconOnHover: !isSelected,
        onClick: () => {
          const newProvince = isSelected ? "" : option.value;
 
          setValue(name, newProvince, {
            shouldDirty: true,
            shouldValidate: true,
          });
        },
        isSelected: isSelected,
      };
    });
  };
 
  Iif (Array.isArray(multiChoiseCountry) && !multiChoiseCountry?.length) {
    return (
      <HFGiveInput
        name={name}
        label="Province"
        fullWidth
        disabled={disabled}
        {...rest}
      />
    );
  }
 
  if (multiChoiseCountry && !isUS) {
    return (
      <HFGiveSelect
        useContextualMenu
        name={name}
        label="Province"
        placeholder="Province"
        disabled={disabled}
        options={filteredNonUSStates}
        contextualMenuProps={{
          color: "secondary",
          texture: isMobileView ? "solid" : "blurred",
          options: getMenuOptions(filteredNonUSStates),
          searchBarProps: {
            value: searchValue,
            handleChange: (val: string) => setSearchValue(val),
          },
          showEmptySectionWhenNoData: true,
          emptySection: "empty-search",
          onCloseEmptyState: clearSearch,
          customPaperStyle: {
            background: `${theme.palette.surface?.["tertiary-transparent"]} !important`,
            backdropFilter: "blur(8px)",
          },
        }}
      />
    );
  }
 
  if (isUS)
    return (
      <HFGiveSelect
        name={name || "businessAddress.province"}
        label="State"
        placeholder="Select State"
        value={province}
        disabled={disabled}
        options={getFilteredUSStates(searchValue)}
        {...rest}
        useContextualMenu={true}
        contextualMenuProps={{
          color: "secondary",
          texture: isMobileView ? "solid" : "blurred",
          options: getMenuOptions(getFilteredUSStates(searchValue)),
          searchBarProps: {
            value: searchValue,
            handleChange: (val: string) => setSearchValue(val),
          },
          showEmptySectionWhenNoData: true,
          emptySection: "empty-search",
          onCloseEmptyState: clearSearch,
          customPaperStyle: {
            background: `${theme.palette.surface?.["tertiary-transparent"]} !important`,
            backdropFilter: "blur(8px)",
          },
        }}
      />
    );
 
  return (
    <HFGiveInput
      name={name || "businessAddress.province"}
      label="Province"
      fullWidth
      disabled={disabled}
      {...rest}
    />
  );
};
 
export default GiveProvinceInput;