-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGridMenu.tsx
154 lines (137 loc) · 4.32 KB
/
GridMenu.tsx
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
154
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import ClickAwayListener, { ClickAwayListenerProps } from '@mui/material/ClickAwayListener';
import { unstable_composeClasses as composeClasses, HTMLElementType } from '@mui/utils';
import Grow, { GrowProps } from '@mui/material/Grow';
import Paper from '@mui/material/Paper';
import Popper, { PopperProps } from '@mui/material/Popper';
import { styled } from '@mui/material/styles';
import { getDataGridUtilityClass, gridClasses } from '../../constants/gridClasses';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
type MenuPosition =
| 'bottom-end'
| 'bottom-start'
| 'bottom'
| 'left-end'
| 'left-start'
| 'left'
| 'right-end'
| 'right-start'
| 'right'
| 'top-end'
| 'top-start'
| 'top'
| undefined;
type OwnerState = { classes: DataGridProcessedProps['classes'] };
const useUtilityClasses = (ownerState: OwnerState) => {
const { classes } = ownerState;
const slots = {
root: ['menu'],
};
return composeClasses(slots, getDataGridUtilityClass, classes);
};
const GridMenuRoot = styled(Popper, {
name: 'MuiDataGrid',
slot: 'Menu',
overridesResolver: (props, styles) => styles.menu,
})(({ theme }) => ({
zIndex: theme.zIndex.modal,
[`& .${gridClasses.menuList}`]: {
outline: 0,
},
}));
export interface GridMenuProps extends Omit<PopperProps, 'onKeyDown' | 'children'> {
open: boolean;
target: HTMLElement | null;
onClickAway: ClickAwayListenerProps['onClickAway'];
position?: MenuPosition;
onExited?: GrowProps['onExited'];
children: React.ReactNode;
}
const transformOrigin = {
'bottom-start': 'top left',
'bottom-end': 'top right',
};
function GridMenu(props: GridMenuProps) {
const { open, target, onClickAway, children, position, className, onExited, ...other } = props;
const apiRef = useGridApiContext();
const prevTarget = React.useRef(target);
const prevOpen = React.useRef(open);
const rootProps = useGridRootProps();
const ownerState = { classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);
React.useEffect(() => {
if (prevOpen.current && prevTarget.current) {
(prevTarget.current as HTMLElement).focus();
}
// Emit menuOpen or menuClose events
const eventName = open ? 'menuOpen' : 'menuClose';
apiRef.current.publishEvent(eventName, { target });
prevOpen.current = open;
prevTarget.current = target;
}, [apiRef, open, target]);
const handleExited = (popperOnExited: (() => {}) | undefined) => (node: HTMLElement) => {
if (popperOnExited) {
popperOnExited();
}
if (onExited) {
onExited(node);
}
};
return (
<GridMenuRoot
as={rootProps.components.BasePopper}
className={clsx(className, classes.root)}
open={open}
anchorEl={target as any}
transition
placement={position}
{...other}
{...rootProps.componentsProps?.basePopper}
>
{({ TransitionProps, placement }) => (
<ClickAwayListener onClickAway={onClickAway} mouseEvent="onMouseDown">
<Grow
{...TransitionProps}
style={{ transformOrigin: transformOrigin[placement as keyof typeof transformOrigin] }}
onExited={handleExited(TransitionProps?.onExited)}
>
<Paper>{children}</Paper>
</Grow>
</ClickAwayListener>
)}
</GridMenuRoot>
);
}
GridMenu.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
children: PropTypes.node,
onClickAway: PropTypes.func.isRequired,
onExited: PropTypes.func,
/**
* If `true`, the component is shown.
*/
open: PropTypes.bool.isRequired,
position: PropTypes.oneOf([
'bottom-end',
'bottom-start',
'bottom',
'left-end',
'left-start',
'left',
'right-end',
'right-start',
'right',
'top-end',
'top-start',
'top',
]),
target: HTMLElementType,
} as any;
export { GridMenu };