-
Notifications
You must be signed in to change notification settings - Fork 98
/
index.js
320 lines (279 loc) · 8.31 KB
/
index.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { Children, Component } from 'react'
import PropTypes from 'prop-types'
import keycode from 'keycode'
import themeable from '@instructure/ui-themeable'
import CustomPropTypes from '@instructure/ui-utils/lib/react/CustomPropTypes'
import createChainedFunction from '@instructure/ui-utils/lib/createChainedFunction'
import safeCloneElement from '@instructure/ui-utils/lib/react/safeCloneElement'
import { omitProps } from '@instructure/ui-utils/lib/react/passthroughProps'
import matchComponentTypes from '@instructure/ui-utils/lib/react/matchComponentTypes'
import containsActiveElement from '@instructure/ui-utils/lib/dom/containsActiveElement'
import MenuItem from '../MenuItem'
import MenuItemFlyout from '../MenuItemFlyout'
import MenuItemGroup from '../MenuItemGroup'
import MenuItemSeparator from '../MenuItemSeparator'
import styles from './styles.css'
import theme from './theme'
/**
---
parent: Menu
---
**/
@themeable(theme, styles)
export default class MenuList extends Component {
static propTypes = {
/**
* children of type `MenuItem`
*/
children: CustomPropTypes.Children.oneOf([
MenuItem, MenuItemSeparator, MenuItemGroup, MenuItemFlyout
]),
/**
* the id of the element that contains the text description of the menu
*/
labelledBy: PropTypes.string,
/**
* the id of the element that the menu items will act upon
*/
controls: PropTypes.string,
/**
* a description of the menu
*/
title: PropTypes.string,
disabled: PropTypes.bool,
/**
* call this function when a menu item is selected (note: value argument will default to the index)
*/
onSelect: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
/**
* prop specifying if the `<MenuList />` is shown
*/
show: PropTypes.bool,
/**
* Callback fired when a `<MenuItemFlyout />` in the `<MenuList />` is dismissed
*/
onFlyoutDismiss: PropTypes.func
}
static defaultProps = {
show: true,
disabled: false,
onKeyDown: function (e) {},
onKeyUp: function (e) {},
onFocus: function (e) {},
onBlur: function (e) {},
onSelect: function (e, value, selected) {},
onFlyoutDismiss: function (e, documentClick) {}
}
constructor (props) {
super()
this.state = {
hasFocus: false
}
}
_menuitems = []
_node = null
_activeMenuItemFlyout = null
componentWillUnmount () {
delete this._activeMenuItemFlyout
}
handleKeyDown = (event) => {
const key = event.keyCode
const {
down,
up,
pgup,
pgdn
} = keycode.codes
if (key === down || key === pgdn) {
this.moveFocus(1)
event.preventDefault()
event.stopPropagation()
this.hideActiveMenuItemFlyout()
} else if (key === up || key === pgup) {
this.moveFocus(-1)
event.preventDefault()
event.stopPropagation()
this.hideActiveMenuItemFlyout()
}
}
handleItemFocus = () => this.setState({ hasFocus: true })
handleItemBlur = () => this.setState({ hasFocus: this.focusedIndex >= 0 })
focus () {
this._node && this._node.focus()
}
focused () {
return containsActiveElement(this._node) || this.state.hasFocus
}
get focusedIndex () {
return this._menuitems.findIndex((item) => (item.focused === true))
}
moveFocus (step) {
const items = this._menuitems
const count = items ? items.length : 0
if (count <= 0) {
return
}
const current = (this.focusedIndex < 0 && step < 0) ? 0 : this.focusedIndex
const next = (current + count + step) % count
if (items[next] && items[next].focus) {
items[next].focus()
}
}
handleMenuFocus = () => {
// when there is only one menu item, focus that item so SR users
// don't have to drill down for access
if (this._menuitems.length === 1) {
this._menuitems[0].focus()
}
}
handleMenuItemMouseOver = (mouseOverItem) => {
if (this._activeMenuItemFlyout && mouseOverItem !== this._activeMenuItemFlyout._trigger) {
this.hideActiveMenuItemFlyout()
}
}
handleFlyoutToggle = (isShown, menuItemFlyout) => {
if (isShown) {
this._activeMenuItemFlyout = menuItemFlyout
}
}
hideActiveMenuItemFlyout = (e) => {
if (this._activeMenuItemFlyout) {
this._activeMenuItemFlyout.hide(e)
this._activeMenuItemFlyout = null
}
}
handleMenuRef = (node) => {
this._node = node
}
renderChildren () {
const {
children,
disabled,
controls,
labelledBy,
title,
onFlyoutDismiss
} = this.props
this._menuitems = []
let count = 0
const items = Children.map(children, (child, index) => {
if (!matchComponentTypes(child, [MenuItem, MenuItemGroup, MenuItemFlyout])) {
return child
}
count += 1
const ref = (node) => {
if (node) {
this._menuitems.push(node)
}
}
const props = {
controls: controls,
...child.props.controls, // child 'controls' prop should override parent
disabled: (disabled || child.props.disabled),
onFocus: createChainedFunction(this.handleItemFocus, this.props.onFocus),
onBlur: createChainedFunction(this.handleItemBlur, this.props.onBlur),
onSelect: this.props.onSelect,
onMouseOver: this.handleMenuItemMouseOver
}
const isTabbable = !this.state.hasFocus && count === 1
if (matchComponentTypes(child, [MenuItem])) {
return (
<li>
{safeCloneElement(child, {
...props,
tabIndex: isTabbable ? 0 : -1,
ref,
key: index
})}
</li>
)
}
if (matchComponentTypes(child, [MenuItemFlyout])) {
return (
<li>
{safeCloneElement(child, {
...props,
tabIndex: isTabbable ? 0 : -1,
onToggle: this.handleFlyoutToggle,
onDismiss: onFlyoutDismiss,
labelledBy,
title,
ref,
key: index
})}
</li>
)
}
if (matchComponentTypes(child, [MenuItemGroup])) {
return (
<li>
{safeCloneElement(child, {
...props,
itemRef: ref,
isTabbable,
key: index
})}
</li>
)
}
})
return items
}
render () {
const {
labelledBy,
disabled,
controls,
title,
onKeyDown,
show
} = this.props
const props = omitProps(this.props, MenuList.propTypes)
return (
<ul
{...props}
role="menu"
tabIndex={this.state.hasFocus ? null : '0'}
ref={this.handleMenuRef}
className={styles.root}
aria-hidden={!show}
aria-labelledby={labelledBy}
aria-controls={controls}
aria-expanded={show}
aria-disabled={disabled ? 'true' : null}
title={title}
onKeyDown={createChainedFunction(onKeyDown, this.handleKeyDown)}
onFocus={this.handleMenuFocus}
>
{this.renderChildren()}
</ul>
)
}
}