zarm-web
Version:
基于 React 的桌面端UI库
359 lines (296 loc) • 13.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = SubMenuConsumer;
exports.SubMenu = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames3 = _interopRequireDefault(require("classnames"));
var _dom = _interopRequireDefault(require("../utils/dom"));
var _events = _interopRequireDefault(require("../utils/events"));
var _menuContext = _interopRequireDefault(require("./menu-context"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
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 _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; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
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); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 } }); 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); }
var SubMenu =
/*#__PURE__*/
function (_Component) {
_inherits(SubMenu, _Component);
function SubMenu(props) {
var _this;
_classCallCheck(this, SubMenu);
_this = _possibleConstructorReturn(this, _getPrototypeOf(SubMenu).call(this, props));
_this.subTitle = void 0;
_this.sub = void 0;
_this.timeout = void 0;
_this.toggleSubMenuOpen = function (e) {
e.stopPropagation();
var subMenuKey = _this.props.subMenuKey;
_this.props.toggleOpenKeys(subMenuKey);
};
_this.onSubAnimationEnd = function () {
var _this$props = _this.props,
subMenuKey = _this$props.subMenuKey,
openKeys = _this$props.openKeys;
var isOpen = openKeys.indexOf(subMenuKey) > -1;
_this.setState({
collapsedSubVisible: isOpen
});
};
_this.onClickOutSide = function (e) {
var target = e.target;
var _this$props2 = _this.props,
subMenuKey = _this$props2.subMenuKey,
openKeys = _this$props2.openKeys;
if (_this.subTitle.contains(target)) {
return;
}
if (!_this.sub.contains(target) && openKeys.indexOf(subMenuKey) > -1) {
_this.props.toggleOpenKeys(subMenuKey);
}
};
_this.state = {
collapsedSubVisible: false,
collapsedSubAnimation: ''
};
return _this;
}
_createClass(SubMenu, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props3 = this.props,
openKeys = _this$props3.openKeys,
inlineCollapsed = _this$props3.inlineCollapsed;
if (openKeys.length > 0) {
if (!inlineCollapsed) {
this.setSubHeight({
openKeys: []
});
}
}
if (inlineCollapsed) {
_events.default.on(document, 'click', this.onClickOutSide);
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var inlineCollapsed = nextProps.inlineCollapsed;
if (!inlineCollapsed) {
// eslint-disable-next-line react/destructuring-assignment
if (this.props.inlineCollapsed) {
_events.default.off(document, 'click', this.onClickOutSide);
}
return;
} // eslint-disable-next-line react/destructuring-assignment
if (!this.props.inlineCollapsed) {
_events.default.on(document, 'click', this.onClickOutSide);
}
var _this$props4 = this.props,
subMenuKey = _this$props4.subMenuKey,
openKeys = _this$props4.openKeys;
var isOpenNow = openKeys.indexOf(subMenuKey) > -1;
var isOpenNext = nextProps.openKeys.indexOf(subMenuKey) > -1;
if (!isOpenNow && isOpenNext) {
// 展开菜单
this.slideDown();
} else if (isOpenNow && !isOpenNext) {
// 收起菜单
this.slideUp();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var inlineCollapsed = this.props.inlineCollapsed;
if (!inlineCollapsed) {
this.setSubHeight(prevProps);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var inlineCollapsed = this.props.inlineCollapsed;
if (inlineCollapsed) {
_events.default.off(document, 'click', this.onClickOutSide);
}
}
}, {
key: "getSubHeight",
value: function getSubHeight() {
if (!this.sub) {
return;
}
var childs = _toConsumableArray(this.sub.children);
var marginBottom = 0;
if (childs[0]) {
marginBottom = parseFloat(_dom.default.getStyleComputedProperty(childs[0], 'marginBottom'));
}
var height = childs.reduce(function (res, next) {
res += next.offsetHeight + marginBottom;
return res;
}, marginBottom / 2);
return height;
}
}, {
key: "setSubHeight",
value: function setSubHeight(prevProps) {
var _this2 = this;
if (!this.sub) {
return;
}
var lastOpenKeys = prevProps.openKeys;
var _this$props5 = this.props,
openKeys = _this$props5.openKeys,
subMenuKey = _this$props5.subMenuKey;
var keyIndex = openKeys.indexOf(subMenuKey);
var keysLength = openKeys.length;
if (keyIndex > -1) {
if (keysLength > 1 && keyIndex < keysLength - 1 || keysLength < lastOpenKeys.length) {
// 如果不是最后一级子菜单,或者嵌套的子菜单被收起,当前子菜单高度自适应
this.sub.style.height = 'auto';
} else {
// 否则,设置具体的高度产生过渡动画
var height = this.getSubHeight();
this.sub.style.height = "".concat(height, "px");
}
} else {
var _height = this.getSubHeight();
this.sub.style.height = "".concat(_height, "px");
setTimeout(function () {
_this2.sub.style.height = 0;
}, 0);
}
}
}, {
key: "slideUp",
value: function slideUp() {
this.setState({
collapsedSubVisible: false,
collapsedSubAnimation: 'up'
});
}
}, {
key: "slideDown",
value: function slideDown() {
this.setState({
collapsedSubVisible: true,
collapsedSubAnimation: 'down'
});
}
}, {
key: "renderChildren",
value: function renderChildren() {
var _this$props6 = this.props,
children = _this$props6.children,
level = _this$props6.level,
inlineIndent = _this$props6.inlineIndent,
mode = _this$props6.mode,
prefixCls = _this$props6.prefixCls,
subMenuKey = _this$props6.subMenuKey;
var childProps = {
mode: mode,
level: level + 1,
inlineIndent: inlineIndent,
prefixCls: prefixCls
};
return _react.Children.map(children, function (child, index) {
var c = child;
var _ref = child,
key = _ref.key;
childProps.itemKey = key || "".concat(subMenuKey, "-").concat(level, "-").concat(index);
childProps.subMenuKey = key || "".concat(subMenuKey, "-").concat(level, "-").concat(index);
return (0, _react.cloneElement)(c, childProps);
});
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$props7 = this.props,
title = _this$props7.title,
level = _this$props7.level,
mode = _this$props7.mode,
style = _this$props7.style,
inlineIndent = _this$props7.inlineIndent,
prefixCls = _this$props7.prefixCls,
openKeys = _this$props7.openKeys,
subMenuKey = _this$props7.subMenuKey,
inlineCollapsed = _this$props7.inlineCollapsed;
var _this$state = this.state,
collapsedSubVisible = _this$state.collapsedSubVisible,
collapsedSubAnimation = _this$state.collapsedSubAnimation;
var subMenuStyle = {};
if (mode === 'inline' && !inlineCollapsed) {
subMenuStyle.paddingLeft = level * inlineIndent;
}
var isOpen = openKeys.indexOf(subMenuKey) > -1;
var cls = (0, _classnames3.default)("".concat(prefixCls, "-submenu"), _defineProperty({
open: isOpen
}, "".concat(prefixCls, "-level-").concat(level), level));
var subStyle = {
display: 'block'
};
var subCls = "".concat(prefixCls, "-submenu-sub");
if (inlineCollapsed) {
subStyle = {
display: collapsedSubVisible ? 'block' : 'none'
};
subCls = (0, _classnames3.default)("".concat(prefixCls), "".concat(prefixCls, "-submenu-sub"), _defineProperty({}, "slide-".concat(collapsedSubAnimation), !!collapsedSubAnimation));
}
return _react.default.createElement("li", {
className: cls,
style: style
}, _react.default.createElement("div", {
ref: function ref(subTitle) {
_this3.subTitle = subTitle;
},
onClick: this.toggleSubMenuOpen,
style: subMenuStyle,
className: "".concat(prefixCls, "-submenu-title")
}, title, _react.default.createElement("i", {
className: "".concat(prefixCls, "-submenu-arrow")
})), _react.default.createElement("ul", {
ref: function ref(sub) {
_this3.sub = sub;
},
className: subCls,
style: subStyle,
onAnimationEnd: this.onSubAnimationEnd
}, this.renderChildren()));
}
}]);
return SubMenu;
}(_react.Component);
exports.SubMenu = SubMenu;
SubMenu.defaultProps = {
prefixCls: 'za-menu',
title: '',
level: 1,
style: {},
openKeys: []
};
function SubMenuConsumer(props) {
return _react.default.createElement(_menuContext.default.Consumer, null, function (menuKeys) {
return _react.default.createElement(SubMenu, _extends({}, props, {
inlineCollapsed: menuKeys.inlineCollapsed,
openKeys: menuKeys.openKeys,
toggleOpenKeys: menuKeys.toggleOpenKeys
}));
});
}