-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathMenuItem.jsx
211 lines (190 loc) · 5.19 KB
/
MenuItem.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import KeyCode from 'rc-util/lib/KeyCode';
import classNames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';
import { connect } from 'mini-store';
import { noop, menuAllProps } from './util';
/* eslint react/no-is-mounted:0 */
export class MenuItem extends React.Component {
static propTypes = {
attribute: PropTypes.object,
rootPrefixCls: PropTypes.string,
eventKey: PropTypes.string,
active: PropTypes.bool,
children: PropTypes.any,
selectedKeys: PropTypes.array,
disabled: PropTypes.bool,
title: PropTypes.string,
onItemHover: PropTypes.func,
onSelect: PropTypes.func,
onClick: PropTypes.func,
onDeselect: PropTypes.func,
parentMenu: PropTypes.object,
onDestroy: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
multiple: PropTypes.bool,
isSelected: PropTypes.bool,
manualRef: PropTypes.func,
itemIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
};
static defaultProps = {
onSelect: noop,
onMouseEnter: noop,
onMouseLeave: noop,
manualRef: noop,
};
constructor(props) {
super(props);
}
componentDidMount() {
// invoke customized ref to expose component to mixin
this.callRef();
}
componentDidUpdate() {
if (this.props.active) {
scrollIntoView(ReactDOM.findDOMNode(this), ReactDOM.findDOMNode(this.props.parentMenu), {
onlyScrollIfNeeded: true,
});
}
this.callRef();
}
componentWillUnmount() {
const props = this.props;
if (props.onDestroy) {
props.onDestroy(props.eventKey);
}
}
onKeyDown = (e) => {
const keyCode = e.keyCode;
if (keyCode === KeyCode.ENTER) {
this.onClick(e);
return true;
}
};
onMouseLeave = (e) => {
const { eventKey, onItemHover, onMouseLeave } = this.props;
onItemHover({
key: eventKey,
hover: false,
});
onMouseLeave({
key: eventKey,
domEvent: e,
});
};
onMouseEnter = (e) => {
const { eventKey, onItemHover, onMouseEnter } = this.props;
onItemHover({
key: eventKey,
hover: true,
});
onMouseEnter({
key: eventKey,
domEvent: e,
});
};
onClick = (e) => {
const { eventKey, multiple, onClick, onSelect, onDeselect, isSelected } = this.props;
const info = {
key: eventKey,
keyPath: [eventKey],
item: this,
domEvent: e,
};
onClick(info);
if (multiple) {
if (isSelected) {
onDeselect(info);
} else {
onSelect(info);
}
} else if (!isSelected) {
onSelect(info);
}
};
getPrefixCls() {
return `${this.props.rootPrefixCls}-item`;
}
getActiveClassName() {
return `${this.getPrefixCls()}-active`;
}
getSelectedClassName() {
return `${this.getPrefixCls()}-selected`;
}
getDisabledClassName() {
return `${this.getPrefixCls()}-disabled`;
}
callRef() {
if (this.props.manualRef) {
this.props.manualRef(this);
}
}
render() {
const props = { ...this.props };
const className = classNames(this.getPrefixCls(), props.className, {
[this.getActiveClassName()]: !props.disabled && props.active,
[this.getSelectedClassName()]: props.isSelected,
[this.getDisabledClassName()]: props.disabled,
});
let attrs = {
...props.attribute,
title: props.title,
className,
// set to menuitem by default
role: props.role || 'menuitem',
'aria-disabled': props.disabled,
};
if (props.role === 'option') {
// overwrite to option
attrs = {
...attrs,
role: 'option',
'aria-selected': props.isSelected,
};
} else if (props.role === null || props.role === 'none') {
// sometimes we want to specify role inside <li/> element
// <li><a role='menuitem'>Link</a></li> would be a good example
// in this case the role on <li/> should be "none" to
// remove the implied listitem role.
// https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html
attrs.role = 'none';
}
// In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
const mouseEvent = {
onClick: props.disabled ? null : this.onClick,
onMouseLeave: this.onMouseLeave,
onMouseEnter: this.onMouseEnter,
};
const style = {
...props.style,
};
if (props.mode === 'inline') {
style.paddingLeft = props.inlineIndent * props.level;
}
menuAllProps.forEach(key => delete props[key]);
let icon = this.props.itemIcon;
if (typeof this.props.itemIcon === 'function') {
icon = React.createElement(this.props.itemIcon, this.props);
}
return (
<li
{...props}
{...attrs}
{...mouseEvent}
style={style}
>
{props.children}
{icon}
</li>
);
}
}
MenuItem.isMenuItem = true;
const connected = connect(({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
active: activeKey[subMenuKey] === eventKey,
isSelected: selectedKeys.indexOf(eventKey) !== -1,
}))(MenuItem);
export default connected;