Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
97 lines
2.8 KiB
JavaScript
97 lines
2.8 KiB
JavaScript
import * as React from 'react';
|
|
import Button from '@mui/material/Button';
|
|
import ButtonGroup from '@mui/material/ButtonGroup';
|
|
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
|
|
import ClickAwayListener from '@mui/material/ClickAwayListener';
|
|
import Grow from '@mui/material/Grow';
|
|
import Paper from '@mui/material/Paper';
|
|
import Popper from '@mui/material/Popper';
|
|
import MenuItem from '@mui/material/MenuItem';
|
|
import MenuList from '@mui/material/MenuList';
|
|
|
|
const options = ['Create a merge commit', 'Squash and merge', 'Rebase and merge'];
|
|
|
|
export default function SplitButton() {
|
|
const [open, setOpen] = React.useState(false);
|
|
const anchorRef = React.useRef(null);
|
|
const [selectedIndex, setSelectedIndex] = React.useState(1);
|
|
|
|
const handleClick = () => {
|
|
console.info(`You clicked ${options[selectedIndex]}`);
|
|
};
|
|
|
|
const handleMenuItemClick = (event, index) => {
|
|
setSelectedIndex(index);
|
|
setOpen(false);
|
|
};
|
|
|
|
const handleToggle = () => {
|
|
setOpen((prevOpen) => !prevOpen);
|
|
};
|
|
|
|
const handleClose = (event) => {
|
|
if (anchorRef.current && anchorRef.current.contains(event.target)) {
|
|
return;
|
|
}
|
|
|
|
setOpen(false);
|
|
};
|
|
|
|
return (
|
|
<React.Fragment>
|
|
<ButtonGroup
|
|
variant="contained"
|
|
ref={anchorRef}
|
|
aria-label="Button group with a nested menu"
|
|
>
|
|
<Button onClick={handleClick}>{options[selectedIndex]}</Button>
|
|
<Button
|
|
size="small"
|
|
aria-controls={open ? 'split-button-menu' : undefined}
|
|
aria-expanded={open ? 'true' : undefined}
|
|
aria-label="select merge strategy"
|
|
aria-haspopup="menu"
|
|
onClick={handleToggle}
|
|
>
|
|
<ArrowDropDownIcon />
|
|
</Button>
|
|
</ButtonGroup>
|
|
<Popper
|
|
sx={{ zIndex: 1 }}
|
|
open={open}
|
|
anchorEl={anchorRef.current}
|
|
role={undefined}
|
|
transition
|
|
disablePortal
|
|
>
|
|
{({ TransitionProps, placement }) => (
|
|
<Grow
|
|
{...TransitionProps}
|
|
style={{
|
|
transformOrigin:
|
|
placement === 'bottom' ? 'center top' : 'center bottom',
|
|
}}
|
|
>
|
|
<Paper>
|
|
<ClickAwayListener onClickAway={handleClose}>
|
|
<MenuList id="split-button-menu" autoFocusItem>
|
|
{options.map((option, index) => (
|
|
<MenuItem
|
|
key={option}
|
|
disabled={index === 2}
|
|
selected={index === selectedIndex}
|
|
onClick={(event) => handleMenuItemClick(event, index)}
|
|
>
|
|
{option}
|
|
</MenuItem>
|
|
))}
|
|
</MenuList>
|
|
</ClickAwayListener>
|
|
</Paper>
|
|
</Grow>
|
|
)}
|
|
</Popper>
|
|
</React.Fragment>
|
|
);
|
|
}
|