UNPKG

choerodon-ui

Version:

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

486 lines (396 loc) 16.8 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultFieldNames = exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _arrayTreeFilter = _interopRequireDefault(require("array-tree-filter")); var _shallowequal = _interopRequireDefault(require("shallowequal")); var _trigger = _interopRequireDefault(require("../trigger")); var _icon = _interopRequireDefault(require("../../icon")); var _Menus = _interopRequireDefault(require("./Menus")); var _MenusSingle = _interopRequireDefault(require("./MenusSingle")); var _KeyCode = _interopRequireDefault(require("../../_util/KeyCode")); var _en_US = _interopRequireDefault(require("./locale/en_US")); var _excluded = ["prefixCls", "transitionName", "popupClassName", "options", "disabled", "builtinPlacements", "popupPlacement", "children", "locale", "menuMode", "singleMenuStyle", "singleMenuItemStyle"]; var defaultFieldNames = { label: 'label', value: 'value', children: 'children' }; exports.defaultFieldNames = defaultFieldNames; var BUILT_IN_PLACEMENTS = { bottomLeft: { points: ['tl', 'bl'], offset: [0, 4], overflow: { adjustX: 1, adjustY: 1 } }, topLeft: { points: ['bl', 'tl'], offset: [0, -4], overflow: { adjustX: 1, adjustY: 1 } }, bottomRight: { points: ['tr', 'br'], offset: [0, 4], overflow: { adjustX: 1, adjustY: 1 } }, topRight: { points: ['br', 'tr'], offset: [0, -4], overflow: { adjustX: 1, adjustY: 1 } } }; var Cascader = /*#__PURE__*/function (_Component) { (0, _inherits2["default"])(Cascader, _Component); var _super = (0, _createSuper2["default"])(Cascader); function Cascader(props) { var _this; (0, _classCallCheck2["default"])(this, Cascader); _this = _super.call(this, props); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setPopupVisible", function (popupVisible) { if (!('popupVisible' in _this.props)) { _this.setState({ popupVisible: popupVisible }); } // sync activeValue with value when panel open if (popupVisible && !_this.state.popupVisible) { _this.setState({ activeValue: _this.state.value }); } _this.props.onPopupVisibleChange(popupVisible); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleChange", function (options, setProps, e) { if (e.type !== 'keydown' || e.keyCode === _KeyCode["default"].ENTER) { var valueField = _this.getFieldName('value'); _this.props.onChange(options.map(function (o) { return o[valueField]; }), options); _this.setPopupVisible(setProps.visible); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handlePopupVisibleChange", function (popupVisible) { _this.setPopupVisible(popupVisible); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMenuSelect", function (targetOption, menuIndex) { var isTabSelected = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var e = arguments.length > 3 ? arguments[3] : undefined; // Keep focused state for keyboard support var triggerNode = _this.trigger.getRootDomNode(); if (triggerNode && triggerNode.focus) { triggerNode.focus(); } var _this$props = _this.props, changeOnSelect = _this$props.changeOnSelect, loadData = _this$props.loadData, expandTrigger = _this$props.expandTrigger; if (!targetOption || targetOption.disabled) { return; } var activeValue = _this.state.activeValue; var valueField = _this.getFieldName('value'); var childrenField = _this.getFieldName('children'); activeValue = activeValue.slice(0, menuIndex + 1); activeValue[menuIndex] = targetOption[valueField]; var activeOptions = _this.getActiveOptions(activeValue); if (targetOption.isLeaf === false && !targetOption[childrenField] && loadData) { if (changeOnSelect) { _this.handleChange(activeOptions, { visible: true }, e); } _this.setState({ activeValue: activeValue, isTabSelected: isTabSelected }); loadData(activeOptions); return; } var newState = {}; if (!targetOption[childrenField] || !targetOption[childrenField].length) { _this.handleChange(activeOptions, { visible: false }, e); // set value to activeValue when select leaf option newState.value = activeValue; // add e.type judgement to prevent `onChange` being triggered by mouseEnter } else if (changeOnSelect && (e.type === 'click' || e.type === 'keydown')) { if (expandTrigger === 'hover') { _this.handleChange(activeOptions, { visible: false }, e); } else { _this.handleChange(activeOptions, { visible: true }, e); } // set value to activeValue on every select newState.value = activeValue; } newState.activeValue = activeValue; // not change the value by keyboard if ('value' in _this.props || e.type === 'keydown' && e.keyCode !== _KeyCode["default"].ENTER) { delete newState.value; } newState.isTabSelected = isTabSelected; _this.setState(newState); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleKeyDown", function (e) { var children = _this.props.children; // Don't bind keyboard support when children specify the onKeyDown if (children && children.props.onKeyDown) { children.props.onKeyDown(e); return; } var activeValue = (0, _toConsumableArray2["default"])(_this.state.activeValue); var currentLevel = activeValue.length - 1 < 0 ? 0 : activeValue.length - 1; var currentOptions = _this.getCurrentLevelOptions(); if (e.keyCode !== _KeyCode["default"].DOWN && e.keyCode !== _KeyCode["default"].UP && e.keyCode !== _KeyCode["default"].LEFT && e.keyCode !== _KeyCode["default"].RIGHT && e.keyCode !== _KeyCode["default"].ENTER && e.keyCode !== _KeyCode["default"].BACKSPACE && e.keyCode !== _KeyCode["default"].ESC) { return; } // Press any keys above to reopen menu if (!_this.state.popupVisible && e.keyCode !== _KeyCode["default"].BACKSPACE && e.keyCode !== _KeyCode["default"].LEFT && e.keyCode !== _KeyCode["default"].RIGHT && e.keyCode !== _KeyCode["default"].ESC) { _this.setPopupVisible(true); return; } var valueField = _this.getFieldName('value'); var childrenField = _this.getFieldName('children'); var currentIndex = currentOptions.map(function (o) { return o[valueField]; }).indexOf(activeValue[currentLevel]); if (e.keyCode === _KeyCode["default"].DOWN || e.keyCode === _KeyCode["default"].UP) { var nextIndex = currentIndex; if (nextIndex !== -1) { if (e.keyCode === _KeyCode["default"].DOWN) { nextIndex += 1; nextIndex = nextIndex >= currentOptions.length ? 0 : nextIndex; } else { nextIndex -= 1; nextIndex = nextIndex < 0 ? currentOptions.length - 1 : nextIndex; } } else { nextIndex = 0; } activeValue[currentLevel] = currentOptions[nextIndex][valueField]; } else if (e.keyCode === _KeyCode["default"].LEFT || e.keyCode === _KeyCode["default"].BACKSPACE) { activeValue.splice(activeValue.length - 1, 1); } else if (e.keyCode === _KeyCode["default"].RIGHT) { if (currentOptions[currentIndex] && currentOptions[currentIndex][childrenField]) { activeValue.push(currentOptions[currentIndex][childrenField][0][valueField]); } } else if (e.keyCode === _KeyCode["default"].ESC) { _this.setPopupVisible(false); return; } if (!activeValue || activeValue.length === 0) { _this.setPopupVisible(false); } var activeOptions = _this.getActiveOptions(activeValue); var targetOption = activeOptions[activeOptions.length - 1]; _this.handleMenuSelect(targetOption, activeOptions.length - 1, false, e); if (_this.props.onKeyDown) { _this.props.onKeyDown(e); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "saveTrigger", function (node) { _this.trigger = node; }); var initialValue = []; if ('value' in props) { initialValue = props.value || []; } else if ('defaultValue' in props) { initialValue = props.defaultValue || []; } _this.state = { popupVisible: props.popupVisible, activeValue: initialValue, value: initialValue, isTabSelected: false }; return _this; } (0, _createClass2["default"])(Cascader, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps && !(0, _shallowequal["default"])(this.props.value, nextProps.value)) { var newValues = { value: nextProps.value || [], activeValue: nextProps.value || [] }; // allow activeValue diff from value if ('loadData' in nextProps) { delete newValues.activeValue; } this.setState(newValues); } if ('popupVisible' in nextProps) { this.setState({ popupVisible: nextProps.popupVisible }); } } }, { key: "getPopupDOMNode", value: function getPopupDOMNode() { return this.trigger.getPopupDomNode(); } }, { key: "getFieldName", value: function getFieldName(name) { var _this$props2 = this.props, fieldNames = _this$props2.fieldNames, filedNames = _this$props2.filedNames; if ('filedNames' in this.props) { return filedNames[name] || defaultFieldNames[name]; // For old compatibility } return fieldNames[name] || defaultFieldNames[name]; } }, { key: "getFieldNames", value: function getFieldNames() { var _this$props3 = this.props, fieldNames = _this$props3.fieldNames, filedNames = _this$props3.filedNames; if ('filedNames' in this.props) { return filedNames; // For old compatibility } return fieldNames; } }, { key: "getCurrentLevelOptions", value: function getCurrentLevelOptions() { var options = this.props.options; var _this$state$activeVal = this.state.activeValue, activeValue = _this$state$activeVal === void 0 ? [] : _this$state$activeVal; var valueField = this.getFieldName('value'); var childrenField = this.getFieldName('children'); var result = (0, _arrayTreeFilter["default"])(options, function (o, level) { return o[valueField] === activeValue[level]; }, { childrenKeyName: childrenField }); if (result[result.length - 2]) { return result[result.length - 2][childrenField]; } return (0, _toConsumableArray2["default"])(options).filter(function (o) { return !o.disabled; }); } }, { key: "getActiveOptions", value: function getActiveOptions(activeValue) { var valueField = this.getFieldName('value'); var childrenField = this.getFieldName('children'); return (0, _arrayTreeFilter["default"])(this.props.options, function (o, level) { return o[valueField] === activeValue[level]; }, { childrenKeyName: childrenField }); } }, { key: "render", value: function render() { var _this$props4 = this.props, prefixCls = _this$props4.prefixCls, transitionName = _this$props4.transitionName, popupClassName = _this$props4.popupClassName, options = _this$props4.options, disabled = _this$props4.disabled, builtinPlacements = _this$props4.builtinPlacements, popupPlacement = _this$props4.popupPlacement, children = _this$props4.children, locale = _this$props4.locale, menuMode = _this$props4.menuMode, singleMenuStyle = _this$props4.singleMenuStyle, singleMenuItemStyle = _this$props4.singleMenuItemStyle, restProps = (0, _objectWithoutProperties2["default"])(_this$props4, _excluded); // Did not show popup when there is no options var menus = /*#__PURE__*/_react["default"].createElement("div", null); var emptyMenuClassName = ''; if (options && options.length > 0) { if (menuMode === 'multiple') { menus = /*#__PURE__*/_react["default"].createElement(_Menus["default"], (0, _extends2["default"])({}, this.props, { fieldNames: this.getFieldNames(), defaultFieldNames: defaultFieldNames, value: this.state.value, activeValue: this.state.activeValue, onSelect: this.handleMenuSelect, visible: this.state.popupVisible })); } else if (menuMode === 'single') { menus = /*#__PURE__*/_react["default"].createElement(_MenusSingle["default"], (0, _extends2["default"])({}, this.props, { fieldNames: this.getFieldNames(), defaultFieldNames: defaultFieldNames, isTabSelected: this.state.isTabSelected, value: this.state.value, activeValue: this.state.activeValue, onSelect: this.handleMenuSelect, visible: this.state.popupVisible, locale: locale, singleMenuStyle: singleMenuStyle, singleMenuItemStyle: singleMenuItemStyle })); } else { throw new Error('please use correct mode for menu ex "single" and "multiple"'); } } else { emptyMenuClassName = " ".concat(prefixCls, "-menus-empty"); } return /*#__PURE__*/_react["default"].createElement(_trigger["default"], (0, _extends2["default"])({ ref: this.saveTrigger }, restProps, { options: options, disabled: disabled, popupPlacement: popupPlacement, builtinPlacements: builtinPlacements, popupTransitionName: transitionName, action: disabled ? [] : ['click'], popupVisible: disabled ? false : this.state.popupVisible, onPopupVisibleChange: this.handlePopupVisibleChange, prefixCls: "".concat(prefixCls, "-menus"), popupClassName: popupClassName + emptyMenuClassName, popup: menus }), /*#__PURE__*/(0, _react.cloneElement)(children, { onKeyDown: this.handleKeyDown, tabIndex: disabled ? undefined : 0 })); } }]); return Cascader; }(_react.Component); exports["default"] = Cascader; (0, _defineProperty2["default"])(Cascader, "defaultProps", { options: [], onChange: function onChange() {}, onPopupVisibleChange: function onPopupVisibleChange() {}, disabled: false, transitionName: '', prefixCls: 'rc-cascader', popupClassName: '', popupPlacement: 'bottomLeft', builtinPlacements: BUILT_IN_PLACEMENTS, expandTrigger: 'click', fieldNames: defaultFieldNames, expandIcon: /*#__PURE__*/_react["default"].createElement(_icon["default"], { type: "navigate_next" }), menuMode: 'multiple', locale: _en_US["default"] }); //# sourceMappingURL=Cascader.js.map