UNPKG

4.45 kBJavaScriptView Raw
1import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
3import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
4import _assertThisInitialized from "@babel/runtime-corejs2/helpers/esm/assertThisInitialized";
5import classNames from 'classnames';
6import React from 'react';
7import PropTypes from 'prop-types';
8import all from 'prop-types-extra/lib/all';
9import SafeAnchor from './SafeAnchor';
10import { bsClass, prefix, splitBsPropsAndOmit } from './utils/bootstrapUtils';
11import createChainedFunction from './utils/createChainedFunction';
12var propTypes = {
13 /**
14 * Highlight the menu item as active.
15 */
16 active: PropTypes.bool,
17
18 /**
19 * Disable the menu item, making it unselectable.
20 */
21 disabled: PropTypes.bool,
22
23 /**
24 * Styles the menu item as a horizontal rule, providing visual separation between
25 * groups of menu items.
26 */
27 divider: all(PropTypes.bool, function (_ref) {
28 var divider = _ref.divider,
29 children = _ref.children;
30 return divider && children ? new Error('Children will not be rendered for dividers') : null;
31 }),
32
33 /**
34 * Value passed to the `onSelect` handler, useful for identifying the selected menu item.
35 */
36 eventKey: PropTypes.any,
37
38 /**
39 * Styles the menu item as a header label, useful for describing a group of menu items.
40 */
41 header: PropTypes.bool,
42
43 /**
44 * HTML `href` attribute corresponding to `a.href`.
45 */
46 href: PropTypes.string,
47
48 /**
49 * Callback fired when the menu item is clicked.
50 */
51 onClick: PropTypes.func,
52
53 /**
54 * Callback fired when the menu item is selected.
55 *
56 * ```js
57 * (eventKey: any, event: Object) => any
58 * ```
59 */
60 onSelect: PropTypes.func
61};
62var defaultProps = {
63 divider: false,
64 disabled: false,
65 header: false
66};
67
68var MenuItem =
69/*#__PURE__*/
70function (_React$Component) {
71 _inheritsLoose(MenuItem, _React$Component);
72
73 function MenuItem(props, context) {
74 var _this;
75
76 _this = _React$Component.call(this, props, context) || this;
77 _this.handleClick = _this.handleClick.bind(_assertThisInitialized(_assertThisInitialized(_this)));
78 return _this;
79 }
80
81 var _proto = MenuItem.prototype;
82
83 _proto.handleClick = function handleClick(event) {
84 var _this$props = this.props,
85 href = _this$props.href,
86 disabled = _this$props.disabled,
87 onSelect = _this$props.onSelect,
88 eventKey = _this$props.eventKey;
89
90 if (!href || disabled) {
91 event.preventDefault();
92 }
93
94 if (disabled) {
95 return;
96 }
97
98 if (onSelect) {
99 onSelect(eventKey, event);
100 }
101 };
102
103 _proto.render = function render() {
104 var _this$props2 = this.props,
105 active = _this$props2.active,
106 disabled = _this$props2.disabled,
107 divider = _this$props2.divider,
108 header = _this$props2.header,
109 onClick = _this$props2.onClick,
110 className = _this$props2.className,
111 style = _this$props2.style,
112 props = _objectWithoutPropertiesLoose(_this$props2, ["active", "disabled", "divider", "header", "onClick", "className", "style"]);
113
114 var _splitBsPropsAndOmit = splitBsPropsAndOmit(props, ['eventKey', 'onSelect']),
115 bsProps = _splitBsPropsAndOmit[0],
116 elementProps = _splitBsPropsAndOmit[1];
117
118 if (divider) {
119 // Forcibly blank out the children; separators shouldn't render any.
120 elementProps.children = undefined;
121 return React.createElement("li", _extends({}, elementProps, {
122 role: "separator",
123 className: classNames(className, 'divider'),
124 style: style
125 }));
126 }
127
128 if (header) {
129 return React.createElement("li", _extends({}, elementProps, {
130 role: "heading",
131 className: classNames(className, prefix(bsProps, 'header')),
132 style: style
133 }));
134 }
135
136 return React.createElement("li", {
137 role: "presentation",
138 className: classNames(className, {
139 active: active,
140 disabled: disabled
141 }),
142 style: style
143 }, React.createElement(SafeAnchor, _extends({}, elementProps, {
144 role: "menuitem",
145 tabIndex: "-1",
146 onClick: createChainedFunction(onClick, this.handleClick)
147 })));
148 };
149
150 return MenuItem;
151}(React.Component);
152
153MenuItem.propTypes = propTypes;
154MenuItem.defaultProps = defaultProps;
155export default bsClass('dropdown', MenuItem);
\No newline at end of file