@momentum-ui/react-collaboration
Version:
Cisco Momentum UI Framework for React Collaboration Applications
147 lines • 7.37 kB
JavaScript
/** @component menu-item */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import omit from 'lodash/omit';
import mapContextToProps from '@restart/context/mapContextToProps';
import { UIDConsumer } from 'react-uid';
import { ListItem } from '@momentum-ui/react-collaboration';
import ListContext from '../ListContext';
import SelectableContext from '../SelectableContext';
import { prefix } from '../utils/index';
/**
* @deprecated - Components in the legacy folder (/src/legacy) are deprecated. Please use a component from the components folder (/src/components) instead. Legacy components may not follow accessibility standards.
**/
var MenuItem = /** @class */ (function (_super) {
__extends(MenuItem, _super);
function MenuItem(props) {
var _this = _super.call(this, props) || this;
_this.handleClick = function (e, opts) {
var _a = _this.props, onClick = _a.onClick, parentOnSelect = _a.parentOnSelect;
var eventKey = opts.eventKey, label = opts.label, value = opts.value;
onClick && onClick(e, { value: value, label: label });
parentOnSelect && parentOnSelect(e, { eventKey: eventKey, element: _this });
};
_this.handleKeyDown = function (e, opts) {
var _a, _b;
var _c = _this.props, onClick = _c.onClick, parentKeyDown = _c.parentKeyDown, parentOnSelect = _c.parentOnSelect;
var eventKey = opts.eventKey;
if (!((_b = (_a = e.target) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.contains("".concat(prefix, "-list-item")))) {
return;
}
if (e.which === 32 || e.which === 13 || e.charCode === 32 || e.charCode === 13) {
onClick && onClick(e);
parentOnSelect && parentOnSelect(e, { eventKey: eventKey, element: _this });
e.preventDefault();
}
else {
parentKeyDown && parentKeyDown(e, { eventKey: eventKey, element: _this });
}
};
_this.state = {
selectContext: {
parentKeyDown: !props.isHeader ? _this.handleKeyDown : null,
parentOnSelect: !props.isHeader ? _this.handleClick : null,
},
};
return _this;
}
MenuItem.prototype.render = function () {
var _this = this;
var _a = this.props, children = _a.children, className = _a.className, isHeader = _a.isHeader, isOpen = _a.isOpen, itemClassName = _a.itemClassName, props = __rest(_a, ["children", "className", "isHeader", "isOpen", "itemClassName"]);
var selectContext = this.state.selectContext;
var otherProps = omit(__assign({}, props), [
'keepMenuOpen',
'onClick',
'parentKeyDown',
'parentOnSelect',
]);
return (React.createElement(UIDConsumer, { name: function (id) { return "".concat(prefix, "-menu-item-").concat(id); } }, function (id) { return (React.createElement(SelectableContext.Provider, { value: selectContext },
React.createElement(ListContext.Consumer, null, function (listContext) {
var cxtActive = isOpen ||
(listContext &&
listContext.active &&
ReactDOM.findDOMNode(_this.anchorRef) &&
ReactDOM.findDOMNode(_this.anchorRef).attributes["data-".concat(prefix, "-event-key")] &&
ReactDOM.findDOMNode(_this.anchorRef).attributes["data-".concat(prefix, "-event-key")]
.value &&
listContext.active.includes(ReactDOM.findDOMNode(_this.anchorRef).attributes["data-".concat(prefix, "-event-key")]
.value));
return (React.createElement("div", { className: "".concat(prefix, "-menu-item") + "".concat((className && " ".concat(className)) || ''), "aria-expanded": cxtActive, "aria-haspopup": !!children },
React.createElement(ListItem, __assign({ active: cxtActive, className: "".concat((isHeader && "md-menu-item__header") || '') +
"".concat((itemClassName && " ".concat(itemClassName)) || ''), focusOnLoad: true, isReadOnly: isHeader, id: id, ref: function (ref) { return (_this.anchorRef = ref); }, role: "menuitem" }, otherProps), children)));
}))); }));
};
return MenuItem;
}(React.Component));
MenuItem.propTypes = {
/** @prop Children nodes to render inside MenuItem | null */
children: PropTypes.node,
/** @prop Optional css class name | '' */
className: PropTypes.string,
/** @prop Determines if MenuItem is the header | false */
isHeader: PropTypes.bool,
/** @prop Determines if MenuItem is open | false */
isOpen: PropTypes.bool,
/** @prop Optional list css class name | '' */
itemClassName: PropTypes.string,
/** @prop Set to keep the MenuItem open | false */
keepMenuOpen: PropTypes.bool,
/** @prop Callback function invoked when user taps on MenutItem | null */
onClick: PropTypes.func,
// Internal Context Use Only
parentKeyDown: PropTypes.func,
// Internal Context Use Only
parentOnSelect: PropTypes.func,
};
MenuItem.defaultProps = {
children: null,
className: '',
isHeader: false,
isOpen: false,
itemClassName: '',
keepMenuOpen: false,
onClick: null,
parentKeyDown: null,
parentOnSelect: null,
};
MenuItem.displayName = 'MenuItem';
export default mapContextToProps(SelectableContext, function (context) { return context; }, MenuItem);
//# sourceMappingURL=index.js.map