Skip to content

Commit 12fa8c0

Browse files
joshwoodingoliviertassinari
authored andcommitted
[Menu] Convert to function component (#15068)
* Remove extra propTypes and redundant setProps * [Menu] Convert to function component * eps1lon review * olivier review
1 parent 073882f commit 12fa8c0

File tree

5 files changed

+141
-127
lines changed

5 files changed

+141
-127
lines changed

packages/material-ui/src/Fade/Fade.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('<Fade />', () => {
3131
const handleEnter = spy();
3232
const handleEntering = spy();
3333

34-
const wrapper = mount(
34+
mount(
3535
<Fade
3636
onEnter={handleEnter}
3737
onEntering={handleEntering}
@@ -47,10 +47,6 @@ describe('<Fade />', () => {
4747
<div />
4848
</Fade>,
4949
);
50-
51-
wrapper.setProps({
52-
in: true,
53-
});
5450
});
5551
});
5652

packages/material-ui/src/Menu/Menu.js

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import React from 'react';
44
import PropTypes from 'prop-types';
55
import withStyles from '../styles/withStyles';
6-
import withForwardedRef from '../utils/withForwardedRef';
76
import Popover from '../Popover';
87
import MenuList from '../MenuList';
98

@@ -29,92 +28,80 @@ export const styles = {
2928
},
3029
};
3130

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();
4749
};
4850

49-
handleEntering = element => {
50-
const { disableAutoFocusItem, theme } = this.props;
51-
52-
if (this.menuListActionsRef.current) {
51+
const handleEntering = element => {
52+
if (menuListActionsRef.current) {
5353
// Focus so the scroll computation of the Popover works as expected.
5454
if (disableAutoFocusItem !== true) {
55-
this.menuListActionsRef.current.focus();
55+
menuListActionsRef.current.focus();
5656
}
57-
this.menuListActionsRef.current.adjustStyleForScrollbar(element, theme);
57+
menuListActionsRef.current.adjustStyleForScrollbar(element, theme);
5858
}
5959

60-
if (this.props.onEntering) {
61-
this.props.onEntering(element);
60+
if (onEntering) {
61+
onEntering(element);
6262
}
6363
};
6464

65-
handleListKeyDown = event => {
65+
const handleListKeyDown = event => {
6666
if (event.key === 'Tab') {
6767
event.preventDefault();
6868

69-
if (this.props.onClose) {
70-
this.props.onClose(event, 'tabKeyDown');
69+
if (onClose) {
70+
onClose(event, 'tabKeyDown');
7171
}
7272
}
7373
};
7474

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}
10599
>
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+
});
118105

119106
Menu.propTypes = {
120107
/**
@@ -134,11 +121,6 @@ Menu.propTypes = {
134121
* If `true`, the selected / first menu item will not be auto focused.
135122
*/
136123
disableAutoFocusItem: PropTypes.bool,
137-
/**
138-
* @ignore
139-
* from `withForwardRef`
140-
*/
141-
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
142124
/**
143125
* Properties applied to the [`MenuList`](/api/menu-list/) element.
144126
*/
@@ -205,4 +187,4 @@ Menu.defaultProps = {
205187
transitionDuration: 'auto',
206188
};
207189

208-
export default withStyles(styles, { name: 'MuiMenu', withTheme: true })(withForwardedRef(Menu));
190+
export default withStyles(styles, { name: 'MuiMenu', withTheme: true })(Menu);

0 commit comments

Comments
 (0)