@targetprocess/menu 12.20.1-alpha.4 → 12.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -15
- package/Menu.jsx +329 -329
- package/MenuItem.jsx +129 -129
- package/MenuItemWithSubMenu.jsx +139 -139
- package/MenuSeparator.jsx +20 -20
- package/__tests__/Menu.tests.jsx +96 -96
- package/dist/menu.es.js +42 -39
- package/dist/menu.js +42 -39
- package/index.js +15 -12
- package/package.json +4 -4
- package/style/index.css +93 -90
package/Menu.jsx
CHANGED
|
@@ -1,329 +1,329 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
|
-
import PropTypes from 'prop-types'
|
|
3
|
-
import {Popover} from '@targetprocess/popover'
|
|
4
|
-
import {MenuSeparator} from './MenuSeparator'
|
|
5
|
-
import {MenuItem} from './MenuItem'
|
|
6
|
-
import styles from './style/index.css'
|
|
7
|
-
|
|
8
|
-
export const MenuTrigger = () => null
|
|
9
|
-
|
|
10
|
-
MenuTrigger.propTypes = {
|
|
11
|
-
children: PropTypes.any.isRequired
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const defaultMenuState = {
|
|
15
|
-
id: 0,
|
|
16
|
-
isActive: false,
|
|
17
|
-
isSubmitted: false,
|
|
18
|
-
isOpenMenu: false,
|
|
19
|
-
currentEvent: null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class Menu extends React.Component {
|
|
23
|
-
constructor(props) {
|
|
24
|
-
super(props)
|
|
25
|
-
|
|
26
|
-
this.menuItemsAvailableForNavigation = []
|
|
27
|
-
this.state = {
|
|
28
|
-
open: Boolean(this.props.isOpenInitially),
|
|
29
|
-
currentMenuState: {}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
this.commands = eventCode => {
|
|
33
|
-
const config = {
|
|
34
|
-
Escape: () => this.setOpened(false),
|
|
35
|
-
Enter: (activeMenuItem, {currentEvent}) => ({
|
|
36
|
-
value: {
|
|
37
|
-
...(activeMenuItem.isOpenMenu
|
|
38
|
-
? {...activeMenuItem, currentEvent}
|
|
39
|
-
: {
|
|
40
|
-
...activeMenuItem,
|
|
41
|
-
currentEvent,
|
|
42
|
-
isSubmitted: true
|
|
43
|
-
})
|
|
44
|
-
},
|
|
45
|
-
afterUpdateCallBack: () => this.setOpened(false)
|
|
46
|
-
}),
|
|
47
|
-
ArrowDown: (activeMenuItem, {maxLength, currentEvent}) => ({
|
|
48
|
-
value: {
|
|
49
|
-
...(activeMenuItem.isOpenMenu
|
|
50
|
-
? {...activeMenuItem, currentEvent}
|
|
51
|
-
: {
|
|
52
|
-
...activeMenuItem,
|
|
53
|
-
isActive: true,
|
|
54
|
-
id: maxLength === activeMenuItem.id ? activeMenuItem.id : activeMenuItem.id + 1
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
}),
|
|
58
|
-
ArrowUp: (activeMenuItem, {minLength, currentEvent}) => ({
|
|
59
|
-
value: {
|
|
60
|
-
...(activeMenuItem.isOpenMenu
|
|
61
|
-
? {...activeMenuItem, currentEvent}
|
|
62
|
-
: {
|
|
63
|
-
...activeMenuItem,
|
|
64
|
-
isActive: true,
|
|
65
|
-
id: minLength > activeMenuItem.id - 1 ? activeMenuItem.id : activeMenuItem.id - 1
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
}),
|
|
69
|
-
[this.props.isLeftOpen ? 'ArrowLeft' : 'ArrowRight']: (activeMenuItem, {currentEvent}) => {
|
|
70
|
-
const activeElement = this.menuItemsAvailableForNavigation[activeMenuItem.id]
|
|
71
|
-
const newValue = activeMenuItem.isOpenMenu
|
|
72
|
-
? {...activeMenuItem, currentEvent}
|
|
73
|
-
: {...activeMenuItem, isOpenMenu: true, currentEvent}
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
value: activeElement.type === MenuItem ? activeMenuItem : newValue
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
[this.props.isLeftOpen ? 'ArrowRight' : 'ArrowLeft']: (activeMenuItem, {currentEvent}) => {
|
|
80
|
-
const defaultValue = this.isParentMenu() ? activeMenuItem : {}
|
|
81
|
-
const value = activeMenuItem.isOpenMenu ? {...activeMenuItem, currentEvent} : defaultValue
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
value,
|
|
85
|
-
afterUpdateCallBack: () =>
|
|
86
|
-
!activeMenuItem.isOpenMenu && this.handleMenuByParent('CloseCurrentMenu')
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
// Custom actions
|
|
90
|
-
CloseCurrentMenu: activeMenuItem => ({
|
|
91
|
-
value: {...activeMenuItem, currentEvent: null, isOpenMenu: false}
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// eventCode generated by library here
|
|
96
|
-
// eslint-disable-next-line security/detect-object-injection
|
|
97
|
-
return config[eventCode]
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* LifeCycle Methods
|
|
103
|
-
*/
|
|
104
|
-
componentDidMount() {
|
|
105
|
-
// Assume that children will be passed in sync manner
|
|
106
|
-
this.menuItemsAvailableForNavigation = this.getMenuItemsState()
|
|
107
|
-
if (this.props.enableKeyboardNavigation) {
|
|
108
|
-
document.addEventListener('keydown', this.handleKeyboard)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
componentWillReceiveProps({navigationState}) {
|
|
113
|
-
if (this.props.navigationState.isOpenMenu !== navigationState.isOpenMenu) {
|
|
114
|
-
this.setInitialMenuState(navigationState.isOpenMenu)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Transfering of control from parent menu to submenu
|
|
118
|
-
// by dispatching `handleKeyboard` manually
|
|
119
|
-
if (
|
|
120
|
-
Boolean(navigationState.currentEvent) &&
|
|
121
|
-
navigationState.isOpenMenu &&
|
|
122
|
-
this.props.navigationState !== navigationState
|
|
123
|
-
) {
|
|
124
|
-
this.handleKeyboard({code: navigationState.currentEvent})
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
componentWillUnmount() {
|
|
129
|
-
if (this.props.enableKeyboardNavigation) {
|
|
130
|
-
document.removeEventListener('keydown', this.handleKeyboard)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Assume that only parent menu have this flag
|
|
135
|
-
isParentMenu = () => this.props.enableKeyboardNavigation
|
|
136
|
-
|
|
137
|
-
isControlledMode = (props = this.props) =>
|
|
138
|
-
typeof props.open !== 'undefined' && props.open !== null
|
|
139
|
-
|
|
140
|
-
handleMenuByParent = eventCode =>
|
|
141
|
-
this.props.navigationState.onChangeMenuState &&
|
|
142
|
-
this.props.navigationState.onChangeMenuState(eventCode)
|
|
143
|
-
|
|
144
|
-
handleKeyboard = event => {
|
|
145
|
-
const {currentMenuState} = this.state
|
|
146
|
-
const indexOfLastChildInRow = this.menuItemsAvailableForNavigation.length - 1
|
|
147
|
-
const getNewState = this.commands(event.code)
|
|
148
|
-
|
|
149
|
-
if (!getNewState || !this.state.open) {
|
|
150
|
-
return
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const newMenuState = getNewState(currentMenuState, {
|
|
154
|
-
minLength: 0,
|
|
155
|
-
maxLength: indexOfLastChildInRow,
|
|
156
|
-
currentEvent: event.code
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
if (!newMenuState || !newMenuState.value) {
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
this.setState({currentMenuState: newMenuState.value}, newMenuState.afterUpdateCallBack)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
getMenuItemsState = () =>
|
|
167
|
-
React.Children.toArray(this.props.children).filter(
|
|
168
|
-
({type}) => type !== MenuSeparator && type !== MenuTrigger
|
|
169
|
-
)
|
|
170
|
-
|
|
171
|
-
setOpened = open => {
|
|
172
|
-
if (!this.isControlledMode()) {
|
|
173
|
-
this.setState({open})
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (this.isParentMenu()) {
|
|
177
|
-
this.setInitialMenuState(open)
|
|
178
|
-
}
|
|
179
|
-
// Force clean state, when user navigating by mouse
|
|
180
|
-
if (!open) {
|
|
181
|
-
this.handleMenuByParent('CloseCurrentMenu')
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (this.props.onSetOpened) {
|
|
185
|
-
this.props.onSetOpened(open)
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
setInitialMenuState = open =>
|
|
190
|
-
this.setState(
|
|
191
|
-
open
|
|
192
|
-
? {open, currentMenuState: {...defaultMenuState, isActive: true}}
|
|
193
|
-
: {open, currentMenuState: {}}
|
|
194
|
-
)
|
|
195
|
-
|
|
196
|
-
getChildren = () => {
|
|
197
|
-
if (!this.props.enableKeyboardNavigation) {
|
|
198
|
-
return React.Children.toArray(this.props.children)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
let counter = 0
|
|
202
|
-
const {currentMenuState} = this.state
|
|
203
|
-
const {children, isLeftOpen} = this.props
|
|
204
|
-
const navigationState = {
|
|
205
|
-
...currentMenuState,
|
|
206
|
-
onChangeMenuState: code => this.handleKeyboard({code})
|
|
207
|
-
}
|
|
208
|
-
const setAdditionalProps = element =>
|
|
209
|
-
isLeftOpen ? React.cloneElement(element, {isLeftOpen}) : element
|
|
210
|
-
|
|
211
|
-
return React.Children.toArray(children).map(child => {
|
|
212
|
-
let element = child
|
|
213
|
-
|
|
214
|
-
if (child.type !== MenuSeparator && child.type !== MenuTrigger) {
|
|
215
|
-
const isCurrentActiveItem = currentMenuState && currentMenuState.id === counter
|
|
216
|
-
|
|
217
|
-
element = isCurrentActiveItem
|
|
218
|
-
? React.cloneElement(child, {
|
|
219
|
-
navigationState,
|
|
220
|
-
isLeftOpen
|
|
221
|
-
})
|
|
222
|
-
: setAdditionalProps(child)
|
|
223
|
-
|
|
224
|
-
counter += 1
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return element
|
|
228
|
-
})
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
onOuterAction = () => this.props.closeOnOuterAction && this.setOpened(false)
|
|
232
|
-
|
|
233
|
-
prepareTrigger = menuChildren => {
|
|
234
|
-
const {isLeftOpen, navigationState} = this.props
|
|
235
|
-
const isOpen = this.isControlledMode() ? this.props.open : this.state.open
|
|
236
|
-
const menuTrigger = menuChildren.find(element => element.type === MenuTrigger)
|
|
237
|
-
if (!menuTrigger) {
|
|
238
|
-
throw new Error(
|
|
239
|
-
'Trigger not found in Menu children. Did you forget to add <MenuTrigger> to <Menu> ?'
|
|
240
|
-
)
|
|
241
|
-
}
|
|
242
|
-
const {children} = menuTrigger.props
|
|
243
|
-
|
|
244
|
-
const renderCallbackArgs = {
|
|
245
|
-
isOpen,
|
|
246
|
-
isLeftOpen,
|
|
247
|
-
navigationState,
|
|
248
|
-
key: navigationState.id,
|
|
249
|
-
setOpened: this.setOpened
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const trigger = typeof children === 'function' ? children(renderCallbackArgs) : children
|
|
253
|
-
|
|
254
|
-
const getPopoverPosition =
|
|
255
|
-
typeof this.props.getPopoverPosition === 'undefined'
|
|
256
|
-
? hostRect =>
|
|
257
|
-
isLeftOpen ? {right: hostRect.width, top: 0} : {left: hostRect.width, top: 0}
|
|
258
|
-
: this.props.getPopoverPosition
|
|
259
|
-
|
|
260
|
-
return {trigger, getPopoverPosition}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
render() {
|
|
264
|
-
const children = this.getChildren()
|
|
265
|
-
const isOpen = this.isControlledMode() ? this.props.open : this.state.open
|
|
266
|
-
const menuItems = children.filter(element => element.type !== MenuTrigger)
|
|
267
|
-
const {trigger, getPopoverPosition} = this.prepareTrigger(children)
|
|
268
|
-
const renderBody =
|
|
269
|
-
this.props.renderBody || (items => <Popover.Body noPadding>{items}</Popover.Body>)
|
|
270
|
-
|
|
271
|
-
return (
|
|
272
|
-
<Popover
|
|
273
|
-
className={styles['menu']}
|
|
274
|
-
isOpen={isOpen}
|
|
275
|
-
onOuterAction={this.onOuterAction}
|
|
276
|
-
body={renderBody(menuItems)}
|
|
277
|
-
inline={this.props.appendInline}
|
|
278
|
-
relativeToParent={this.props.relativeToParent}
|
|
279
|
-
getPopoverPosition={getPopoverPosition}
|
|
280
|
-
>
|
|
281
|
-
{trigger}
|
|
282
|
-
</Popover>
|
|
283
|
-
)
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
Menu.propTypes = {
|
|
288
|
-
open: PropTypes.bool,
|
|
289
|
-
/** Indicates that submenu will be inserted into DOM as siblings to menu trigger */
|
|
290
|
-
appendInline: PropTypes.bool,
|
|
291
|
-
/** Indicates that popover positioning offsets (i.e. `top` and `left` with
|
|
292
|
-
* `position: absolute`) should be calculated relative to parent element,
|
|
293
|
-
* rather than `document.body`. Useful when using `appendInline` and a container
|
|
294
|
-
* with `position: relative`.
|
|
295
|
-
* */
|
|
296
|
-
relativeToParent: PropTypes.bool,
|
|
297
|
-
onSetOpened: PropTypes.func,
|
|
298
|
-
isOpenInitially: PropTypes.bool,
|
|
299
|
-
/**
|
|
300
|
-
* This option may be provided only for parent menu,
|
|
301
|
-
* which in turn will be set for all submenus
|
|
302
|
-
* (This option may override `Trigger` offset option)
|
|
303
|
-
*/
|
|
304
|
-
isLeftOpen: PropTypes.bool,
|
|
305
|
-
closeOnOuterAction: PropTypes.bool,
|
|
306
|
-
/**
|
|
307
|
-
* Naviagtion` state represent set of characterisics
|
|
308
|
-
* that define current state of particular `Menu`, `MenuItem`
|
|
309
|
-
*/
|
|
310
|
-
getPopoverPosition: PropTypes.func,
|
|
311
|
-
/**
|
|
312
|
-
* Render callback that can be used to customize how menu container
|
|
313
|
-
* and children are rendered. Default is wrapping in Menu.Body.
|
|
314
|
-
*/
|
|
315
|
-
renderBody: PropTypes.func,
|
|
316
|
-
navigationState: PropTypes.shape({
|
|
317
|
-
id: PropTypes.number,
|
|
318
|
-
isActive: PropTypes.bool,
|
|
319
|
-
isSubmitted: PropTypes.bool,
|
|
320
|
-
isOpenMenu: PropTypes.bool,
|
|
321
|
-
currentEvent: PropTypes.string,
|
|
322
|
-
onChangeMenuState: PropTypes.func
|
|
323
|
-
})
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
Menu.defaultProps = {
|
|
327
|
-
closeOnOuterAction: true,
|
|
328
|
-
navigationState: {}
|
|
329
|
-
}
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import PropTypes from 'prop-types'
|
|
3
|
+
import {Popover} from '@targetprocess/popover'
|
|
4
|
+
import {MenuSeparator} from './MenuSeparator'
|
|
5
|
+
import {MenuItem} from './MenuItem'
|
|
6
|
+
import styles from './style/index.css'
|
|
7
|
+
|
|
8
|
+
export const MenuTrigger = () => null
|
|
9
|
+
|
|
10
|
+
MenuTrigger.propTypes = {
|
|
11
|
+
children: PropTypes.any.isRequired
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const defaultMenuState = {
|
|
15
|
+
id: 0,
|
|
16
|
+
isActive: false,
|
|
17
|
+
isSubmitted: false,
|
|
18
|
+
isOpenMenu: false,
|
|
19
|
+
currentEvent: null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Menu extends React.Component {
|
|
23
|
+
constructor(props) {
|
|
24
|
+
super(props)
|
|
25
|
+
|
|
26
|
+
this.menuItemsAvailableForNavigation = []
|
|
27
|
+
this.state = {
|
|
28
|
+
open: Boolean(this.props.isOpenInitially),
|
|
29
|
+
currentMenuState: {}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.commands = eventCode => {
|
|
33
|
+
const config = {
|
|
34
|
+
Escape: () => this.setOpened(false),
|
|
35
|
+
Enter: (activeMenuItem, {currentEvent}) => ({
|
|
36
|
+
value: {
|
|
37
|
+
...(activeMenuItem.isOpenMenu
|
|
38
|
+
? {...activeMenuItem, currentEvent}
|
|
39
|
+
: {
|
|
40
|
+
...activeMenuItem,
|
|
41
|
+
currentEvent,
|
|
42
|
+
isSubmitted: true
|
|
43
|
+
})
|
|
44
|
+
},
|
|
45
|
+
afterUpdateCallBack: () => this.setOpened(false)
|
|
46
|
+
}),
|
|
47
|
+
ArrowDown: (activeMenuItem, {maxLength, currentEvent}) => ({
|
|
48
|
+
value: {
|
|
49
|
+
...(activeMenuItem.isOpenMenu
|
|
50
|
+
? {...activeMenuItem, currentEvent}
|
|
51
|
+
: {
|
|
52
|
+
...activeMenuItem,
|
|
53
|
+
isActive: true,
|
|
54
|
+
id: maxLength === activeMenuItem.id ? activeMenuItem.id : activeMenuItem.id + 1
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
ArrowUp: (activeMenuItem, {minLength, currentEvent}) => ({
|
|
59
|
+
value: {
|
|
60
|
+
...(activeMenuItem.isOpenMenu
|
|
61
|
+
? {...activeMenuItem, currentEvent}
|
|
62
|
+
: {
|
|
63
|
+
...activeMenuItem,
|
|
64
|
+
isActive: true,
|
|
65
|
+
id: minLength > activeMenuItem.id - 1 ? activeMenuItem.id : activeMenuItem.id - 1
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
}),
|
|
69
|
+
[this.props.isLeftOpen ? 'ArrowLeft' : 'ArrowRight']: (activeMenuItem, {currentEvent}) => {
|
|
70
|
+
const activeElement = this.menuItemsAvailableForNavigation[activeMenuItem.id]
|
|
71
|
+
const newValue = activeMenuItem.isOpenMenu
|
|
72
|
+
? {...activeMenuItem, currentEvent}
|
|
73
|
+
: {...activeMenuItem, isOpenMenu: true, currentEvent}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
value: activeElement.type === MenuItem ? activeMenuItem : newValue
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
[this.props.isLeftOpen ? 'ArrowRight' : 'ArrowLeft']: (activeMenuItem, {currentEvent}) => {
|
|
80
|
+
const defaultValue = this.isParentMenu() ? activeMenuItem : {}
|
|
81
|
+
const value = activeMenuItem.isOpenMenu ? {...activeMenuItem, currentEvent} : defaultValue
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
value,
|
|
85
|
+
afterUpdateCallBack: () =>
|
|
86
|
+
!activeMenuItem.isOpenMenu && this.handleMenuByParent('CloseCurrentMenu')
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
// Custom actions
|
|
90
|
+
CloseCurrentMenu: activeMenuItem => ({
|
|
91
|
+
value: {...activeMenuItem, currentEvent: null, isOpenMenu: false}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// eventCode generated by library here
|
|
96
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
97
|
+
return config[eventCode]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* LifeCycle Methods
|
|
103
|
+
*/
|
|
104
|
+
componentDidMount() {
|
|
105
|
+
// Assume that children will be passed in sync manner
|
|
106
|
+
this.menuItemsAvailableForNavigation = this.getMenuItemsState()
|
|
107
|
+
if (this.props.enableKeyboardNavigation) {
|
|
108
|
+
document.addEventListener('keydown', this.handleKeyboard)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
componentWillReceiveProps({navigationState}) {
|
|
113
|
+
if (this.props.navigationState.isOpenMenu !== navigationState.isOpenMenu) {
|
|
114
|
+
this.setInitialMenuState(navigationState.isOpenMenu)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Transfering of control from parent menu to submenu
|
|
118
|
+
// by dispatching `handleKeyboard` manually
|
|
119
|
+
if (
|
|
120
|
+
Boolean(navigationState.currentEvent) &&
|
|
121
|
+
navigationState.isOpenMenu &&
|
|
122
|
+
this.props.navigationState !== navigationState
|
|
123
|
+
) {
|
|
124
|
+
this.handleKeyboard({code: navigationState.currentEvent})
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
componentWillUnmount() {
|
|
129
|
+
if (this.props.enableKeyboardNavigation) {
|
|
130
|
+
document.removeEventListener('keydown', this.handleKeyboard)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Assume that only parent menu have this flag
|
|
135
|
+
isParentMenu = () => this.props.enableKeyboardNavigation
|
|
136
|
+
|
|
137
|
+
isControlledMode = (props = this.props) =>
|
|
138
|
+
typeof props.open !== 'undefined' && props.open !== null
|
|
139
|
+
|
|
140
|
+
handleMenuByParent = eventCode =>
|
|
141
|
+
this.props.navigationState.onChangeMenuState &&
|
|
142
|
+
this.props.navigationState.onChangeMenuState(eventCode)
|
|
143
|
+
|
|
144
|
+
handleKeyboard = event => {
|
|
145
|
+
const {currentMenuState} = this.state
|
|
146
|
+
const indexOfLastChildInRow = this.menuItemsAvailableForNavigation.length - 1
|
|
147
|
+
const getNewState = this.commands(event.code)
|
|
148
|
+
|
|
149
|
+
if (!getNewState || !this.state.open) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const newMenuState = getNewState(currentMenuState, {
|
|
154
|
+
minLength: 0,
|
|
155
|
+
maxLength: indexOfLastChildInRow,
|
|
156
|
+
currentEvent: event.code
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
if (!newMenuState || !newMenuState.value) {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
this.setState({currentMenuState: newMenuState.value}, newMenuState.afterUpdateCallBack)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getMenuItemsState = () =>
|
|
167
|
+
React.Children.toArray(this.props.children).filter(
|
|
168
|
+
({type}) => type !== MenuSeparator && type !== MenuTrigger
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
setOpened = open => {
|
|
172
|
+
if (!this.isControlledMode()) {
|
|
173
|
+
this.setState({open})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (this.isParentMenu()) {
|
|
177
|
+
this.setInitialMenuState(open)
|
|
178
|
+
}
|
|
179
|
+
// Force clean state, when user navigating by mouse
|
|
180
|
+
if (!open) {
|
|
181
|
+
this.handleMenuByParent('CloseCurrentMenu')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (this.props.onSetOpened) {
|
|
185
|
+
this.props.onSetOpened(open)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
setInitialMenuState = open =>
|
|
190
|
+
this.setState(
|
|
191
|
+
open
|
|
192
|
+
? {open, currentMenuState: {...defaultMenuState, isActive: true}}
|
|
193
|
+
: {open, currentMenuState: {}}
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
getChildren = () => {
|
|
197
|
+
if (!this.props.enableKeyboardNavigation) {
|
|
198
|
+
return React.Children.toArray(this.props.children)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let counter = 0
|
|
202
|
+
const {currentMenuState} = this.state
|
|
203
|
+
const {children, isLeftOpen} = this.props
|
|
204
|
+
const navigationState = {
|
|
205
|
+
...currentMenuState,
|
|
206
|
+
onChangeMenuState: code => this.handleKeyboard({code})
|
|
207
|
+
}
|
|
208
|
+
const setAdditionalProps = element =>
|
|
209
|
+
isLeftOpen ? React.cloneElement(element, {isLeftOpen}) : element
|
|
210
|
+
|
|
211
|
+
return React.Children.toArray(children).map(child => {
|
|
212
|
+
let element = child
|
|
213
|
+
|
|
214
|
+
if (child.type !== MenuSeparator && child.type !== MenuTrigger) {
|
|
215
|
+
const isCurrentActiveItem = currentMenuState && currentMenuState.id === counter
|
|
216
|
+
|
|
217
|
+
element = isCurrentActiveItem
|
|
218
|
+
? React.cloneElement(child, {
|
|
219
|
+
navigationState,
|
|
220
|
+
isLeftOpen
|
|
221
|
+
})
|
|
222
|
+
: setAdditionalProps(child)
|
|
223
|
+
|
|
224
|
+
counter += 1
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return element
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
onOuterAction = () => this.props.closeOnOuterAction && this.setOpened(false)
|
|
232
|
+
|
|
233
|
+
prepareTrigger = menuChildren => {
|
|
234
|
+
const {isLeftOpen, navigationState} = this.props
|
|
235
|
+
const isOpen = this.isControlledMode() ? this.props.open : this.state.open
|
|
236
|
+
const menuTrigger = menuChildren.find(element => element.type === MenuTrigger)
|
|
237
|
+
if (!menuTrigger) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
'Trigger not found in Menu children. Did you forget to add <MenuTrigger> to <Menu> ?'
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
const {children} = menuTrigger.props
|
|
243
|
+
|
|
244
|
+
const renderCallbackArgs = {
|
|
245
|
+
isOpen,
|
|
246
|
+
isLeftOpen,
|
|
247
|
+
navigationState,
|
|
248
|
+
key: navigationState.id,
|
|
249
|
+
setOpened: this.setOpened
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const trigger = typeof children === 'function' ? children(renderCallbackArgs) : children
|
|
253
|
+
|
|
254
|
+
const getPopoverPosition =
|
|
255
|
+
typeof this.props.getPopoverPosition === 'undefined'
|
|
256
|
+
? hostRect =>
|
|
257
|
+
isLeftOpen ? {right: hostRect.width, top: 0} : {left: hostRect.width, top: 0}
|
|
258
|
+
: this.props.getPopoverPosition
|
|
259
|
+
|
|
260
|
+
return {trigger, getPopoverPosition}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
render() {
|
|
264
|
+
const children = this.getChildren()
|
|
265
|
+
const isOpen = this.isControlledMode() ? this.props.open : this.state.open
|
|
266
|
+
const menuItems = children.filter(element => element.type !== MenuTrigger)
|
|
267
|
+
const {trigger, getPopoverPosition} = this.prepareTrigger(children)
|
|
268
|
+
const renderBody =
|
|
269
|
+
this.props.renderBody || (items => <Popover.Body noPadding>{items}</Popover.Body>)
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<Popover
|
|
273
|
+
className={styles['menu']}
|
|
274
|
+
isOpen={isOpen}
|
|
275
|
+
onOuterAction={this.onOuterAction}
|
|
276
|
+
body={renderBody(menuItems)}
|
|
277
|
+
inline={this.props.appendInline}
|
|
278
|
+
relativeToParent={this.props.relativeToParent}
|
|
279
|
+
getPopoverPosition={getPopoverPosition}
|
|
280
|
+
>
|
|
281
|
+
{trigger}
|
|
282
|
+
</Popover>
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
Menu.propTypes = {
|
|
288
|
+
open: PropTypes.bool,
|
|
289
|
+
/** Indicates that submenu will be inserted into DOM as siblings to menu trigger */
|
|
290
|
+
appendInline: PropTypes.bool,
|
|
291
|
+
/** Indicates that popover positioning offsets (i.e. `top` and `left` with
|
|
292
|
+
* `position: absolute`) should be calculated relative to parent element,
|
|
293
|
+
* rather than `document.body`. Useful when using `appendInline` and a container
|
|
294
|
+
* with `position: relative`.
|
|
295
|
+
* */
|
|
296
|
+
relativeToParent: PropTypes.bool,
|
|
297
|
+
onSetOpened: PropTypes.func,
|
|
298
|
+
isOpenInitially: PropTypes.bool,
|
|
299
|
+
/**
|
|
300
|
+
* This option may be provided only for parent menu,
|
|
301
|
+
* which in turn will be set for all submenus
|
|
302
|
+
* (This option may override `Trigger` offset option)
|
|
303
|
+
*/
|
|
304
|
+
isLeftOpen: PropTypes.bool,
|
|
305
|
+
closeOnOuterAction: PropTypes.bool,
|
|
306
|
+
/**
|
|
307
|
+
* Naviagtion` state represent set of characterisics
|
|
308
|
+
* that define current state of particular `Menu`, `MenuItem`
|
|
309
|
+
*/
|
|
310
|
+
getPopoverPosition: PropTypes.func,
|
|
311
|
+
/**
|
|
312
|
+
* Render callback that can be used to customize how menu container
|
|
313
|
+
* and children are rendered. Default is wrapping in Menu.Body.
|
|
314
|
+
*/
|
|
315
|
+
renderBody: PropTypes.func,
|
|
316
|
+
navigationState: PropTypes.shape({
|
|
317
|
+
id: PropTypes.number,
|
|
318
|
+
isActive: PropTypes.bool,
|
|
319
|
+
isSubmitted: PropTypes.bool,
|
|
320
|
+
isOpenMenu: PropTypes.bool,
|
|
321
|
+
currentEvent: PropTypes.string,
|
|
322
|
+
onChangeMenuState: PropTypes.func
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
Menu.defaultProps = {
|
|
327
|
+
closeOnOuterAction: true,
|
|
328
|
+
navigationState: {}
|
|
329
|
+
}
|