All files / src/features/Permissions/ModalV2/components FilterPermissionList.tsx

100% Statements 22/22
100% Branches 10/10
100% Functions 9/9
100% Lines 20/20

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                                              132x     132x       132x 1x     132x 1x   132x 1x 1x 1x   132x 23x         132x   132x 432x 432x 1x                   132x                                                                                 12x                             132x              
import React, { useEffect, useState } from "react";
import Popover from "@mui/material/Popover";
import { Box, Stack, styled } from "@mui/material";
import { Text } from "@common/Text";
import { ArrowDownChevron } from "@assets/icons";
import { palette } from "@palette";
import CheckIcon from "@assets/icons/RebrandedIcons/CheckIcon";
import { isFunction } from "lodash";
 
export type ItemType = {
  value: string;
  title: string | undefined;
};
 
function FilterPermissionList({
  filterData,
  defaultValue,
  onHandleSelect,
}: {
  defaultValue?: ItemType;
  filterData?: ItemType[];
  onHandleSelect?: (e: ItemType) => void;
}) {
  const [permission, setPermission] = useState<ItemType | undefined>(
    defaultValue,
  );
  const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(
    null,
  );
 
  const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    setAnchorEl(event.currentTarget);
  };
 
  const handleClose = () => {
    setAnchorEl(null);
  };
  const handleSelect = (e: { value: string; title: string | undefined }) => {
    setPermission(e);
    isFunction(onHandleSelect) && onHandleSelect(e);
    handleClose();
  };
  useEffect(() => {
    defaultValue
      ? setPermission(defaultValue)
      : filterData && setPermission(filterData[0]);
  }, []);
 
  const open = Boolean(anchorEl);
 
  const renderMenuItem = (state: ItemType) => {
    const isSelected = permission?.value === state.value;
    return (
      <ItemWrapper onClick={() => handleSelect(state)} key={state.value}>
        <Text fontSize="14px" fontWeight="book" color="#575353">
          {state.title}
        </Text>
        {isSelected && (
          <CheckIcon width={20} height={20} fill={palette.black[300]} />
        )}
      </ItemWrapper>
    );
  };
  return (
    <Box>
      <CustomButton onClick={handleClick}>
        <Text fontWeight="regular" fontSize="16px" color="#403D3D">
          {permission?.title}
        </Text>
        <Box
          sx={{
            transform: `rotate(${open ? "180deg" : "0deg"})`,
            transition: " transform 0.3s ease-in-out",
          }}
        >
          <ArrowDownChevron color="#8F8F8F" height={7} width={11.5} />{" "}
        </Box>
      </CustomButton>
 
      <Popover
        id="permission-permissions"
        open={open}
        anchorEl={anchorEl}
        onClose={handleClose}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "left",
        }}
        PaperProps={{
          style: {
            borderRadius: "8px",
            boxShadow: "0px 8px 25px 0px #00000026",
            padding: "4px",
          },
        }}
      >
        <Box width="244px">{filterData?.map(renderMenuItem)}</Box>
      </Popover>
    </Box>
  );
}
 
export default FilterPermissionList;
 
const ItemWrapper = styled(Stack)(() => ({
  height: "40px",
  flexDirection: "row",
  justifyContent: "space-between",
  alignItems: "center",
  padding: "4px 8px",
  width: "100%",
  cursor: "pointer",
  borderRadius: "4px",
  userSelect: "none",
  "&:hover": {
    backgroundColor: "#ECECE9",
  },
}));
 
const CustomButton = styled("button")(() => ({
  all: "unset",
  cursor: "pointer",
  display: "flex",
  alignItems: "center",
  gap: "12px",
}));