1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | var _excluded = ["bsPrefix", "className", "align", "alignRight", "rootCloseEvent", "flip", "show", "renderOnMount", "as", "popperConfig"];
|
4 | import classNames from 'classnames';
|
5 | import PropTypes from 'prop-types';
|
6 | import React, { useContext } from 'react';
|
7 | import { useDropdownMenu } from 'react-overlays/DropdownMenu';
|
8 | import useMergedRefs from '@restart/hooks/useMergedRefs';
|
9 | import warning from 'warning';
|
10 | import NavbarContext from './NavbarContext';
|
11 | import { useBootstrapPrefix } from './ThemeProvider';
|
12 | import useWrappedRefWithWarning from './useWrappedRefWithWarning';
|
13 | import usePopperMarginModifiers from './usePopperMarginModifiers';
|
14 | var alignDirection = PropTypes.oneOf(['left', 'right']);
|
15 | export var alignPropType = PropTypes.oneOfType([alignDirection, PropTypes.shape({
|
16 | sm: alignDirection
|
17 | }), PropTypes.shape({
|
18 | md: alignDirection
|
19 | }), PropTypes.shape({
|
20 | lg: alignDirection
|
21 | }), PropTypes.shape({
|
22 | xl: alignDirection
|
23 | })]);
|
24 | var defaultProps = {
|
25 | align: 'left',
|
26 | alignRight: false,
|
27 | flip: true
|
28 | };
|
29 | var DropdownMenu = React.forwardRef(function (_ref, ref) {
|
30 | var bsPrefix = _ref.bsPrefix,
|
31 | className = _ref.className,
|
32 | align = _ref.align,
|
33 | alignRight = _ref.alignRight,
|
34 | rootCloseEvent = _ref.rootCloseEvent,
|
35 | flip = _ref.flip,
|
36 | showProps = _ref.show,
|
37 | renderOnMount = _ref.renderOnMount,
|
38 | _ref$as = _ref.as,
|
39 | Component = _ref$as === void 0 ? 'div' : _ref$as,
|
40 | popperConfig = _ref.popperConfig,
|
41 | props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
42 |
|
43 | var isNavbar = useContext(NavbarContext);
|
44 | var prefix = useBootstrapPrefix(bsPrefix, 'dropdown-menu');
|
45 |
|
46 | var _usePopperMarginModif = usePopperMarginModifiers(),
|
47 | popperRef = _usePopperMarginModif[0],
|
48 | marginModifiers = _usePopperMarginModif[1];
|
49 |
|
50 | var alignClasses = [];
|
51 |
|
52 | if (align) {
|
53 | if (typeof align === 'object') {
|
54 | var keys = Object.keys(align);
|
55 | process.env.NODE_ENV !== "production" ? warning(keys.length === 1, 'There should only be 1 breakpoint when passing an object to `align`') : void 0;
|
56 |
|
57 | if (keys.length) {
|
58 | var brkPoint = keys[0];
|
59 | var direction = align[brkPoint];
|
60 |
|
61 |
|
62 |
|
63 | alignRight = direction === 'left';
|
64 | alignClasses.push(prefix + "-" + brkPoint + "-" + direction);
|
65 | }
|
66 | } else if (align === 'right') {
|
67 | alignRight = true;
|
68 | }
|
69 | }
|
70 |
|
71 | var _useDropdownMenu = useDropdownMenu({
|
72 | flip: flip,
|
73 | rootCloseEvent: rootCloseEvent,
|
74 | show: showProps,
|
75 | alignEnd: alignRight,
|
76 | usePopper: !isNavbar && alignClasses.length === 0,
|
77 | popperConfig: _extends({}, popperConfig, {
|
78 | modifiers: marginModifiers.concat((popperConfig == null ? void 0 : popperConfig.modifiers) || [])
|
79 | })
|
80 | }),
|
81 | menuProps = _useDropdownMenu[0],
|
82 | _useDropdownMenu$ = _useDropdownMenu[1],
|
83 | hasShown = _useDropdownMenu$.hasShown,
|
84 | popper = _useDropdownMenu$.popper,
|
85 | show = _useDropdownMenu$.show,
|
86 | alignEnd = _useDropdownMenu$.alignEnd,
|
87 | toggle = _useDropdownMenu$.toggle;
|
88 |
|
89 | menuProps.ref = useMergedRefs(popperRef, useMergedRefs(useWrappedRefWithWarning(ref, 'DropdownMenu'), menuProps.ref));
|
90 | if (!hasShown && !renderOnMount) return null;
|
91 |
|
92 | if (typeof Component !== 'string') {
|
93 | menuProps.show = show;
|
94 |
|
95 | menuProps.close = function () {
|
96 | return toggle == null ? void 0 : toggle(false);
|
97 | };
|
98 |
|
99 | menuProps.alignRight = alignEnd;
|
100 | }
|
101 |
|
102 | var style = props.style;
|
103 |
|
104 | if (popper != null && popper.placement) {
|
105 |
|
106 |
|
107 | style = _extends({}, props.style, menuProps.style);
|
108 | props['x-placement'] = popper.placement;
|
109 | }
|
110 |
|
111 | return React.createElement(Component, _extends({}, props, menuProps, {
|
112 | style: style,
|
113 | className: classNames.apply(void 0, [className, prefix, show && 'show', alignEnd && prefix + "-right"].concat(alignClasses))
|
114 | }));
|
115 | });
|
116 | DropdownMenu.displayName = 'DropdownMenu';
|
117 | DropdownMenu.defaultProps = defaultProps;
|
118 | export default DropdownMenu; |
\ | No newline at end of file |