UNPKG

@txdfe/at

Version:

一个设计体系组件库

267 lines (266 loc) 13 kB
var _excluded = ["children", "subMenuContentClassName", "noIcon"]; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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) { function SubMenu(props) { var _this; _classCallCheck(this, SubMenu); _this = _callSuper(this, SubMenu, [props]); bindCtx(_this, ['handleMouseEnter', 'handleMouseLeave', 'handleClick', 'handleOpen', 'afterLeave']); return _this; } _inherits(SubMenu, _Component); return _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 _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(_defineProperty(_defineProperty({}, "".concat(prefix, "menu-sub-menu-wrapper"), true), className, !!className)) }; 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(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "menu-icon-arrow"), true), "".concat(prefix, "menu-icon-arrow-down"), inlineArrowDirection === 'down'), "".concat(prefix, "menu-icon-arrow-right"), inlineArrowDirection === 'right'), "".concat(prefix, "open"), open)) }; 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(_defineProperty(_defineProperty({}, "".concat(prefix, "menu-sub-menu"), true), subMenuContentClassName, !!subMenuContentClassName)); 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 _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(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "menu"), true), "".concat(prefix, "ver"), true), popupClassName, !!popupClassName), subMenuContentClassName, !!subMenuContentClassName)); 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(); } }]); }(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 };