All files / src/shared/Inputs GiveEditableTag.tsx

72.72% Statements 24/33
47.61% Branches 20/42
54.54% Functions 6/11
77.41% Lines 24/31

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                                              52x                   649x 649x 649x 649x 649x   649x 649x   649x 167x 167x 167x 167x 167x       649x 649x 649x 649x       649x                                                                                       1x                                     52x 649x                       52x 649x                                              
import { PencilSimpleIcon } from "@phosphor-icons/react";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { useEffect, useRef, useState } from "react";
import { typography } from "@theme/v2/typography";
import "./GiveEditableTagCSS.css";
import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { isFunction } from "lodash";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { EDIT_DENY_MESSAGE } from "@constants/permissions";
import { truncateString } from "@utils/helpers";
 
export type GiveTagInputProps = {
  value: string;
  setValue: (v: string) => void;
  onSaveTag?: (v: string) => void;
  isMobile?: boolean;
  type?: "editable" | "form-field";
  title?: string;
  isUpdateAllowed?: boolean;
  isTruncated?: boolean;
};
 
export const GiveTagInput = ({
  value,
  setValue,
  onSaveTag,
  isMobile,
  type = "editable",
  title,
  isUpdateAllowed,
  isTruncated = true,
}: GiveTagInputProps) => {
  const { palette } = useAppTheme();
  const [isFocused, setIsFocused] = useState(false);
  const [isHovered, setIsHovered] = useState(false);
  const labelRef = useRef<HTMLLabelElement>(null);
  const inputRef = useRef<HTMLInputElement>(null);
 
  const isActive = isFocused || isHovered;
  const formattedValue = isTruncated ? truncateString(value, 20) : value;
 
  useEffect(() => {
    const parentElement = labelRef.current as HTMLElement;
    const inputElement = inputRef.current as HTMLElement;
    Eif (parentElement && inputElement) {
      Iif (isActive) parentElement.dataset.value = value;
      else parentElement.dataset.value = formattedValue;
    }
  }, [value, isActive]);
 
  const isEditable = type === "editable";
  const getMaxWidth = () => {
    Eif (isMobile) {
      return isTruncated ? "138px" : "fit-content";
    }
    return isActive ? "50%" : "212px";
  };
  return (
    <Label
      className="input-sizer"
      ref={labelRef}
      isFocused={isFocused}
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
      onClick={(e) => e.stopPropagation()}
      isEditable={isEditable}
      sx={{
        maxWidth: getMaxWidth(),
      }}
    >
      {isEditable && ((!isMobile && isHovered) || isMobile) && !isFocused && (
        <PencilSimpleIcon
          width={12}
          height={12}
          fill={palette.icon?.["icon-secondary"]}
          style={{
            marginRight: "8px",
            height: "16px",
            width: "16px",
          }}
        />
      )}
      {isEditable ? (
        <GiveTooltip
          width="compact"
          alignment="left"
          title={EDIT_DENY_MESSAGE}
          color="default"
          disableHoverListener={isUpdateAllowed}
        >
          <Input
            ref={inputRef}
            disabled={!isUpdateAllowed}
            type="text"
            size={1}
            onFocus={() => setIsFocused(true)}
            onBlur={() => {
              if (isFunction(onSaveTag)) onSaveTag(value);
              setIsFocused(false);
            }}
            value={isActive ? value : formattedValue}
            onChange={(e) => setValue(e.target.value)}
            isFocused={isFocused}
            maxLength={50}
          />
        </GiveTooltip>
      ) : (
        <Stack padding="0 !important" width="100%" direction="row" gap="8px">
          {title && (
            <GiveText variant="bodyXS" color="secondary">
              {isTruncated ? truncateString(title, 20) : title}
            </GiveText>
          )}
          {value && <GiveText variant="bodyXS">{formattedValue}</GiveText>}
        </Stack>
      )}
    </Label>
  );
};
 
const Input = styled("input")<{ isFocused: boolean }>(
  ({ theme, isFocused }) => ({
    color: isFocused
      ? theme.palette.text?.primary
      : theme.palette.text?.secondary,
    width: "105% !important",
    display: "block",
    overflow: "hidden",
    whiteSpace: "nowrap",
    textOverflow: "ellipsis",
  }),
);
 
const Label = styled("label")<{ isFocused: boolean; isEditable: boolean }>(
  ({ isFocused, isEditable, theme }) => ({
    backgroundColor: isFocused
      ? theme.palette.surface?.tertiary
      : theme.palette.surface?.secondary,
 
    ...typography.bodyXS,
    transition: "background-color 0.2s",
 
    ...(isEditable
      ? {
          height: "28px",
          borderRadius: "4px",
          padding: "6px 8px",
          "&:hover": {
            backgroundColor: theme.palette.surface?.tertiary,
          },
        }
      : {
          borderRadius: "12px",
          padding: "4px 8px",
        }),
  }),
);