All files / src/components/common/ColorPicker ColorPicker.tsx

66.66% Statements 8/12
72.72% Branches 8/11
33.33% Functions 2/6
66.66% Lines 8/12

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                                        4x               14x 14x 14x       14x       14x 14x     14x                                                                                                                                                                                                            
import Box from "@mui/material/Box";
import { palette } from "@palette";
 
// import { useFormContext, Controller } from "react-hook-form";
import { Input } from "@common/Input";
import { Text } from "@common/Text";
import GradientPickerPopoverNew from "@components/EnterpriseSettings/Branding/components/GradientPickerPopoverNew";
import { Stack } from "@mui/material";
import { RGBAToHexA } from "@sections/Actions/Share/functions";
import { memo, useCallback, useEffect, useState } from "react";
 
export type ColorPickerProps = {
  fullWidth?: boolean;
  width?: string | number;
  size?: "small" | "medium";
  label?: string | JSX.Element;
  onChange: (color: string) => void;
  defaultValue: string;
};
 
const ColorPicker: React.FC<ColorPickerProps> = ({
  label,
  width,
  fullWidth,
  size = "medium",
  defaultValue,
  onChange,
}) => {
  const [anchorEl, setAnchorEl] = useState<HTMLDivElement | null>(null);
  const [value, setValue] = useState<string>(defaultValue);
  const handleClose = useCallback(() => {
    setAnchorEl(null);
  }, []);
 
  const handleSetColor = useCallback((col: string) => {
    setValue(RGBAToHexA(col).toUpperCase());
  }, []);
 
  useEffect(() => {
    onChange(value);
  }, [value]);
 
  return (
    <Box
      sx={{
        "& > div": {
          width: "100%",
        },
 
        "button.MuiButton-root": {
          boxShadow: "none",
          borderRadius: "50%",
 
          "& > div": {
            borderRadius: "50%",
            border: `1px solid ${palette.neutral[700]}`,
          },
        },
 
        ...(fullWidth && {
          "& .MuiFormControl-root.MuiTextField-root": {
            width: "100%",
          },
        }),
 
        ...(width && {
          "& .MuiFormControl-root.MuiTextField-root": {
            width: width,
          },
        }),
 
        "& .MuiInputBase-input": {
          marginTop: "0 !important",
        },
        "& .MuiInputBase-root": {
          padding: "15px 4px 15px 12px",
        },
 
        ...(size === "small" && {
          "& .MuiInputBase-root": {
            padding: "0 4px 0 12px",
 
            "& input": {
              padding: "4px 0",
            },
          },
        }),
      }}
    >
      {label && (
        <Text
          fontWeight="medium"
          sx={{ pb: size === "small" ? 0.5 : 1 }}
          color={palette.neutral[800]}
        >
          {label}
        </Text>
      )}
      <Stack
        direction="row"
        justifyContent="center"
        alignItems="center"
        gap={1}
      >
        <Box
          onClick={(e) => setAnchorEl(e.currentTarget)}
          sx={{
            width: 24,
            minWidth: 24,
            height: 24,
            border: "solid .5px black",
            borderRadius: 24,
            cursor: "pointer",
 
            backgroundColor: value,
          }}
        ></Box>
        <Input
          fullWidth
          sx={{
            width: "100%",
          }}
          inputProps={{ maxLength: 9 }}
          type="text"
          value={value}
          name="text1"
          onChange={(e) => {
            setValue(e.target.value.toUpperCase());
          }}
        />
      </Stack>
      <GradientPickerPopoverNew
        anchorEl={anchorEl}
        onChange={handleSetColor}
        handleClose={handleClose}
        value={value}
        hideOpacity={false}
        hideGradientButton
      />
    </Box>
  );
};
 
export default memo(ColorPicker);