UNPKG

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