UNPKG

@txdfe/at

Version:

一个设计体系组件库

311 lines (268 loc) 14 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } var _excluded = ["children", "subMenuContentClassName", "noIcon"]; function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React, { Component, Children, cloneElement } from 'react'; import { findDOMNode } from 'react-dom'; import PropTypes from 'prop-types'; import cx from 'classnames'; import Animate from '../../animate'; import Icon from '../../icon'; import { func, obj } from '../../util'; import Item from './item'; import SelectableItem from './selectable-item'; import PopupItem from './popup-item'; var Expand = Animate.Expand; var bindCtx = func.bindCtx; /** * Menu.SubMenu * @order 1 */ var SubMenu = /*#__PURE__*/function (_Component) { _inherits(SubMenu, _Component); var _super = _createSuper(SubMenu); function SubMenu(props) { var _this; _classCallCheck(this, SubMenu); _this = _super.call(this, props); bindCtx(_assertThisInitialized(_this), ['handleMouseEnter', 'handleMouseLeave', 'handleClick', 'handleOpen', 'afterLeave']); return _this; } _createClass(SubMenu, [{ key: "componentDidMount", value: function componentDidMount() { this.itemNode = findDOMNode(this); } }, { key: "afterLeave", value: function afterLeave() { var _this$props = this.props, focused = _this$props.focused, root = _this$props.root; var focusable = root.props.focusable; if (focusable && focused) { this.itemNode.focus(); } } }, { key: "getOpen", value: function getOpen() { var _this$props2 = this.props, _key = _this$props2._key, root = _this$props2.root; var openKeys = root.state.openKeys; return openKeys.indexOf(_key) > -1; } }, { key: "handleMouseEnter", value: function handleMouseEnter(e) { this.handleOpen(true); this.props.onMouseEnter && this.props.onMouseEnter(e); } }, { key: "handleMouseLeave", value: function handleMouseLeave(e) { this.handleOpen(false); this.props.onMouseLeave && this.props.onMouseLeave(e); } }, { key: "handleClick", value: function handleClick(e) { var _this$props3 = this.props, root = _this$props3.root, selectable = _this$props3.selectable; var selectMode = root.props.selectMode; if (selectMode && selectable) { e.stopPropagation(); } var open = this.getOpen(); this.handleOpen(!open); } }, { key: "handleOpen", value: function handleOpen(open, triggerType, e) { var _this$props4 = this.props, _key = _this$props4._key, root = _this$props4.root; root.handleOpen(_key, open, triggerType, e); } }, { key: "passParentToChildren", value: function passParentToChildren(children) { var _this2 = this; var _this$props5 = this.props, mode = _this$props5.mode, root = _this$props5.root; return Children.map(children, function (child) { return /*#__PURE__*/cloneElement(child, { parent: _this2, parentMode: mode || root.props.mode }); }); } }, { key: "renderInline", value: function renderInline() { var _cx, _cx2, _cx3; var _this$props6 = this.props, _key = _this$props6._key, level = _this$props6.level, root = _this$props6.root, className = _this$props6.className, selectableFromProps = _this$props6.selectable, label = _this$props6.label, children = _this$props6.children, noIcon = _this$props6.noIcon, subMenuContentClassName = _this$props6.subMenuContentClassName, propsTriggerType = _this$props6.triggerType, parentMode = _this$props6.parentMode; var _root$props = root.props, prefix = _root$props.prefix, selectMode = _root$props.selectMode, rootTriggerType = _root$props.triggerType, inlineArrowDirection = _root$props.inlineArrowDirection, iconArrowType = _root$props.iconArrowType, expandAnimation = _root$props.expandAnimation, rtl = _root$props.rtl; var triggerType = propsTriggerType || rootTriggerType; var open = this.getOpen(); var others = obj.pickOthers(Object.keys(SubMenu.propTypes), this.props); var liProps = { className: cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-sub-menu-wrapper"), true), _defineProperty(_cx, className, !!className), _cx)) }; var itemProps = { 'aria-expanded': open, _key: _key, level: level, root: root, type: 'submenu', component: 'div', parentMode: parentMode }; var arrorProps = { type: inlineArrowDirection === 'right' ? "".concat(iconArrowType, "-right-s") : "".concat(iconArrowType, "-down-s"), className: cx((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow"), true), _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow-down"), inlineArrowDirection === 'down'), _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow-right"), inlineArrowDirection === 'right'), _defineProperty(_cx2, "".concat(prefix, "open"), open), _cx2)) }; var selectable = !!selectMode && selectableFromProps; var NewItem = selectable ? SelectableItem : Item; if (triggerType === 'hover') { liProps.onMouseEnter = this.handleMouseEnter; liProps.onMouseLeave = this.handleMouseLeave; } else if (selectable) { arrorProps.onClick = this.handleClick; } else { itemProps.onClick = this.handleClick; } if (open) { itemProps.className = "".concat(prefix, "opened"); } var newSubMenuContentClassName = cx((_cx3 = {}, _defineProperty(_cx3, "".concat(prefix, "menu-sub-menu"), true), _defineProperty(_cx3, subMenuContentClassName, !!subMenuContentClassName), _cx3)); var roleMenu = 'menu', roleItem = 'menuitem'; if ('selectMode' in root.props) { roleMenu = 'listbox'; roleItem = 'listitem'; } var subMenu = open ? /*#__PURE__*/React.createElement("ul", { role: roleMenu, dir: rtl ? 'rtl' : undefined, ref: "subMenu", className: newSubMenuContentClassName }, this.passParentToChildren(children)) : null; return /*#__PURE__*/React.createElement("li", _extends({ role: roleItem }, others, liProps), /*#__PURE__*/React.createElement(NewItem, itemProps, /*#__PURE__*/React.createElement("span", { className: "".concat(prefix, "menu-item-text") }, label), noIcon ? null : /*#__PURE__*/React.createElement(Icon, arrorProps)), expandAnimation ? /*#__PURE__*/React.createElement(Expand, { animationAppear: false, afterLeave: this.afterLeave }, subMenu) : subMenu); } }, { key: "renderPopup", value: function renderPopup() { var _cx4; var _this$props7 = this.props, children = _this$props7.children, subMenuContentClassName = _this$props7.subMenuContentClassName, noIcon = _this$props7.noIcon, others = _objectWithoutProperties(_this$props7, _excluded); var root = this.props.root; var _root$props2 = root.props, prefix = _root$props2.prefix, popupClassName = _root$props2.popupClassName, popupStyle = _root$props2.popupStyle, rtl = _root$props2.rtl; var newClassName = cx((_cx4 = {}, _defineProperty(_cx4, "".concat(prefix, "menu"), true), _defineProperty(_cx4, "".concat(prefix, "ver"), true), _defineProperty(_cx4, popupClassName, !!popupClassName), _defineProperty(_cx4, subMenuContentClassName, !!subMenuContentClassName), _cx4)); others.rtl = rtl; return /*#__PURE__*/React.createElement(PopupItem, _extends({}, others, { noIcon: noIcon, hasSubMenu: true }), /*#__PURE__*/React.createElement("ul", { role: "menu", dir: rtl ? 'rtl' : undefined, className: newClassName, style: popupStyle }, this.passParentToChildren(children))); } }, { key: "render", value: function render() { var _this$props8 = this.props, mode = _this$props8.mode, root = _this$props8.root; var newMode = mode || root.props.mode; return newMode === 'popup' ? this.renderPopup() : this.renderInline(); } }]); return SubMenu; }(Component); _defineProperty(SubMenu, "menuChildType", 'submenu'); _defineProperty(SubMenu, "propTypes", { _key: PropTypes.string, root: PropTypes.object, level: PropTypes.number, groupIndent: PropTypes.number, noIcon: PropTypes.bool, /** * 标签内容 */ label: PropTypes.node, /** * 是否可选,该属性仅在设置 Menu 组件 selectMode 属性后生效 */ selectable: PropTypes.bool, /** * 子菜单打开方式,如果设置会覆盖 Menu 上的同名属性 * @default Menu 的 mode 属性值 */ mode: PropTypes.oneOf(['inline', 'popup']), /** * 菜单项或下一级子菜单 */ children: PropTypes.node, onMouseEnter: PropTypes.func, onMouseLeave: PropTypes.func, subMenuContentClassName: PropTypes.string, triggerType: PropTypes.oneOf(['click', 'hover']), align: PropTypes.oneOf(['outside', 'follow']), parentMode: PropTypes.oneOf(['inline', 'popup']) }); _defineProperty(SubMenu, "defaultProps", { groupIndent: 0, noIcon: false, selectable: false }); export { SubMenu as default };