UNPKG

aws-northstar

Version:
122 lines (118 loc) 6.08 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; /** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import React, { useCallback, useEffect, } from 'react'; import Typography from '@material-ui/core/Typography'; import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; import ArrowDropDown from '@material-ui/icons/ArrowDropDown'; import { makeStyles } from '@material-ui/core/styles'; import clsx from 'clsx'; import { v4 as uuidv4 } from 'uuid'; import Box from '../../layouts/Box'; import Button from '../Button'; import useUniqueId from '../../hooks/useUniqueId'; const useStyles = makeStyles((theme) => ({ menuItem: { padding: '5px 20px', }, subHeading: { padding: '5px 20px', fontSize: '14px', fontWeight: 700, color: theme.palette.grey[600], }, disabledSubHeading: { opacity: '0.5', cursor: 'default', }, childMenuItem: { paddingLeft: '24px', '&:first-of-type': { borderTop: `1px solid ${theme.palette.grey[200]}`, }, '&:last-of-type': { borderBottom: `1px solid ${theme.palette.grey[200]}`, }, }, darkTheme: { '& .MuiButton-root': { border: 'none', padding: '4px 5px', color: 'currentColor', '&:hover': { color: 'currentColor', }, }, }, })); /** * A button dropdown is used to group a set of actions under one button. */ const ButtonDropdown = (_a) => { var { content, items = [], variant = 'normal', loading, disabled, disableArrowDropdown = false, menuItemClassName, darkTheme, onClick } = _a, props = __rest(_a, ["content", "items", "variant", "loading", "disabled", "disableArrowDropdown", "menuItemClassName", "darkTheme", "onClick"]); const classes = useStyles(); const menuId = useUniqueId(); const [anchorEl, setAnchorEl] = React.useState(null); const testId = props['data-testid'] || 'button-dropdown'; const handleClick = useCallback((event) => { onClick === null || onClick === void 0 ? void 0 : onClick(event); if (items.length > 0) { setAnchorEl(event.currentTarget); } }, [onClick, items, setAnchorEl]); const handleClose = useCallback(() => { setAnchorEl(null); }, [setAnchorEl]); const handleMenuItemClick = useCallback((item) => { return (e) => { var _a; (_a = item.onClick) === null || _a === void 0 ? void 0 : _a.call(item, e); if (!(e.isDefaultPrevented() || e.isPropagationStopped())) { handleClose(); } }; }, [handleClose]); useEffect(() => { if (items.length === 0 && anchorEl != null) { handleClose(); } }, [items, anchorEl, handleClose]); const renderMenuItem = useCallback((item) => (React.createElement(MenuItem, { key: uuidv4(), className: clsx(menuItemClassName, classes.menuItem), onClick: handleMenuItemClick(item), disabled: item.disabled, dense: true }, item.text)), [menuItemClassName, handleMenuItemClick, classes.menuItem]); const renderMenuItemWithHeading = useCallback((item) => { var _a; return (React.createElement(Box, { key: uuidv4() }, React.createElement(Typography, { className: clsx(classes.subHeading, { [classes.disabledSubHeading]: item.disabled }), variant: "subtitle1" }, item.text), (_a = item.items) === null || _a === void 0 ? void 0 : _a.map((itemChild) => (React.createElement(MenuItem, { key: uuidv4(), className: clsx(menuItemClassName, classes.menuItem, classes.childMenuItem), onClick: itemChild.onClick, disabled: item.disabled || itemChild.disabled, dense: true }, itemChild.text))))); }, [classes, menuItemClassName]); return (React.createElement(Box, { className: clsx({ [classes.darkTheme]: darkTheme }), "data-testid": testId }, React.createElement(Button, { variant: variant, onClick: handleClick, loading: loading, disabled: disabled, "data-testid": `${testId}-button`, "aria-haspopup": "true", "aria-controls": menuId }, content, " ", !disableArrowDropdown && React.createElement(ArrowDropDown, { fontSize: "small" })), React.createElement(Menu, { id: menuId, anchorEl: anchorEl, keepMounted: true, getContentAnchorEl: null, anchorOrigin: { vertical: 'bottom', horizontal: 'left', }, elevation: 2, transitionDuration: 0, open: Boolean(anchorEl), onClose: handleClose, "data-testid": `${testId}-menu` }, items.map((item) => item.items ? renderMenuItemWithHeading(item) : renderMenuItem(item))))); }; export default ButtonDropdown;