-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68d75cc
commit aa2bb95
Showing
19 changed files
with
254 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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 MenuList from '@mui/material/MenuList'; | ||
|
||
type SplitButtonProps = { | ||
label?: string; | ||
children?: React.ReactNode; | ||
icon?: React.ReactNode; | ||
disabled?: boolean; | ||
secondary?: boolean; | ||
large?: boolean; | ||
onClick: () => void; | ||
}; | ||
|
||
export default function SplitButton({ | ||
label, | ||
icon, | ||
large, | ||
disabled, | ||
secondary, | ||
children, | ||
onClick, | ||
}: SplitButtonProps) { | ||
const [open, setOpen] = React.useState(false); | ||
const anchorRef = React.useRef<HTMLDivElement>(null); | ||
|
||
const handleToggle = () => { | ||
setOpen((prevOpen) => !prevOpen); | ||
}; | ||
|
||
const handleClose = (event: Event) => { | ||
if ( | ||
anchorRef.current && | ||
anchorRef.current.contains(event.target as HTMLElement) | ||
) { | ||
return; | ||
} | ||
|
||
setOpen(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ButtonGroup | ||
variant="contained" | ||
color={secondary ? 'secondary' : 'primary'} | ||
size={large ? 'large' : 'medium'} | ||
ref={anchorRef} | ||
aria-label={label} | ||
disabled={disabled} | ||
> | ||
<Button disabled={disabled} startIcon={icon} onClick={onClick}> | ||
{label} | ||
</Button> | ||
<Button | ||
disabled={disabled} | ||
size="small" | ||
aria-controls={open ? 'split-button-menu' : undefined} | ||
aria-expanded={open ? 'true' : undefined} | ||
aria-label="select other options" | ||
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> | ||
{children} | ||
</MenuList> | ||
</ClickAwayListener> | ||
</Paper> | ||
</Grow> | ||
)} | ||
</Popper> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.