forked from reactstrap/reactstrap
/
DropdownMenu.js
99 lines (86 loc) · 2.4 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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Popper } from 'react-popper';
import { DropdownContext } from './DropdownContext';
import { mapToCssModules, tagPropType } from './utils';
const propTypes = {
tag: tagPropType,
children: PropTypes.node.isRequired,
right: PropTypes.bool,
flip: PropTypes.bool,
modifiers: PropTypes.object,
className: PropTypes.string,
cssModule: PropTypes.object,
persist: PropTypes.bool,
positionFixed: PropTypes.bool,
};
const defaultProps = {
tag: 'div',
flip: true,
};
const noFlipModifier = { flip: { enabled: false } };
const directionPositionMap = {
up: 'top',
left: 'left',
right: 'right',
down: 'bottom',
};
class DropdownMenu extends React.Component {
render() {
const { className, cssModule, right, tag, flip, modifiers, persist, positionFixed, ...attrs } = this.props;
const classes = mapToCssModules(classNames(
className,
'dropdown-menu',
{
'dropdown-menu-right': right,
show: this.context.isOpen,
}
), cssModule);
const Tag = tag;
if (persist || (this.context.isOpen && !this.context.inNavbar)) {
const position1 = directionPositionMap[this.context.direction] || 'bottom';
const position2 = right ? 'end' : 'start';
const poperPlacement = `${position1}-${position2}`;
const poperModifiers = !flip ? {
...modifiers,
...noFlipModifier,
} : modifiers;
const poperPositionFixed = !!positionFixed;
return (
<Popper
placement={poperPlacement}
modifiers={poperModifiers}
positionFixed={poperPositionFixed}
>
{({ ref, style, placement }) => (
<Tag
tabIndex="-1"
role="menu"
ref={ref}
style={style}
{...attrs}
aria-hidden={!this.context.isOpen}
className={classes}
x-placement={placement}
/>
)}
</Popper>
);
}
return (
<Tag
tabIndex="-1"
role="menu"
{...attrs}
aria-hidden={!this.context.isOpen}
className={classes}
x-placement={attrs.placement}
/>
);
}
};
DropdownMenu.propTypes = propTypes;
DropdownMenu.defaultProps = defaultProps;
DropdownMenu.contextType = DropdownContext;
export default DropdownMenu;