-
Notifications
You must be signed in to change notification settings - Fork 12
/
MenuItem.js
219 lines (186 loc) · 5.77 KB
/
MenuItem.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
/* @flow */
import * as React from 'react';
import type {Ref as ReactRef, Node as ReactNode} from 'react';
import PropTypes from 'prop-types';
import type MenuEvent from './events/MenuEvent';
import ChosenEvent from './events/ChosenEvent';
import {MenuListContext} from './MenuList';
import type {MenuListContextValue, MenuListHandle} from './MenuList';
import setRef from './lib/setRef';
import type {Direction, Rect} from './types';
type State = {
highlighted: boolean,
};
export type Props = {
onItemChosen?: (event: ChosenEvent) => void,
onLeftPushed?: (event: MenuEvent) => void,
onRightPushed?: (event: MenuEvent) => void,
onHighlightChange?: (
highlighted: boolean,
details: {
byKeyboard?: boolean,
prevCursorLocation?: Rect,
direction?: Direction,
}
) => void,
className?: string,
style?: Object,
highlightedClassName?: string,
highlightedStyle?: Object,
noMouseHighlight?: boolean,
index?: number,
onMouseLeave?: (event: MouseEvent) => void,
children?: ReactNode,
domRef?: ReactRef<'div'>,
'aria-haspopup'?: boolean,
'aria-expanded'?: boolean,
};
export default class MenuItem extends React.Component<Props, State> {
_menuListHandle: MenuListHandle;
state = {
highlighted: false,
};
static propTypes = {
onItemChosen: PropTypes.func,
onHighlightChange: PropTypes.func,
onLeftPushed: PropTypes.func,
onRightPushed: PropTypes.func,
className: PropTypes.string,
style: PropTypes.object,
highlightedClassName: PropTypes.string,
highlightedStyle: PropTypes.object,
noMouseHighlight: PropTypes.bool,
index: PropTypes.number,
onMouseLeave: PropTypes.func,
children: PropTypes.node,
domRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
'aria-haspopup': PropTypes.bool,
'aria-expanded': PropTypes.bool,
};
static contextType = MenuListContext;
_el: ?HTMLDivElement;
_elSetter = (el: ?HTMLDivElement) => {
this._el = el;
if (this.props.domRef) {
setRef(this.props.domRef, el);
}
};
hasHighlight(): boolean {
return this.state.highlighted;
}
takeKeyboard() {
this._menuListHandle.takeKeyboard();
}
releaseKeyboard() {
this._menuListHandle.releaseKeyboard();
}
lockHighlight() {
this._menuListHandle.lockHighlight();
}
unlockHighlight() {
this._menuListHandle.unlockHighlight();
}
// byKeyboard forces focus immediately and scrolls the item into view.
// With it false, the highlight might be delayed depending on mouse movement
// and won't cause anything to scroll.
highlight(byKeyboard: boolean = true) {
if (!byKeyboard && this.props.noMouseHighlight) {
return;
}
this._menuListHandle.highlight(byKeyboard);
}
unhighlight(byKeyboard: boolean = true) {
if (!byKeyboard && this.props.noMouseHighlight) {
return;
}
this._menuListHandle.unhighlight();
}
moveCursor(direction: Direction, prevCursorLocation: ?Rect) {
this._menuListHandle.moveCursor(direction, prevCursorLocation);
}
componentDidMount() {
const el = this._el;
/*:: if (!el) throw new Error(); */
this._menuListHandle = (this.context: MenuListContextValue).registerItem(
this.props,
{
notifyHighlighted: (
highlighted: boolean,
byKeyboard: ?boolean,
direction: ?Direction,
prevCursorLocation: ?Rect
) => {
this.setState({highlighted}, () => {
if (highlighted && byKeyboard) {
const el = this._el;
/*:: if (!el) throw new Error(); */
if (typeof (el: any).scrollIntoViewIfNeeded === 'function') {
(el: any).scrollIntoViewIfNeeded();
} else if (el.scrollIntoView) {
el.scrollIntoView();
}
}
});
if (this.props.onHighlightChange) {
this.props.onHighlightChange(highlighted, {
byKeyboard: byKeyboard == null ? undefined : byKeyboard,
prevCursorLocation:
prevCursorLocation == null ? undefined : prevCursorLocation,
direction: direction == null ? undefined : direction,
});
}
},
notifyEvent: (event: MenuEvent) => {
switch (event.type) {
case 'chosen':
/*:: if (!(event instanceof ChosenEvent)) throw new Error(); */
if (this.props.onItemChosen) this.props.onItemChosen(event);
break;
case 'left':
if (this.props.onLeftPushed) this.props.onLeftPushed(event);
break;
case 'right':
if (this.props.onRightPushed) this.props.onRightPushed(event);
break;
}
},
},
el
);
}
componentWillUnmount() {
this._menuListHandle.unregister();
}
componentDidUpdate() {
this._menuListHandle.updateProps(this.props);
}
render() {
const {children, onMouseLeave} = this.props;
const {highlighted} = this.state;
let style = this.props.style;
let className = this.props.className;
if (highlighted) {
if (this.props.highlightedStyle) {
style = {...style, ...this.props.highlightedStyle};
}
if (this.props.highlightedClassName) {
className = `${className || ''} ${this.props.highlightedClassName}`;
}
}
return (
<div
ref={this._elSetter}
style={style}
className={className}
onClick={() => this._menuListHandle.itemChosen()}
onMouseEnter={() => this.highlight(false)}
onMouseLeave={onMouseLeave || (() => this.unhighlight(false))}
role="menuitem"
aria-haspopup={this.props['aria-haspopup']}
aria-expanded={this.props['aria-expanded']}
>
{children}
</div>
);
}
}