UNPKG

choerodon-ui

Version:

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

394 lines (349 loc) 13.8 kB
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 _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import { __decorate } from "tslib"; import React from 'react'; import classNames from 'classnames'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import Tree from '../../../es/tree'; import TreeNode from './TreeNode'; import { DISABLED_FIELD, isSearchTextEmpty, MORE_KEY, Select } from '../select/Select'; import normalizeTreeNodes from './normalizeTreeNodes'; import autobind from '../_util/autobind'; import isIE from '../_util/isIE'; import { defaultRenderer } from '../tree'; import { getTreeNodes } from '../tree/util'; import { preventDefault } from '../_util/EventManager'; import { CheckedStrategy } from '../data-set/enum'; var TreeSelect = /*#__PURE__*/function (_Select) { _inherits(TreeSelect, _Select); var _super = _createSuper(TreeSelect); function TreeSelect() { _classCallCheck(this, TreeSelect); return _super.apply(this, arguments); } _createClass(TreeSelect, [{ key: "parentField", get: function get() { return this.getProp('parentField') || 'parentValue'; } }, { key: "idField", get: function get() { return this.getProp('idField') || this.valueField; } }, { key: "options", get: function get() { var field = this.field, textField = this.textField, valueField = this.valueField, idField = this.idField, parentField = this.parentField, multiple = this.multiple, _this$observableProps = this.observableProps, children = _this$observableProps.children, options = _this$observableProps.options; return options || field && field.getOptions(this.record) || normalizeTreeNodes({ textField: textField, valueField: valueField, disabledField: DISABLED_FIELD, parentField: parentField, idField: idField, multiple: multiple, children: children }); } }, { key: "treeData", get: function get() { return this.optionsWithCombo.filter(function (r) { return !r.parent; }); } }, { key: "selectedKeys", get: function get() { var _this = this; var idField = this.idField; return this.options.reduce(function (array, r) { return _this.isSelected(r) ? array.concat(String(r.get(idField))) : array; }, []); } }, { key: "handleSearch", value: function handleSearch(text) { if (!isSearchTextEmpty(text)) { var options = this.options; var idField = options.props.idField; this.expandedKeys = options.map(function (r) { return String(r.get(idField)); }); } } }, { key: "handleExpand", value: function handleExpand(keys) { this.expandedKeys = keys; } }, { key: "multiple", get: function get() { return !!this.getProp('multiple') || !!this.props.treeCheckable; } }, { key: "handleTreeSelect", value: function handleTreeSelect(_e, _ref) { var node = _ref.node; var record = node.record, disabled = node.disabled, key = node.key; if (key === MORE_KEY) { var options = this.options; options.queryMore(options.currentPage + 1); } else if (!disabled) { var multiple = this.multiple; if (multiple) { if (this.isSelected(record)) { this.unChoose(record); } else { this.choose(record); } } else { this.choose(record); } } } }, { key: "handleTreeCheck", value: function handleTreeCheck(_e, _ref2) { var _this2 = this; var node = _ref2.node; var record = node.record, disabled = node.disabled, key = node.key; var valueField = this.valueField; if (key === MORE_KEY) { var options = this.options; options.queryMore(options.currentPage + 1); } else if (!disabled) { var multiple = this.multiple; if (multiple) { var records = record.treeReduce(function (array, r) { return _this2.isSelected(r) ? array : array.concat(r); }, []); var parents = []; record.parents.every(function (parent, index) { var children = parent.children; if (children && children.every(function (child) { return (index === 0 ? records.includes(child) : parents.includes(child)) || _this2.isSelected(child); })) { parents.push(parent); return true; } return false; }); var showCheckedStrategy = this.getProp('showCheckedStrategy'); if (showCheckedStrategy === CheckedStrategy.SHOW_ALL) { if (this.isSelected(record)) { var unChooseRecords = record.treeReduce(function (array, r) { return _this2.isSelected(r) ? array.concat(r) : array; }, []); var unChooseParents = record.parents.filter(function (parent) { return _this2.isSelected(parent); }); this.unChoose(unChooseRecords.concat(unChooseParents)); } else { this.choose(records.concat(parents)); } } else if (showCheckedStrategy === CheckedStrategy.SHOW_PARENT) { if (this.isSelected(record) || record.parents.some(function (parent) { return _this2.isSelected(parent); })) { var selectedRecords = this.options.filter(function (option) { if (key === option.get(valueField) || option.children && option.children.includes(record)) { return false; } return !!(option.parent && option.children && record.parent && (option.parents.some(function (parent) { return _this2.isSelected(parent); }) || _this2.isSelected(option)) || !option.children && option.parent.children.includes(record) && option.parents.some(function (parent) { return _this2.isSelected(parent); }) || !option.children && option.parents.every(function (parent) { return !_this2.isSelected(parent); }) && _this2.isSelected(option)); }); this.setValue(selectedRecords.map(this.processRecordToObject, this)); } else { var preSelected = this.options.filter(function (option) { return _this2.isSelected(option); }).concat(record, parents); var _selectedRecords = preSelected.filter(function (child) { return !(child.parent && preSelected.includes(child.parent)); }); this.setValue(_selectedRecords.map(this.processRecordToObject, this)); } } else if (showCheckedStrategy === 'SHOW_CHILD') { if (!record.parent) { if (this.options.every(function (option) { return !option.children && _this2.isSelected(option) || !!option.children; })) { this.unChooseAll(); } else { this.choose(records.filter(function (childRecord) { return !childRecord.children; })); } } if (this.isSelected(record) && !record.children) { this.unChoose(record); } else if (!this.isSelected(record) && record.children && record.parent) { if (record.children.every(function (child) { return _this2.isSelected(child); })) { this.unChoose(record.children); } else { this.choose(records.filter(function (chooseRecord) { return !chooseRecord.children; })); } } else { this.choose(records.filter(function (chooseRecord) { return !chooseRecord.children; })); } } } else { this.choose(record); } } } }, { key: "handleTreeNode", value: function handleTreeNode(e) { var onOption = this.props.onOption; var props = onOption(e) || {}; props.className = classNames(props.className, "".concat(this.getMenuPrefixCls(), "-item")); return props; } }, { key: "handleLoadData", value: function handleLoadData(event) { var loadData = this.props.loadData; var dataSet = this.options; var promises = []; if (dataSet) { var record = event.props.record; promises.push(dataSet.queryMoreChild(record)); } if (loadData) { promises.push(loadData(event)); } return Promise.all(promises); } }, { key: "renderSelectAll", value: function renderSelectAll() {// noop } }, { key: "getMenu", value: function getMenu() { var menuProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = this.options; var menuPrefixCls = this.getMenuPrefixCls(); var menuDisabled = this.disabled, textField = this.textField, selectedKeys = this.selectedKeys, expandedKeys = this.expandedKeys, multiple = this.multiple, text = this.text, _this$props = this.props, dropdownMenuStyle = _this$props.dropdownMenuStyle, _this$props$optionRen = _this$props.optionRenderer, optionRenderer = _this$props$optionRen === void 0 ? defaultRenderer : _this$props$optionRen, optionsFilter = _this$props.optionsFilter, treeDefaultExpandAll = _this$props.treeDefaultExpandAll, treeDefaultExpandedKeys = _this$props.treeDefaultExpandedKeys, treeCheckable = _this$props.treeCheckable, async = _this$props.async, loadData = _this$props.loadData; /** * fixed when ie the scroll width would cover the item width */ var IeMenuStyle = !this.dropdownMatchSelectWidth && isIE() ? { padding: '.08rem' } : {}; var treeData = getTreeNodes(options, this.treeData, optionRenderer, this.handleTreeNode, async || !!loadData, textField, optionsFilter, this.matchRecordBySearch, text); if (!treeData || !treeData.length) { return /*#__PURE__*/React.createElement("div", { className: menuPrefixCls }, /*#__PURE__*/React.createElement("div", { className: "".concat(menuPrefixCls, "-item ").concat(menuPrefixCls, "-item-disabled") }, this.loading ? ' ' : this.getNotFoundContent())); } if (options.paging && options.currentPage < options.totalPage) { treeData.push({ key: MORE_KEY, eventKey: MORE_KEY, title: this.getPagingOptionContent(), className: "".concat(menuPrefixCls, "-item ").concat(menuPrefixCls, "-item-more"), isLeaf: true }); } var props = {}; if (expandedKeys) { props.expandedKeys = expandedKeys.slice(); } return /*#__PURE__*/React.createElement(Tree, _extends({ ref: this.saveMenu, ripple: true, disabled: menuDisabled, onMouseDown: preventDefault, onSelect: treeCheckable ? this.handleTreeCheck : this.handleTreeSelect, onCheck: this.handleTreeCheck, onExpand: this.handleExpand, style: _objectSpread(_objectSpread({}, IeMenuStyle), dropdownMenuStyle), selectable: true, focusable: false, treeData: treeData, defaultExpandAll: treeDefaultExpandAll, defaultExpandedKeys: treeDefaultExpandedKeys, selectedKeys: selectedKeys, checkedKeys: selectedKeys, checkable: treeCheckable, className: menuPrefixCls, multiple: multiple, loadData: async ? this.handleLoadData : loadData }, props, menuProps)); } }]); return TreeSelect; }(Select); TreeSelect.displayName = 'TreeSelect'; TreeSelect.defaultProps = _objectSpread(_objectSpread({}, Select.defaultProps), {}, { suffixCls: 'tree-select', dropdownMatchSelectWidth: false, showCheckedStrategy: 'SHOW_ALL', reverse: false }); TreeSelect.TreeNode = TreeNode; __decorate([observable], TreeSelect.prototype, "expandedKeys", void 0); __decorate([computed], TreeSelect.prototype, "parentField", null); __decorate([computed], TreeSelect.prototype, "idField", null); __decorate([computed], TreeSelect.prototype, "options", null); __decorate([computed], TreeSelect.prototype, "treeData", null); __decorate([computed], TreeSelect.prototype, "selectedKeys", null); __decorate([action], TreeSelect.prototype, "handleSearch", null); __decorate([autobind, action], TreeSelect.prototype, "handleExpand", null); __decorate([computed], TreeSelect.prototype, "multiple", null); __decorate([autobind], TreeSelect.prototype, "handleTreeSelect", null); __decorate([autobind], TreeSelect.prototype, "handleTreeCheck", null); __decorate([autobind], TreeSelect.prototype, "handleTreeNode", null); __decorate([autobind], TreeSelect.prototype, "handleLoadData", null); __decorate([autobind], TreeSelect.prototype, "getMenu", null); TreeSelect = __decorate([observer], TreeSelect); export default TreeSelect; //# sourceMappingURL=TreeSelect.js.map