3
3
import React from 'react' ;
4
4
import PropTypes from 'prop-types' ;
5
5
import withStyles from '../styles/withStyles' ;
6
- import withForwardedRef from '../utils/withForwardedRef' ;
7
6
import Popover from '../Popover' ;
8
7
import MenuList from '../MenuList' ;
9
8
@@ -29,92 +28,80 @@ export const styles = {
29
28
} ,
30
29
} ;
31
30
32
- class Menu extends React . Component {
33
- menuListActionsRef = React . createRef ( ) ;
34
-
35
- componentDidMount ( ) {
36
- if ( this . props . open && this . props . disableAutoFocusItem !== true ) {
37
- this . focus ( ) ;
38
- }
39
- }
40
-
41
- getContentAnchorEl = ( ) => {
42
- return this . menuListActionsRef . current . getContentAnchorEl ( ) ;
43
- } ;
44
-
45
- focus = ( ) => {
46
- return this . menuListActionsRef . current && this . menuListActionsRef . current . focus ( ) ;
31
+ const Menu = React . forwardRef ( function Menu ( props , ref ) {
32
+ const {
33
+ children,
34
+ classes,
35
+ disableAutoFocusItem,
36
+ MenuListProps,
37
+ onClose,
38
+ onEntering,
39
+ open,
40
+ PaperProps = { } ,
41
+ PopoverClasses,
42
+ theme,
43
+ ...other
44
+ } = props ;
45
+ const menuListActionsRef = React . useRef ( ) ;
46
+
47
+ const getContentAnchorEl = ( ) => {
48
+ return menuListActionsRef . current . getContentAnchorEl ( ) ;
47
49
} ;
48
50
49
- handleEntering = element => {
50
- const { disableAutoFocusItem, theme } = this . props ;
51
-
52
- if ( this . menuListActionsRef . current ) {
51
+ const handleEntering = element => {
52
+ if ( menuListActionsRef . current ) {
53
53
// Focus so the scroll computation of the Popover works as expected.
54
54
if ( disableAutoFocusItem !== true ) {
55
- this . menuListActionsRef . current . focus ( ) ;
55
+ menuListActionsRef . current . focus ( ) ;
56
56
}
57
- this . menuListActionsRef . current . adjustStyleForScrollbar ( element , theme ) ;
57
+ menuListActionsRef . current . adjustStyleForScrollbar ( element , theme ) ;
58
58
}
59
59
60
- if ( this . props . onEntering ) {
61
- this . props . onEntering ( element ) ;
60
+ if ( onEntering ) {
61
+ onEntering ( element ) ;
62
62
}
63
63
} ;
64
64
65
- handleListKeyDown = event => {
65
+ const handleListKeyDown = event => {
66
66
if ( event . key === 'Tab' ) {
67
67
event . preventDefault ( ) ;
68
68
69
- if ( this . props . onClose ) {
70
- this . props . onClose ( event , 'tabKeyDown' ) ;
69
+ if ( onClose ) {
70
+ onClose ( event , 'tabKeyDown' ) ;
71
71
}
72
72
}
73
73
} ;
74
74
75
- render ( ) {
76
- const {
77
- children,
78
- classes,
79
- disableAutoFocusItem,
80
- innerRef,
81
- MenuListProps,
82
- onEntering,
83
- PaperProps = { } ,
84
- PopoverClasses,
85
- theme,
86
- ...other
87
- } = this . props ;
88
-
89
- return (
90
- < Popover
91
- getContentAnchorEl = { this . getContentAnchorEl }
92
- classes = { PopoverClasses }
93
- onEntering = { this . handleEntering }
94
- anchorOrigin = { theme . direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN }
95
- transformOrigin = { theme . direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN }
96
- PaperProps = { {
97
- ...PaperProps ,
98
- classes : {
99
- ...PaperProps . classes ,
100
- root : classes . paper ,
101
- } ,
102
- } }
103
- ref = { innerRef }
104
- { ...other }
75
+ return (
76
+ < Popover
77
+ getContentAnchorEl = { getContentAnchorEl }
78
+ classes = { PopoverClasses }
79
+ onClose = { onClose }
80
+ onEntering = { handleEntering }
81
+ anchorOrigin = { theme . direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN }
82
+ transformOrigin = { theme . direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN }
83
+ PaperProps = { {
84
+ ...PaperProps ,
85
+ classes : {
86
+ ...PaperProps . classes ,
87
+ root : classes . paper ,
88
+ } ,
89
+ } }
90
+ open = { open }
91
+ ref = { ref }
92
+ { ...other }
93
+ >
94
+ < MenuList
95
+ data-mui-test = "Menu"
96
+ onKeyDown = { handleListKeyDown }
97
+ { ...MenuListProps }
98
+ actions = { menuListActionsRef }
105
99
>
106
- < MenuList
107
- data-mui-test = "Menu"
108
- onKeyDown = { this . handleListKeyDown }
109
- { ...MenuListProps }
110
- actions = { this . menuListActionsRef }
111
- >
112
- { children }
113
- </ MenuList >
114
- </ Popover >
115
- ) ;
116
- }
117
- }
100
+ { children }
101
+ </ MenuList >
102
+ </ Popover >
103
+ ) ;
104
+ } ) ;
118
105
119
106
Menu . propTypes = {
120
107
/**
@@ -134,11 +121,6 @@ Menu.propTypes = {
134
121
* If `true`, the selected / first menu item will not be auto focused.
135
122
*/
136
123
disableAutoFocusItem : PropTypes . bool ,
137
- /**
138
- * @ignore
139
- * from `withForwardRef`
140
- */
141
- innerRef : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . object ] ) ,
142
124
/**
143
125
* Properties applied to the [`MenuList`](/api/menu-list/) element.
144
126
*/
@@ -205,4 +187,4 @@ Menu.defaultProps = {
205
187
transitionDuration : 'auto' ,
206
188
} ;
207
189
208
- export default withStyles ( styles , { name : 'MuiMenu' , withTheme : true } ) ( withForwardedRef ( Menu ) ) ;
190
+ export default withStyles ( styles , { name : 'MuiMenu' , withTheme : true } ) ( Menu ) ;
0 commit comments