-
Notifications
You must be signed in to change notification settings - Fork 98
/
index.js
231 lines (199 loc) · 6.08 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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import IconCheckSolid from 'instructure-icons/lib/Solid/IconCheckSolid'
import IconArrowOpenRightSolid from 'instructure-icons/lib/Solid/IconArrowOpenRightSolid'
import keycode from 'keycode'
import themeable from '@instructure/ui-themeable'
import CustomPropTypes from '@instructure/ui-utils/lib/react/CustomPropTypes'
import { omitProps } from '@instructure/ui-utils/lib/react/passthroughProps'
import getElementType from '@instructure/ui-utils/lib/react/getElementType'
import createChainedFunction from '@instructure/ui-utils/lib/createChainedFunction'
import isActiveElement from '@instructure/ui-utils/lib/dom/isActiveElement'
import findDOMNode from '@instructure/ui-utils/lib/dom/findDOMNode'
import styles from './styles.css'
import theme from './theme'
/**
---
parent: Menu
---
**/
@themeable(theme, styles)
class MenuItem extends Component {
/* eslint-disable react/require-default-props */
static propTypes = {
/* the menu item label */
children: PropTypes.node.isRequired,
/* whether to set the menu item state to selected or not on initial render */
defaultSelected: PropTypes.bool,
/**
* whether the menu item is selected or not (must be accompanied by an `onSelect` prop)
*/
selected: CustomPropTypes.controllable(PropTypes.bool, 'onSelect', 'defaultSelected'),
/**
* when used with the `selected` prop, the component will not control its own state
*/
onSelect: PropTypes.func,
onClick: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
onMouseOver: PropTypes.func,
/**
* the id of the element that the menu item will act upon
*/
controls: PropTypes.string,
disabled: PropTypes.bool,
/**
* the element type to render as (will default to `<a>` if href is provided)
*/
as: CustomPropTypes.elementType,
type: PropTypes.oneOf(['button', 'checkbox', 'radio', 'flyout']),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
active: PropTypes.bool,
href: PropTypes.string
}
/* eslint-enable react/require-default-props */
static defaultProps = {
type: 'button',
disabled: false,
onSelect: function (e, value, selected) {}
}
constructor (props) {
super()
this.state = {
selected: props.selected === undefined ? !!props.defaultSelected : null
}
}
handleClick = e => {
const { onSelect, onClick, disabled, value, href } = this.props
const selected = !this.selected
if (disabled) {
e.preventDefault()
return
}
if (!href) {
e.preventDefault()
}
if (this.props.selected === undefined) {
this.setState({ selected })
}
if (typeof onSelect === 'function') {
onSelect(e, value, selected)
}
if (typeof onClick === 'function') {
onClick(e)
}
}
handleKeyDown = e => {
const spaceKey = e.keyCode === keycode.codes.space
const enterKey = e.keyCode === keycode.codes.enter
if (spaceKey || enterKey) {
e.preventDefault()
e.stopPropagation()
if (enterKey) {
// handle space key on keyUp for FF
findDOMNode(this._node).click() // eslint-disable-line react/no-find-dom-node
}
}
}
handleKeyUp = e => {
const spaceKey = e.keyCode === keycode.codes.space
const enterKey = e.keyCode === keycode.codes.enter
if (spaceKey || enterKey) {
e.preventDefault()
e.stopPropagation()
if (spaceKey) {
findDOMNode(this._node).click() // eslint-disable-line react/no-find-dom-node
}
}
}
handleMouseOver = () => {
this.focus()
}
get elementType () {
return getElementType(MenuItem, this.props)
}
get role () {
switch (this.props.type) {
case 'checkbox':
return 'menuitemcheckbox'
case 'radio':
return 'menuitemradio'
case 'flyout':
return 'button'
default:
return 'menuitem'
}
}
get selected () {
return this.props.selected === undefined ? this.state.selected : this.props.selected
}
get focused () {
return isActiveElement(this._node)
}
focus () {
findDOMNode(this._node).focus() // eslint-disable-line react/no-find-dom-node
}
renderContent () {
const { children, type } = this.props
return (
<span>
{(type === 'checkbox' || type === 'radio') &&
<span className={styles.icon}>
{this.selected && <IconCheckSolid />}
</span>}
<span className={styles.label}>
{children}
</span>
{type === 'flyout' &&
<span className={styles.icon}>
<IconArrowOpenRightSolid />
</span>}
</span>
)
}
render () {
const {
disabled,
controls,
onKeyDown,
onKeyUp,
onMouseOver,
active,
type,
children,
href
} = this.props
const props = omitProps(this.props, MenuItem.propTypes)
const ElementType = this.elementType
const classes = {
[styles.root]: true,
[styles.active]: active,
[styles.flyout]: type === 'flyout'
}
/* eslint-disable jsx-a11y/mouse-events-have-key-events, no-nested-ternary */
return (
<ElementType
tabIndex="-1" // note: tabIndex can be overridden by Menu or MenuItemGroup components
{...props}
href={href}
role={this.role}
aria-disabled={disabled ? 'true' : null}
aria-controls={controls}
aria-checked={type === 'checkbox' || type === 'radio' ? (this.selected ? 'true' : 'false') : null}
onClick={this.handleClick}
onKeyUp={createChainedFunction(onKeyUp, this.handleKeyUp)}
onKeyDown={createChainedFunction(onKeyDown, this.handleKeyDown)}
ref={c => {
this._node = c
}}
className={classnames(classes)}
onMouseOver={createChainedFunction(onMouseOver, this.handleMouseOver)}
>
{this.renderContent()}
</ElementType>
)
/* eslint-enable jsx-a11y/mouse-events-have-key-events, no-nested-ternary */
}
}
export default MenuItem