import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import Button from "@material-ui/core/Button"; import ButtonGroup from "@material-ui/core/ButtonGroup"; import ArrowDropDownIcon from "@material-ui/icons/ArrowDropDown"; import ClickAwayListener from "@material-ui/core/ClickAwayListener"; import Grow from "@material-ui/core/Grow"; import Paper from "@material-ui/core/Paper"; import Popper from "@material-ui/core/Popper"; import MenuItem from "@material-ui/core/MenuItem"; import MenuList from "@material-ui/core/MenuList"; import { isDarkTheme } from "../theme"; interface Option { label: string; key: string; } interface Props { options: Option[]; initialSelectedKey: string; onSelect: (key: string) => void; } const useStyles = makeStyles((theme) => ({ popper: { zIndex: 2, }, buttonContained: { backgroundColor: isDarkTheme(theme) ? "#303030" : theme.palette.background.default, color: theme.palette.text.primary, "&:hover": { backgroundColor: theme.palette.action.hover, }, }, })); export default function SplitButton(props: Props) { const classes = useStyles(); const [open, setOpen] = React.useState(false); const anchorRef = React.useRef(null); const [selectedKey, setSelectedKey] = React.useState( props.initialSelectedKey ); const handleMenuItemClick = ( event: React.MouseEvent, key: string ) => { setSelectedKey(key); setOpen(false); props.onSelect(key); }; const handleToggle = () => { setOpen((prevOpen) => !prevOpen); }; const handleClose = (event: React.MouseEvent) => { if ( anchorRef.current && anchorRef.current.contains(event.target as HTMLElement) ) { return; } setOpen(false); }; const selectedOpt = props.options.find((opt) => opt.key === selectedKey); return ( <> {({ TransitionProps, placement }) => ( {props.options.map((opt) => ( handleMenuItemClick(event, opt.key)} > {opt.label} ))} )} ); }