All files / src/shared/HFInputs/HFRichInput/components Wrapper.tsx

69.23% Statements 18/26
48% Branches 12/25
63.63% Functions 7/11
69.23% Lines 18/26

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                                                      102x                       2639x   2639x 3x     2639x             2639x                 3x 3x     8x 8x                                                                     3x                       102x                       13x 13x   13x 13x                                                                                                                                             102x 1043x                                      
import { Button } from "@mui/material";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { cloneElement, useState } from "react";
import { MenuItems } from "../components/MenuItems";
import { styled, useAppTheme } from "@theme/v2/Provider";
import GiveText from "@shared/Text/GiveText";
import { CaretDownIcon, CaretUpIcon } from "@phosphor-icons/react";
import { optionType } from "../Headings/types";
import { fontSizeSingleType, fontSizeTypes } from "../FontSize/const";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
 
type Props = {
  options?: optionType[] | fontSizeTypes;
  value: any;
  menuWidth: number;
  activeIndex: number;
  handleClick?: (vals: any) => void;
  anchor?: any;
  customContent?: ({ onClose }: any) => JSX.Element;
  searchBarProps?: {
    handleChange?: (val: string) => void;
    value: string;
  };
  isColorOptions?: boolean;
  overrideOnClick?: boolean;
};
 
export const WrapperActionComponent = ({
  value,
  options,
  menuWidth,
  handleClick,
  activeIndex,
  anchor,
  customContent: CustomContent,
  searchBarProps,
  isColorOptions = false,
  overrideOnClick,
}: Props) => {
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const closeMenu = () => {
    setAnchorEl(null);
  };
 
  const handleMenuItemClick = (option: optionType | fontSizeSingleType) => {
    if (typeof handleClick === "function") {
      handleClick(option);
    }
    closeMenu();
  };
 
  return (
    <div style={{ position: "relative" }}>
      {anchor ? (
        cloneElement(
          anchor,
          overrideOnClick === false
            ? {}
            : {
                onMouseDown: (e: any) => {
                  e.preventDefault();
                  setAnchorEl(e?.currentTarget as HTMLElement);
                },
                onClick: (e: any) => {
                  e?.preventDefault();
                  setAnchorEl(e?.currentTarget as HTMLElement);
                },
              },
        )
      ) : (
        <AnchorButton
          disableRipple
          disableFocusRipple
          disableTouchRipple
          variant="text"
          onMouseDown={(e) => {
            e.preventDefault();
            setAnchorEl(e?.currentTarget as HTMLElement);
          }}
        >
          <GiveText marginRight="6px" fontSize="14px" fontWeight="400">
            {value}
          </GiveText>
          {!anchorEl ? <CaretDownIcon /> : <CaretUpIcon />}
        </AnchorButton>
      )}
      {anchorEl && (
        <Menu
          anchorEl={anchorEl}
          closeMenu={closeMenu}
          handleMenuItemClick={handleMenuItemClick}
          activeIndex={activeIndex}
          options={options}
          menuWidth={menuWidth}
          flexDirection={anchor ? "row" : "column"}
          isColorOptions={isColorOptions}
          customContent={
            CustomContent ? (
              <CustomContent
                onClose={() => {
                  closeMenu();
                }}
              />
            ) : null
          }
          searchBarProps={searchBarProps}
        />
      )}
    </div>
  );
};
//TODO fix types
const Menu = ({
  anchorEl,
  closeMenu,
  handleMenuItemClick,
  activeIndex,
  options,
  menuWidth,
  flexDirection,
  customContent,
  isColorOptions,
  searchBarProps,
}: any) => {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
 
  Eif (!options) {
    return (
      <ContextualMenu
        autoFocus={false}
        disableAutoFocus
        disableAutoFocusItem
        disableEnforceFocus
        customContent={customContent}
        anchorEl={anchorEl as any}
        handleClose={closeMenu}
        options={[]}
        color="primary"
        texture={isMobileView ? "solid" : "blurred"}
        menuWidth={menuWidth}
        customPaperStyle={{
          background: palette.surface?.["tertiary-transparent"],
          width: `${menuWidth}px`,
        }}
      />
    );
  }
 
  return (
    <ContextualMenu
      autoFocus={false}
      disableAutoFocus
      disableAutoFocusItem
      disableEnforceFocus
      searchBarProps={searchBarProps}
      emptySection="empty-search"
      onCloseEmptyState={() => searchBarProps?.handleChange?.("")}
      customContent={
        customContent ? (
          customContent
        ) : (
          <MenuItems
            options={options}
            handleMenuItemClick={handleMenuItemClick}
            activeIndex={activeIndex}
            flexDirection={flexDirection}
            isColorOptions={isColorOptions}
          />
        )
      }
      anchorEl={anchorEl as any}
      handleClose={closeMenu}
      options={options?.map((option: optionType | fontSizeSingleType) => {
        return {
          ...option,
          text: option.label,
          iconPlacement: "right",
        };
      })}
      color="primary"
      texture={isMobileView ? "solid" : "blurred"}
      menuWidth={menuWidth}
      customPaperStyle={{
        background: palette.surface?.["tertiary-transparent"],
        width: `${menuWidth}px`,
 
        "& ul div":
          options?.[0]?.id === "color"
            ? {
                display: "flex",
                flexWrap: "wrap",
              }
            : {},
      }}
    />
  );
};
 
const AnchorButton = styled(Button)(({ theme }) => {
  return {
    color: theme.palette.text.primary,
    backgroundColor: "transparent",
    padding: "4px",
    borderRadius: "4px",
    cursor: "pointer",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    margin: "0 2px",
    "&:hover": {
      backgroundColor: theme.palette.primitive?.transparent["darken-5"],
    },
    "&:focus": {
      backgroundColor: "transparent",
      outline: "none",
    },
  };
});