diff --git a/src/Components/ProfileMenu/ProfileMenu.tsx b/src/Components/ProfileMenu/ProfileMenu.tsx index 88782b0e..ed14d760 100644 --- a/src/Components/ProfileMenu/ProfileMenu.tsx +++ b/src/Components/ProfileMenu/ProfileMenu.tsx @@ -4,6 +4,7 @@ import React, { useCallback, useMemo, useRef, + MutableRefObject } from "react"; import IconButton from "@material-ui/core/IconButton"; import Menu from "@material-ui/core/Menu"; @@ -19,30 +20,25 @@ import ResetPasswordModal from "./ResetPassword"; import { ProfileMenuProps } from "./types"; function getCustomMenuElements(menuItemConf: any, classes: any) { - return Object.entries(menuItemConf ?? {}).map(([key, menuItem]: [string, any]) => { - if (React.isValidElement(menuItem)) - return menuItem; - - return ( - { i18n.t(menuItem.title) } - ); - }); + return Object.entries(menuItemConf ?? {}).map( + ([key, menuItem]: [string, any]) => { + if (React.isValidElement(menuItem)) return menuItem; + + return ( + + {i18n.t(menuItem.title)} + + ); + } + ); } const ProfileMenu = (props: ProfileMenuProps) => { - // State hooks - const [anchorEl, setAnchorEl] = useState(null); - const [username, setUsername] = useState(""); - // Other hooks - const user = useMemo(() => new User(), []); - const classes = profileMenuStyles(); - // Refs - const resetModalRef = useRef<{ open: Function }>(); // Props const { welcomeLabel = "Hello", @@ -51,11 +47,24 @@ const ProfileMenu = (props: ProfileMenuProps) => { version = "", extraItems = [], isDarkTheme = true, + menuItemConf, + isMenuOpen, handleLogout = () => console.log("logout"), handleToggleTheme, - menuItemConf, + onClose } = props; + // State hooks + const [anchorEl, setAnchorEl] = useState(null); + const [username, setUsername] = useState(""); + // Other hooks + const triggerButtonRef: MutableRefObject = useRef(); + const menuOpenAnimation: MutableRefObject = useRef("auto"); + const user = useMemo(() => new User(), []); + const classes = profileMenuStyles(); + // Refs + const resetModalRef = useRef<{ open: Function }>(); + //======================================================================================== /* * * Handlers * @@ -75,7 +84,8 @@ const ProfileMenu = (props: ProfileMenuProps) => { */ const handleClose = useCallback(() => { setAnchorEl(null); - }, []); + onClose && onClose(); + }, [onClose]); /** * Open Password Reset modal @@ -90,7 +100,7 @@ const ProfileMenu = (props: ProfileMenuProps) => { */ const handleLogoutClick = useCallback(() => { handleLogout(); - }, []); + }, [handleLogout]); //======================================================================================== /* * @@ -98,13 +108,23 @@ const ProfileMenu = (props: ProfileMenuProps) => { * */ //======================================================================================== + useEffect(() => { + if (isMenuOpen) { + menuOpenAnimation.current = 1; + triggerButtonRef.current?.click(); + } + }, [isMenuOpen]); + // On component mount useEffect(() => { // Set authenticated user name setUsername(user.getUsername()); }, [user]); - const customEl = useMemo(() => getCustomMenuElements(menuItemConf, classes), [menuItemConf, classes]); + const customEl = useMemo( + () => getCustomMenuElements(menuItemConf, classes), + [menuItemConf, classes] + ); //======================================================================================== /* * @@ -116,6 +136,7 @@ const ProfileMenu = (props: ProfileMenuProps) => {
{ { {i18n.t("Change Password")} )} - { customEl } + {customEl} {handleToggleTheme && (
{darkThemeLabel} diff --git a/src/Components/ProfileMenu/types.ts b/src/Components/ProfileMenu/types.ts index aa0c49a2..f3cb91a5 100644 --- a/src/Components/ProfileMenu/types.ts +++ b/src/Components/ProfileMenu/types.ts @@ -7,15 +7,19 @@ export interface ProfileMenuProps { version: string; extraItems: { func: MouseEventHandler; label: string }[]; isDarkTheme: boolean; + isMenuOpen?: boolean; + onClose?: Function; handleLogout: Function; handleToggleTheme: ( event: React.ChangeEvent, checked: boolean ) => void; menuItemConf: { - [key: string]: { - handler: Function, - title: string, - } | React.ElementType, - }, + [key: string]: + | { + handler: Function; + title: string; + } + | React.ElementType; + }; }