UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

361 lines (316 loc) 11.2 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import arrayTreeFilter from 'array-tree-filter'; import { findDOMNode } from 'react-dom'; import Locale from './locale/en_US'; import isFunction from 'lodash/isFunction'; import Icon from '../../icon'; var Menus = /*#__PURE__*/ function (_Component) { _inherits(Menus, _Component); var _super = _createSuper(Menus); function Menus(props) { var _this; _classCallCheck(this, Menus); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "saveMenuItem", function (index) { return function (node) { _this.menuItems[index] = node; }; }); _this.menuItems = {}; return _this; } _createClass(Menus, [{ key: "componentDidMount", value: function componentDidMount() { this.scrollActiveItemToView(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (!prevProps.visible && this.props.visible) { this.scrollActiveItemToView(); } } /** * render th li list * @param {*} option * @param {*} menuIndex */ }, { key: "getOption", value: function getOption(option, menuIndex) { var _this$props = this.props, prefixCls = _this$props.prefixCls, expandTrigger = _this$props.expandTrigger, singleMenuItemStyle = _this$props.singleMenuItemStyle, selectedValues = _this$props.selectedValues; var onSelect = this.props.onSelect.bind(this, option, menuIndex, false); var expandProps = { onClick: function onClick() { return onSelect('click'); } }; var hasChildren = option.children && option.children.length > 0; var menuItemCls = "".concat(prefixCls, "-menu-item"); // TODO: add item style if (expandTrigger === 'hover' && hasChildren) { expandProps = { onMouseEnter: this.delayOnSelect.bind(this, onSelect), onMouseLeave: this.delayOnSelect.bind(this), onClick: function onClick() { return onSelect('click'); } }; } if (selectedValues.findIndex(function (item) { return item === option.value; }) > -1) { menuItemCls += " ".concat(prefixCls, "-menu-item-selected"); } if (this.isActiveOption(option, menuIndex)) { menuItemCls += " ".concat(prefixCls, "-menu-item-active"); expandProps.ref = this.saveMenuItem(menuIndex); } if (option.disabled) { menuItemCls += " ".concat(prefixCls, "-menu-item-disabled"); } if (option.loading) { menuItemCls += " ".concat(prefixCls, "-menu-item-loading"); } var title = ''; if (option.title) { title = option.title; } else if (typeof option.label === 'string') { title = option.label; } return React.createElement("li", _extends({ key: option.key || option.value, className: menuItemCls, title: title, style: singleMenuItemStyle }, expandProps), option.label); } /** * be active value is a array of items * @param string[] values */ }, { key: "getActiveOptions", value: function getActiveOptions(values) { var activeValue = values || this.props.activeValue; var options = this.props.options; return arrayTreeFilter(options, function (o, level) { return o.value === activeValue[level]; }); } }, { key: "getShowOptions", value: function getShowOptions() { var options = this.props.options; var result = this.getActiveOptions().map(function (activeOption) { return activeOption.children; }).filter(function (activeOption) { return !!activeOption; }); result.unshift(options); return result; } }, { key: "delayOnSelect", value: function delayOnSelect(onSelect) { var _this2 = this; for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (this.delayTimer) { clearTimeout(this.delayTimer); this.delayTimer = null; } if (typeof onSelect === 'function') { this.delayTimer = setTimeout(function () { onSelect('hover', args); _this2.delayTimer = null; }, 150); } } }, { key: "scrollActiveItemToView", value: function scrollActiveItemToView() { // scroll into view var optionsLength = this.getShowOptions().length; for (var i = 0; i < optionsLength; i++) { var itemComponent = this.menuItems[i]; if (itemComponent) { var target = findDOMNode(itemComponent); target.parentNode.scrollTop = target.offsetTop; } } } }, { key: "isActiveOption", value: function isActiveOption(option, menuIndex) { var _this$props$activeVal = this.props.activeValue, activeValue = _this$props$activeVal === void 0 ? [] : _this$props$activeVal; return activeValue[menuIndex] === option.value; } }, { key: "getTabItem", /** * render th li list * @param {*} option * @param {*} menuIndex */ value: function getTabItem(option, menuIndex) { var _this$props2 = this.props, prefixCls = _this$props2.prefixCls, singleMenuItemRender = _this$props2.singleMenuItemRender; var onSelect = this.props.onSelect.bind(this, option, menuIndex, true); var expandProps = { onClick: onSelect }; var menuItemCls = "".concat(prefixCls, "-menu-tab-item"); var label = option.label; if (isFunction(singleMenuItemRender)) { label = singleMenuItemItem(option.label); } return React.createElement("span", _extends({ key: option.key || option.value, className: menuItemCls }, expandProps), label, React.createElement(Icon, { type: "arrow_drop_down" })); } }, { key: "render", value: function render() { var _this3 = this; var _this$props3 = this.props, prefixCls = _this$props3.prefixCls, dropdownMenuColumnStyle = _this$props3.dropdownMenuColumnStyle, isTabSelected = _this$props3.isTabSelected, locale = _this$props3.locale, singleMenuStyle = _this$props3.singleMenuStyle, singlePleaseRender = _this$props3.singlePleaseRender; var showOptions = this.getShowOptions(); var showOptionsIndex = showOptions.length - 1; var activeOptions = this.getActiveOptions(); var dropdownMenuColumnStyleSingle = _objectSpread({}, dropdownMenuColumnStyle, {}, singleMenuStyle); var tabItemRender = activeOptions.map(function (item, indexItem) { return _this3.getTabItem(item, indexItem); }); var tabItemRenderResult; if (showOptions && activeOptions && !isTabSelected && showOptions.length > activeOptions.length) { var pleaseRenderProps = { key: "please_check", className: "".concat(prefixCls, "-menu-tab-item ").concat(prefixCls, "-menu-tab-please"), text: locale.pleaseSelect }; if (isFunction(singlePleaseRender)) { tabItemRenderResult = singlePleaseRender(pleaseRenderProps); } else { var pleaseItem = React.createElement("span", pleaseRenderProps, pleaseRenderProps.text, React.createElement(Icon, { type: "arrow_drop_down" })); tabItemRenderResult = tabItemRender.length > 0 ? [].concat(_toConsumableArray(tabItemRender), [pleaseItem]) : pleaseItem; } } if (isTabSelected) { showOptionsIndex = activeOptions.length - 1 < 0 ? 0 : activeOptions.length - 1; } return React.createElement("div", { className: "".concat(prefixCls, "-mode-single ") }, React.createElement("div", { className: "".concat(prefixCls, "-menu-tab") }, tabItemRenderResult || tabItemRender), React.createElement("ul", { className: "".concat(prefixCls, "-menu ").concat(prefixCls, "-menu-single "), key: showOptionsIndex, style: dropdownMenuColumnStyleSingle }, showOptions[showOptionsIndex].map(function (option) { return _this3.getOption(option, showOptionsIndex); }))); } }]); return Menus; }(Component); _defineProperty(Menus, "defaultProps", { options: [], value: [], activeValue: [], onSelect: function onSelect() {}, prefixCls: 'rc-cascader-menus', visible: false, expandTrigger: 'click', selectedValues: [], isTabSelected: false, locale: Locale, singleMenuStyle: { width: '3rem' }, singleMenuItemStyle: { minWidth: '1rem' } }); _defineProperty(Menus, "propTypes", { // 选择的值 value: PropTypes.array, // 当前激活的值 activeValue: PropTypes.array, // 可选内容 options: PropTypes.array.isRequired, // 注入样式开头 prefixCls: PropTypes.string, // 触发展开事件 expandTrigger: PropTypes.string, // 被选择后触发 onSelect: PropTypes.func, // 是否可见 visible: PropTypes.bool, // 下拉列表的样式配置 dropdownMenuColumnStyle: PropTypes.object, // 标识是由于tab select 触发的事件 isTabSelected: PropTypes.bool, selectedValues: PropTypes.array, locale: PropTypes.object, singleMenuStyle: PropTypes.object, singleMenuItemStyle: PropTypes.object, singlePleaseRender: PropTypes.func, singleMenuItemRender: PropTypes.func }); export { Menus as default }; //# sourceMappingURL=MenusSingle.js.map