@@ -26,9 +26,21 @@ import PropTypes from 'prop-types';
26
26
// of doing reusable widgets like dialog boxes & menus where we go and
27
27
// pass in a custom control as the actual body.
28
28
29
- module . exports = {
30
- ContextualMenuContainerId : "mx_ContextualMenu_Container" ,
29
+ const ContextualMenuContainerId = "mx_ContextualMenu_Container" ;
30
+
31
+ function getOrCreateContainer ( ) {
32
+ let container = document . getElementById ( ContextualMenuContainerId ) ;
33
+
34
+ if ( ! container ) {
35
+ container = document . createElement ( "div" ) ;
36
+ container . id = ContextualMenuContainerId ;
37
+ document . body . appendChild ( container ) ;
38
+ }
39
+
40
+ return container ;
41
+ }
31
42
43
+ class ContextualMenu extends React . Component {
32
44
propTypes : {
33
45
top : PropTypes . number ,
34
46
bottom : PropTypes . number ,
@@ -45,39 +57,14 @@ module.exports = {
45
57
menuPaddingRight : PropTypes . number ,
46
58
menuPaddingBottom : PropTypes . number ,
47
59
menuPaddingLeft : PropTypes . number ,
48
- } ,
49
-
50
- getOrCreateContainer : function ( ) {
51
- let container = document . getElementById ( this . ContextualMenuContainerId ) ;
52
-
53
- if ( ! container ) {
54
- container = document . createElement ( "div" ) ;
55
- container . id = this . ContextualMenuContainerId ;
56
- document . body . appendChild ( container ) ;
57
- }
58
-
59
- return container ;
60
- } ,
61
-
62
- createMenu : function ( Element , props ) {
63
- const self = this ;
64
-
65
- const closeMenu = function ( ...args ) {
66
- ReactDOM . unmountComponentAtNode ( self . getOrCreateContainer ( ) ) ;
67
-
68
- if ( props && props . onFinished ) {
69
- props . onFinished . apply ( null , args ) ;
70
- }
71
- } ;
72
-
73
- // Close the menu on window resize
74
- const windowResize = function ( ) {
75
- closeMenu ( ) ;
76
- } ;
60
+ }
77
61
62
+ render ( ) {
78
63
const position = { } ;
79
64
let chevronFace = null ;
80
65
66
+ const props = this . props ;
67
+
81
68
if ( props . top ) {
82
69
position . top = props . top ;
83
70
} else {
@@ -158,20 +145,39 @@ module.exports = {
158
145
menuStyle [ "paddingRight" ] = props . menuPaddingRight ;
159
146
}
160
147
148
+ const ElementClass = props . elementClass ;
149
+
161
150
// FIXME: If a menu uses getDefaultProps it clobbers the onFinished
162
151
// property set here so you can't close the menu from a button click!
163
- const menu = (
164
- < div className = { className } style = { position } >
165
- < div className = { menuClasses } style = { menuStyle } >
166
- { chevron }
167
- < Element { ...props } onFinished = { closeMenu } onResize = { windowResize } />
168
- </ div >
169
- < div className = "mx_ContextualMenu_background" onClick = { closeMenu } > </ div >
170
- < style > { chevronCSS } </ style >
152
+ return < div className = { className } style = { position } >
153
+ < div className = { menuClasses } style = { menuStyle } >
154
+ { chevron }
155
+ < ElementClass { ...props } onFinished = { props . closeMenu } onResize = { props . windowResize } />
171
156
</ div >
172
- ) ;
157
+ < div className = "mx_ContextualMenu_background" onClick = { props . closeMenu } > </ div >
158
+ < style > { chevronCSS } </ style >
159
+ </ div > ;
160
+ }
161
+ }
162
+
163
+ module . exports = {
164
+ createMenu : function ( ElementClass , props ) {
165
+ const closeMenu = function ( ...args ) {
166
+ ReactDOM . unmountComponentAtNode ( getOrCreateContainer ( ) ) ;
167
+
168
+ if ( props && props . onFinished ) {
169
+ props . onFinished . apply ( null , args ) ;
170
+ }
171
+ } ;
172
+
173
+ const menu = < ContextualMenu
174
+ { ...props }
175
+ elementClass = { ElementClass }
176
+ closeMenu = { closeMenu }
177
+ windowResize = { closeMenu }
178
+ /> ;
173
179
174
- ReactDOM . render ( menu , this . getOrCreateContainer ( ) ) ;
180
+ ReactDOM . render ( menu , getOrCreateContainer ( ) ) ;
175
181
176
182
return { close : closeMenu } ;
177
183
} ,
0 commit comments