forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdownMenu.js
120 lines (102 loc) · 3.04 KB
/
DropdownMenu.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
import React, { cloneElement } from 'react';
import RootCloseWrapper from 'react-overlays/lib/RootCloseWrapper';
import PropTypes from 'prop-types';
import separateComponentProps from '../../util/separateComponentProps';
import MenuItem from '../MenuItem';
import css from './DropdownMenu.css';
const propTypes = {
bsClass: PropTypes.string,
bsRole: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
labelledBy: PropTypes.string,
minWidth: PropTypes.string,
onClose: PropTypes.func,
onSelect: PropTypes.func,
onSelectItem: PropTypes.func,
onToggle: PropTypes.func,
open: PropTypes.bool,
overrideStyle: PropTypes.object,
pullRight: PropTypes.bool,
rootCloseEvent: PropTypes.string,
width: PropTypes.string,
};
const defaultProps = {
overrideStyle: {},
};
class DropdownMenu extends React.Component {
getItemsAndActiveIndex() {
const items = this.getFocusableMenuItems();
const activeIndex = items.indexOf(document.activeElement);
return { items, activeIndex };
}
getFocusableMenuItems() {
if (!this.node) {
return [];
}
return Array.from(this.node.querySelectorAll('button, input, a, select, [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();
}
focusPrev() {
const { items, activeIndex } = this.getItemsAndActiveIndex();
if (items.length === 0) {
return;
}
const prevIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
items[prevIndex].focus();
}
renderChildren() {
const { children, onSelectItem, onSelect } = this.props;
return React.Children.map(React.Children.toArray(children), (child) => {
if (child.type === MenuItem) {
return cloneElement(child,
Object.assign(
{},
child.props,
{
onSelectItem,
onSelect,
},
),
child.props.children);
}
return child;
});
}
render() {
const { onToggle, pullRight } = this.props;
const [, ddprops] = separateComponentProps(this.props, DropdownMenu.propTypes);
const position = Object.assign({
left: pullRight ? 'initial' : '0',
display: this.props.open ? 'block' : 'none',
right: pullRight ? '0' : 'initial',
width: this.props.width || 'initial',
minWidth: this.props.minWidth || null,
}, this.props.overrideStyle);
const menu = (
<div className={css.DropdownMenu} style={position} {...ddprops} ref={(node) => { this.node = node; }}>
{this.renderChildren()}
</div>
);
if (this.props.open) {
return (
<RootCloseWrapper onRootClose={onToggle} >
{menu}
</RootCloseWrapper>
);
}
return menu;
}
}
DropdownMenu.propTypes = propTypes;
DropdownMenu.defaultProps = defaultProps;
export default DropdownMenu;