-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplitButton.tsx
More file actions
153 lines (138 loc) · 3.96 KB
/
SplitButton.tsx
File metadata and controls
153 lines (138 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
Button,
ButtonGroup,
ButtonGroupProps,
ButtonProps,
ClickAwayListener,
Grow,
MenuItem,
MenuList,
Paper,
Popper,
} from '@mui/material';
import { alpha } from '@mui/material/styles';
import createStyles from '@mui/styles/createStyles';
import makeStyles from '@mui/styles/makeStyles';
import { MenuDown as ArrowDropDownIcon } from 'mdi-material-ui';
import React from 'react';
import { useLogger } from '../util/Logger';
const useStyles = makeStyles((theme) =>
createStyles({
button: {
'&:hover': {
background: alpha(theme.palette.primary.main, theme.palette.action.hoverOpacity),
},
'&:not(:last-child)': {
borderRightColor: theme.palette.getContrastText(theme.palette.primary.main),
},
},
})
);
interface ButtonOption {
label: string;
disabled?: boolean;
ButtonProps?: ButtonProps & { component?: React.ElementType; to?: string };
}
interface Props extends ButtonGroupProps {
options: ButtonOption[];
initiallySelected?: number;
variant?: ButtonProps['variant'];
color?: ButtonProps['color'];
onMenuItemClick?: (index: number) => void;
}
function SplitButton({
options,
initiallySelected,
variant,
color,
onMenuItemClick,
...props
}: Props): JSX.Element {
const [open, setOpen] = React.useState(false);
const anchorRef = React.useRef<HTMLDivElement>(null);
const logger = useLogger('SplitButton');
const [selectedIndex, setSelectedIndex] = React.useState(() => {
if (initiallySelected === undefined) {
return 0;
}
if (initiallySelected < 0 || initiallySelected >= options.length) {
logger.warn(`Initially selected value is invalid. Value: ${initiallySelected}`);
return 0;
}
return initiallySelected;
});
const classes = useStyles();
const { ButtonProps: buttonProps } = options[selectedIndex];
const handleMenuItemClick = (
event: React.MouseEvent<HTMLLIElement, MouseEvent>,
index: number
) => {
setSelectedIndex(index);
setOpen(false);
if (onMenuItemClick) {
onMenuItemClick(index);
}
};
const handleToggle = () => {
setOpen((prevOpen) => !prevOpen);
};
const handleClose = (event: MouseEvent | TouchEvent) => {
if (anchorRef.current?.contains(event.target as HTMLElement)) {
return;
}
setOpen(false);
};
return (
<>
<ButtonGroup
variant={variant}
color={color}
ref={anchorRef}
aria-label='split button'
{...props}
classes={{ grouped: classes.button }}
>
<Button disabled={options[selectedIndex].disabled} {...buttonProps}>
{options[selectedIndex].label}
</Button>
<Button
size='small'
aria-controls={open ? 'split-button-menu' : undefined}
aria-expanded={open ? 'true' : undefined}
aria-haspopup='menu'
onClick={handleToggle}
>
<ArrowDropDownIcon />
</Button>
</ButtonGroup>
<Popper open={open} anchorEl={anchorRef.current} role={undefined} transition>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
}}
>
<Paper>
<ClickAwayListener onClickAway={handleClose}>
<MenuList id='split-button-menu'>
{options.map((option, index) => (
<MenuItem
key={option.label}
disabled={option.disabled ?? false}
selected={index === selectedIndex}
onClick={(event) => handleMenuItemClick(event, index)}
>
{option.label}
</MenuItem>
))}
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</>
);
}
export default SplitButton;