forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdownMenu.js
More file actions
138 lines (118 loc) · 3.31 KB
/
Copy pathDropdownMenu.js
File metadata and controls
138 lines (118 loc) · 3.31 KB
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
import classNames from 'classnames';
import keycode from 'keycode';
import React from 'react';
import ReactDOM from 'react-dom';
import RootCloseWrapper from 'react-overlays/lib/RootCloseWrapper';
import { bsClass, getClassSet, prefix, splitBsProps }
from './utils/bootstrapUtils';
import createChainedFunction from './utils/createChainedFunction';
import ValidComponentChildren from './utils/ValidComponentChildren';
const propTypes = {
open: React.PropTypes.bool,
pullRight: React.PropTypes.bool,
onClose: React.PropTypes.func,
labelledBy: React.PropTypes.oneOfType([
React.PropTypes.string, React.PropTypes.number,
]),
onSelect: React.PropTypes.func,
rootCloseEvent: React.PropTypes.oneOf(['click', 'mousedown']),
};
const defaultProps = {
bsRole: 'menu',
pullRight: false,
};
class DropdownMenu extends React.Component {
constructor(props) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
handleKeyDown(event) {
switch (event.keyCode) {
case keycode.codes.down:
this.focusNext();
event.preventDefault();
break;
case keycode.codes.up:
this.focusPrevious();
event.preventDefault();
break;
case keycode.codes.esc:
case keycode.codes.tab:
this.props.onClose(event);
break;
default:
}
}
getItemsAndActiveIndex() {
const items = this.getFocusableMenuItems();
const activeIndex = items.indexOf(document.activeElement);
return { items, activeIndex };
}
getFocusableMenuItems() {
const node = ReactDOM.findDOMNode(this);
if (!node) {
return [];
}
return Array.from(node.querySelectorAll('[tabIndex="-1"]'));
}
focusNext() {
const { items, activeIndex } = this.getItemsAndActiveIndex();
if (items.length === 0) {
return;
}
const nextIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
items[nextIndex].focus();
}
focusPrevious() {
const { items, activeIndex } = this.getItemsAndActiveIndex();
if (items.length === 0) {
return;
}
const prevIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
items[prevIndex].focus();
}
render() {
const {
open,
pullRight,
onClose,
labelledBy,
onSelect,
className,
rootCloseEvent,
children,
...props
} = this.props;
const [bsProps, elementProps] = splitBsProps(props);
const classes = {
...getClassSet(bsProps),
[prefix(bsProps, 'right')]: pullRight,
};
return (
<RootCloseWrapper
disabled={!open}
onRootClose={onClose}
event={rootCloseEvent}
>
<ul
{...elementProps}
role="menu"
className={classNames(className, classes)}
aria-labelledby={labelledBy}
>
{ValidComponentChildren.map(children, child => (
React.cloneElement(child, {
onKeyDown: createChainedFunction(
child.props.onKeyDown, this.handleKeyDown
),
onSelect: createChainedFunction(child.props.onSelect, onSelect),
})
))}
</ul>
</RootCloseWrapper>
);
}
}
DropdownMenu.propTypes = propTypes;
DropdownMenu.defaultProps = defaultProps;
export default bsClass('dropdown-menu', DropdownMenu);