UNPKG

choerodon-ui

Version:

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

1,462 lines (1,199 loc) 61.4 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"]; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); 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 _reactDom = require("react-dom"); var _classnames2 = _interopRequireDefault(require("classnames")); var _warning = _interopRequireDefault(require("../../_util/warning")); var _noop = _interopRequireDefault(require("lodash/noop")); var _KeyCode = _interopRequireDefault(require("../../_util/KeyCode")); var _toArray = _interopRequireDefault(require("../util/Children/toArray")); var _menu = require("../menu"); var _Option = _interopRequireDefault(require("./Option")); var _animate = _interopRequireDefault(require("../../animate")); var _button = _interopRequireDefault(require("../../button")); var _ripple = _interopRequireDefault(require("../../ripple")); var _util = require("./util"); var _SelectTrigger = _interopRequireDefault(require("./SelectTrigger")); /* eslint func-names: 1 */ function chaining() { for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) { fns[_key] = arguments[_key]; } return function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } // eslint-disable-line // eslint-disable-line for (var i = 0; i < fns.length; i++) { if (fns[i] && typeof fns[i] === 'function') { fns[i].apply(this, args); } } }; } var BUILT_IN_PLACEMENTS = { bottomLeft: { points: ['tl', 'bl'], offset: [0, 4], overflow: { adjustX: 1, adjustY: 1 } }, bottomRight: { points: ['tr', 'br'], offset: [0, 4], overflow: { adjustX: 1, adjustY: 1 } }, topLeft: { points: ['bl', 'tl'], offset: [0, -4], overflow: { adjustX: 1, adjustY: 1 } }, topRight: { points: ['br', 'tr'], offset: [0, -4], overflow: { adjustX: 1, adjustY: 1 } } }; var Select = /*#__PURE__*/function (_Component) { (0, _inherits2["default"])(Select, _Component); var _super = (0, _createSuper2["default"])(Select); function Select(_props) { var _this; (0, _classCallCheck2["default"])(this, Select); _this = _super.call(this, _props); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "needExpand", true); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFilterChange", function () { var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var onFilterChange = _this.props.onFilterChange; _this.onInputValueChange(val); _this.setState({ filterValue: val }); if (onFilterChange) { onFilterChange(val); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onInputChange", function (event) { var val = event.target.value; _this.onInputValueChange(val); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onInputValueChange", function (val) { var _this$props = _this.props, tokenSeparators = _this$props.tokenSeparators, onInput = _this$props.onInput; onInput(val); if ((0, _util.isMultipleOrTags)(_this.props) && tokenSeparators.length && (0, _util.includesSeparators)(val, tokenSeparators)) { var nextValue = _this.getValueByInput(val); if (nextValue !== undefined) { _this.fireChange(nextValue); } _this.setOpenState(false, true); _this.setInputValue('', false); return; } _this.setInputValue(val); _this.setState({ open: true }); if ((0, _util.isCombobox)(_this.props)) { _this.fireChange([val]); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onDropdownVisibleChange", function (open) { if (_this.needExpand) { if (open && !_this.state.focused) { _this.clearBlurTime(); _this.timeoutFocus(); _this.setState({ focused: true }); } var filter = _this.props.filter; if (filter) { _this.onFilterChange(''); if (open) { setTimeout(function () { var filterInput = _this.selectTriggerRef.getFilterInput(); filterInput && filterInput.focus(); }, 20); } } _this.setOpenState(open); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyDown", function (event) { var props = _this.props; if (props.disabled) { return; } var keyCode = event.keyCode; if (_this.state.open && !_this.getInputDOMNode()) { _this.onInputKeyDown(event); } else if (keyCode === _KeyCode["default"].ENTER || keyCode === _KeyCode["default"].DOWN) { _this.setOpenState(true); event.preventDefault(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onInputKeyDown", function (event) { var props = _this.props; if (props.disabled) { return; } var state = _this.state; var keyCode = event.keyCode; if ((0, _util.isMultipleOrTags)(props) && !event.target.value && keyCode === _KeyCode["default"].BACKSPACE) { event.preventDefault(); var value = state.value; if (value.length) { _this.removeSelected(value[value.length - 1], value.length - 1); } return; } if (keyCode === _KeyCode["default"].DOWN) { if (!state.open) { _this.openIfHasChildren(); event.preventDefault(); event.stopPropagation(); return; } } else if (keyCode === _KeyCode["default"].ENTER && state.open) { // Aviod trigger form submit when select item // https://github.com/ant-design/ant-design/issues/10861 event.preventDefault(); } else if (keyCode === _KeyCode["default"].ESC) { if (state.open) { _this.setOpenState(false); event.preventDefault(); event.stopPropagation(); } return; } if (state.open) { var menu = _this.selectTriggerRef.getInnerMenu(); if (menu && menu.onKeyDown(event, _this.handleBackfill)) { event.preventDefault(); event.stopPropagation(); } } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMenuSelect", function (_ref) { var item = _ref.item; if (!item) { return; } var value = _this.state.value; var props = _this.props; var selectedValue = (0, _util.getValuePropValue)(item); var lastValue = value[value.length - 1]; if (_this.fireSelect(selectedValue) === false) { return; } if ((0, _util.isMultipleOrTags)(props)) { if ((0, _util.findIndexInValueBySingleValue)(value, selectedValue) !== -1) { return; } value = value.concat([selectedValue]); } else { if ((0, _util.isCombobox)(props)) { _this.skipAdjustOpen = true; _this.clearAdjustTimer(); _this.skipAdjustOpenTimer = setTimeout(function () { _this.skipAdjustOpen = false; }, 0); } if (lastValue && lastValue === selectedValue && selectedValue !== _this.state.backfillValue) { _this.setOpenState(false, true); return; } value = [selectedValue]; _this.setOpenState(false, true); } _this.fireChange(value); var inputValue; if ((0, _util.isCombobox)(props)) { inputValue = (0, _util.getPropValue)(item, props.optionLabelProp); } else { inputValue = ''; } if (props.autoClearSearchValue || !props.filter) { _this.setInputValue(inputValue, !props.filter); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMenuDeselect", function (_ref2) { var item = _ref2.item, domEvent = _ref2.domEvent; if (domEvent.type === 'keydown' && domEvent.keyCode === _KeyCode["default"].ENTER) { _this.removeSelected((0, _util.getValuePropValue)(item), null); return; } if (domEvent.type === 'click') { _this.removeSelected((0, _util.getValuePropValue)(item), null); } var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this), props = _assertThisInitialize.props; if (props.autoClearSearchValue) { _this.setInputValue('', false); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onArrowClick", function (e) { e.stopPropagation(); e.preventDefault(); if (!_this.props.disabled) { _this.onDropdownVisibleChange(!_this.state.open); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onPlaceholderClick", function () { if (_this.getInputDOMNode()) { _this.getInputDOMNode().focus(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onOuterFocus", function (e) { if (_this.props.disabled) { e.preventDefault(); return; } _this.clearBlurTime(); if (!(0, _util.isMultipleOrTagsOrCombobox)(_this.props) && e.target === _this.getInputDOMNode()) { return; } if (_this.state.focused) { return; } _this.setState({ focused: true }); // only effect multiple or tag mode if (!(0, _util.isMultipleOrTags)(_this.props) || !_this._mouseDown) { _this.timeoutFocus(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onPopupFocus", function () { // fix ie scrollbar, focus element again _this.maybeFocus(true, true); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onOuterBlur", function (e) { if (_this.props.disabled) { e.preventDefault(); return; } _this.blurTimer = setTimeout(function () { _this.setState({ focused: false }); var props = _this.props; var value = _this.state.value; var inputValue = _this.state.inputValue; if ((0, _util.isSingleMode)(props) && props.showSearch && inputValue && props.defaultActiveFirstOption) { var options = _this._options || []; if (options.length) { var firstOption = (0, _util.findFirstMenuItem)(options); if (firstOption) { value = [(0, _util.getValuePropValue)(firstOption)]; _this.fireChange(value); } } } else if ((0, _util.isMultipleOrTags)(props) && inputValue) { if (props.blurChange) { // why not use setState? _this.state.inputValue = _this.getInputDOMNode().value = ''; } value = _this.getValueByInput(inputValue); if (value !== undefined && props.blurChange) { _this.fireChange(value); } } props.onBlur(_this.getVLForOnChange(value)); if (!props.footer && !props.filter && !((0, _util.isMultipleOrTags)(props) && props.showCheckAll)) { _this.setOpenState(false); } }, 10); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onClearSelection", function (event) { var props = _this.props; var state = _this.state; if (props.disabled) { return; } var inputValue = state.inputValue, value = state.value; event.stopPropagation(); if (inputValue || value.length) { props.onClear(); if (value.length) { _this.fireChange([]); } _this.setOpenState(false, true); if (inputValue) { _this.setInputValue(''); } } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChoiceAnimationLeave", function () { _this.forcePopupAlign(); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getOptionInfoBySingleValue", function (value, optionsInfo) { var info; optionsInfo = optionsInfo || _this.state.optionsInfo; if (optionsInfo[(0, _util.getMapKey)(value)]) { info = optionsInfo[(0, _util.getMapKey)(value)]; } if (info) { return info; } var defaultLabel = value; if (_this.props.labelInValue) { var label = (0, _util.getLabelFromPropsValue)(_this.props.value, value); if (label !== undefined) { defaultLabel = label; } } var defaultInfo = { option: /*#__PURE__*/_react["default"].createElement(_Option["default"], { value: value, key: value }, value), value: value, label: defaultLabel }; return defaultInfo; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getOptionBySingleValue", function (value) { var _this$getOptionInfoBy = _this.getOptionInfoBySingleValue(value), option = _this$getOptionInfoBy.option; return option; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getOptionsBySingleValue", function (values) { return values.map(function (value) { return _this.getOptionBySingleValue(value); }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getValueByLabel", function (label) { if (label === undefined) { return null; } var value = null; Object.keys(_this.state.optionsInfo).forEach(function (key) { var info = _this.state.optionsInfo[key]; if ((0, _util.toArray)(info.label).join('') === label) { value = info.value; } }); return value; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getVLBySingleValue", function (value) { if (_this.props.labelInValue) { return { key: value, label: _this.getLabelBySingleValue(value) }; } return value; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getVLForOnChange", function (vls_) { var vls = vls_; if (vls !== undefined) { if (!_this.props.labelInValue) { vls = vls.map(function (v) { return v; }); } else { vls = vls.map(function (vl) { return { key: vl, label: _this.getLabelBySingleValue(vl) }; }); } return (0, _util.isMultipleOrTags)(_this.props) ? vls : vls[0]; } return vls; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getLabelBySingleValue", function (value, optionsInfo) { var _this$getOptionInfoBy2 = _this.getOptionInfoBySingleValue(value, optionsInfo), label = _this$getOptionInfoBy2.label; return label; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getDropdownContainer", function () { if (!_this.dropdownContainer) { _this.dropdownContainer = document.createElement('div'); document.body.appendChild(_this.dropdownContainer); } return _this.dropdownContainer; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getPlaceholderElement", function (floatLabel) { var _assertThisInitialize2 = (0, _assertThisInitialized2["default"])(_this), props = _assertThisInitialize2.props, state = _assertThisInitialize2.state; var hidden = false; if (state.inputValue) { hidden = true; } if (state.value.length) { hidden = true; } if ((0, _util.isCombobox)(props) && state.value.length === 1 && !state.value[0]) { hidden = false; } var _this$props2 = _this.props, placeholder = _this$props2.placeholder, prefixCls = _this$props2.prefixCls; if ((!floatLabel || state.focused) && placeholder) { return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({ onMouseDown: _util.preventDefaultEvent, style: (0, _objectSpread2["default"])({ display: hidden ? 'none' : 'block' }, _util.UNSELECTABLE_STYLE) }, _util.UNSELECTABLE_ATTRIBUTE, { onClick: _this.onPlaceholderClick, className: "".concat(prefixCls, "-selection__placeholder") }), placeholder); } return null; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getInputElement", function () { var props = _this.props; var inputElement = props.getInputElement ? props.getInputElement() : /*#__PURE__*/_react["default"].createElement("input", { id: props.id, autoComplete: "off" }); var inputCls = (0, _classnames2["default"])(inputElement.props.className, (0, _defineProperty2["default"])({}, "".concat(props.prefixCls, "-search__field"), true)); // Add space to the end of the inputValue as the width measurement tolerance return /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(props.prefixCls, "-search__field__wrap") }, /*#__PURE__*/(0, _react.cloneElement)(inputElement, { ref: _this.saveInputRef, onChange: _this.onInputChange, onKeyDown: chaining(_this.onInputKeyDown, inputElement.props.onKeyDown, _this.props.onInputKeyDown), value: _this.state.inputValue, disabled: props.disabled, className: inputCls }), /*#__PURE__*/_react["default"].createElement("span", { ref: _this.saveInputMirrorRef, className: "".concat(props.prefixCls, "-search__field__mirror") }, _this.state.inputValue, "\xA0")); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getInputDOMNode", function () { return _this.topCtrlRef ? _this.topCtrlRef.querySelector('input,textarea,div[contentEditable]') : _this.inputRef; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getInputMirrorDOMNode", function () { return _this.inputMirrorRef; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getPopupDOMNode", function () { return _this.selectTriggerRef.getPopupDOMNode(); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getPopupMenuComponent", function () { return _this.selectTriggerRef.getInnerMenu(); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setOpenState", function (open, needFocus) { var _assertThisInitialize3 = (0, _assertThisInitialized2["default"])(_this), props = _assertThisInitialize3.props, state = _assertThisInitialize3.state; if (state.open === open) { _this.maybeFocus(open, needFocus); return; } var nextState = { open: open, backfillValue: undefined }; // clear search input value when open is false in singleMode. if (!open && (0, _util.isSingleMode)(props) && props.showSearch) { _this.setInputValue('', false); } if (!open) { _this.maybeFocus(open, needFocus); } _this.setState(nextState, function () { if (open) { _this.maybeFocus(open, needFocus); } }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setInputValue", function (inputValue) { var fireSearch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (inputValue !== _this.state.inputValue) { _this.setState({ inputValue: inputValue }, _this.forcePopupAlign); if (fireSearch) { _this.props.onSearch(inputValue); } } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getValueByInput", function (string) { var _this$props3 = _this.props, multiple = _this$props3.multiple, tokenSeparators = _this$props3.tokenSeparators; var nextValue = _this.state.value; var hasNewValue = false; (0, _util.splitBySeparators)(string, tokenSeparators).forEach(function (label) { var selectedValue = [label]; if (multiple) { var value = _this.getValueByLabel(label); if (value && (0, _util.findIndexInValueBySingleValue)(nextValue, value) === -1) { nextValue = nextValue.concat(value); hasNewValue = true; _this.fireSelect(value); } } else { // tag if ((0, _util.findIndexInValueBySingleValue)(nextValue, label) === -1) { nextValue = nextValue.concat(selectedValue); hasNewValue = true; _this.fireSelect(label); } } }); return hasNewValue ? nextValue : undefined; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getRealOpenState", function () { var _open = _this.props.open; if (typeof _open === 'boolean') { return _open; } var open = _this.state.open; var options = _this._options || []; if ((0, _util.isMultipleOrTagsOrCombobox)(_this.props) || !_this.props.showSearch) { if (open && !options.length) { open = false; } } return open; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "focus", function () { if ((0, _util.isSingleMode)(_this.props)) { _this.selectionRef.focus(); } else { _this.getInputDOMNode().focus(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "blur", function () { if ((0, _util.isSingleMode)(_this.props)) { _this.selectionRef.blur(); } else { _this.getInputDOMNode().blur(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleBackfill", function (item) { if (!_this.props.backfill || !((0, _util.isSingleMode)(_this.props) || (0, _util.isCombobox)(_this.props))) { return; } var key = (0, _util.getValuePropValue)(item); if ((0, _util.isCombobox)(_this.props)) { _this.setInputValue(key, false); } _this.setState({ value: [key], backfillValue: key }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "filterOption", function (input, child) { var defaultFilter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _util.defaultFilterFn; var value = _this.state.value; var lastValue = value[value.length - 1]; if (!input || lastValue && lastValue === _this.state.backfillValue) { return true; } var filterFn = _this.props.filterOption; if ('filterOption' in _this.props) { if (_this.props.filterOption === true) { filterFn = defaultFilter; } } else { filterFn = defaultFilter; } if (!filterFn) { return true; } else if (typeof filterFn === 'function') { return filterFn.call((0, _assertThisInitialized2["default"])(_this), input, child); } else if (child.props.disabled) { return false; } return true; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "timeoutFocus", function () { if (_this.focusTimer) { _this.clearFocusTime(); } _this.focusTimer = setTimeout(function () { _this.props.onFocus(); }, 10); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clearFocusTime", function () { if (_this.focusTimer) { clearTimeout(_this.focusTimer); _this.focusTimer = null; } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clearBlurTime", function () { if (_this.blurTimer) { clearTimeout(_this.blurTimer); _this.blurTimer = null; } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clearAdjustTimer", function () { if (_this.skipAdjustOpenTimer) { clearTimeout(_this.skipAdjustOpenTimer); _this.skipAdjustOpenTimer = null; } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "maybeFocus", function (open, needFocus) { if (needFocus || open) { var input = _this.getInputDOMNode(); var _document = document, activeElement = _document.activeElement; if (input && (open || (0, _util.isMultipleOrTagsOrCombobox)(_this.props))) { if (activeElement !== input) { input.focus(); _this.setState({ focused: true }); } } else { if (activeElement !== _this.selectionRef) { _this.selectionRef.focus(); _this.setState({ focused: true }); } } } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleChoiceItemClick", function (selectedKey, e) { var props = _this.props; if (props.disabled || _this.isChildDisabled(selectedKey)) { return; } if (!e.isDefaultPrevented()) { props.onChoiceItemClick(_this.getVLBySingleValue(selectedKey), _this.getOptionBySingleValue(selectedKey)); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "removeSelected", function (selectedKey, index, e) { _this.needExpand = false; setTimeout(function () { _this.needExpand = true; }, 100); if (e && e.preventDefault) { e.preventDefault(); } var props = _this.props; if (props.disabled || _this.isChildDisabled(selectedKey)) { return; } if (e && e.stopPropagation) { e.stopPropagation(); } var value = _this.state.value.filter(function (singleValue, i) { return singleValue !== selectedKey || index !== null && i !== index; }); var canMultiple = (0, _util.isMultipleOrTags)(props); if (canMultiple) { var event = selectedKey; if (props.labelInValue) { event = { key: selectedKey, label: _this.getLabelBySingleValue(selectedKey) }; } props.onDeselect(event, _this.getOptionBySingleValue(selectedKey), index); if (typeof props.onChoiceRemove === 'function') { props.onChoiceRemove(selectedKey); } } _this.fireChange(value); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "openIfHasChildren", function () { var props = _this.props; if (_react.Children.count(props.children) || (0, _util.isSingleMode)(props)) { _this.setOpenState(true); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "fireSelect", function (value) { return _this.props.onSelect(_this.getVLBySingleValue(value), _this.getOptionBySingleValue(value)); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "fireChange", function (value) { var props = _this.props; if (!('value' in props)) { _this.setState({ value: value }, _this.forcePopupAlign); } var vls = _this.getVLForOnChange(value); var options = _this.getOptionsBySingleValue(value); props.onChange(vls, (0, _util.isMultipleOrTags)(_this.props) ? options : options[0]); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "isChildDisabled", function (key) { return (0, _toArray["default"])(_this.props.children).some(function (child) { var childValue = (0, _util.getValuePropValue)(child); return childValue === key && child.props && child.props.disabled; }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "forcePopupAlign", function () { _this.selectTriggerRef.triggerRef.forcePopupAlign(); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "adjustOpenState", function () { if (_this.skipAdjustOpen) { return; } var open = _this.state.open; var options = []; // If hidden menu due to no options, then it should be calculated again if (open || _this.hiddenForNoOptions) { options = _this.renderFilterOptions(); } _this._options = options; if ((0, _util.isMultipleOrTagsOrCombobox)(_this.props) || !_this.props.showSearch) { if (open && !options.length) { open = false; _this.hiddenForNoOptions = true; } // Keep menu open if there are options and hidden for no options before if (_this.hiddenForNoOptions && options.length) { open = true; _this.hiddenForNoOptions = false; } } _this.state.open = open; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderFilterOptions", function () { var inputValue = _this.state.inputValue; var _this$props4 = _this.props, children = _this$props4.children, tags = _this$props4.tags, filterOption = _this$props4.filterOption, notFoundContent = _this$props4.notFoundContent, showNotFindInputItem = _this$props4.showNotFindInputItem, showNotFindSelectedItem = _this$props4.showNotFindSelectedItem, dropdownMenuItemCheckable = _this$props4.dropdownMenuItemCheckable; var menuItems = []; var childrenKeys = []; var options = _this.renderFilterOptionsFromChildren(children, childrenKeys, menuItems); if (tags) { if (showNotFindSelectedItem) { // tags value must be string var value = _this.state.value || []; value = value.filter(function (singleValue) { return childrenKeys.indexOf(singleValue) === -1 && (!inputValue || String(singleValue).indexOf(String(inputValue)) > -1); }); value.forEach(function (singleValue) { var key = singleValue; var menuItem = /*#__PURE__*/_react["default"].createElement(_menu.Item, { checkable: dropdownMenuItemCheckable, style: _util.UNSELECTABLE_STYLE, role: "option", attribute: _util.UNSELECTABLE_ATTRIBUTE, value: key, key: key }, key); options.push(menuItem); menuItems.push(menuItem); }); } if (inputValue && showNotFindInputItem) { var notFindInputItem = menuItems.every(function (option) { // this.filterOption return true has two meaning, // 1, some one exists after filtering // 2, filterOption is set to false // condition 2 does not mean the option has same value with inputValue var filterFn = function filterFn() { return (0, _util.getValuePropValue)(option) === inputValue; }; if (filterOption !== false) { return !_this.filterOption.call((0, _assertThisInitialized2["default"])(_this), inputValue, option, filterFn); } return !filterFn(); }); if (notFindInputItem) { options.unshift( /*#__PURE__*/_react["default"].createElement(_menu.Item, { checkable: dropdownMenuItemCheckable, style: _util.UNSELECTABLE_STYLE, role: "option", attribute: _util.UNSELECTABLE_ATTRIBUTE, value: inputValue, onMouseDown: _util.preventDefaultEvent, key: inputValue }, inputValue)); } } } var loading = _this.props.loading; if (typeof loading === 'boolean') { loading = { spinning: loading }; } if (!options.length && notFoundContent) { options = [/*#__PURE__*/_react["default"].createElement(_menu.Item, { style: _util.UNSELECTABLE_STYLE, attribute: _util.UNSELECTABLE_ATTRIBUTE, disabled: true, role: "option", value: "NOT_FOUND", key: "NOT_FOUND", checkable: false }, loading.spinning ? '' : notFoundContent)]; } return options; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderFilterOptionsFromChildren", function (children, childrenKeys, menuItems) { var sel = []; var props = _this.props; var inputValue = _this.state.inputValue; var tags = props.tags; _react.Children.forEach(children, function (child, index) { if (!child) { return; } if (child.type.isSelectOptGroup) { var innerItems = _this.renderFilterOptionsFromChildren(child.props.children, childrenKeys, menuItems); if (innerItems.length) { var label = child.props.label; var key = child.key; if (!key && typeof label === 'string') { key = label; } else if (!label && key) { label = key; } sel.push( /*#__PURE__*/_react["default"].createElement(_menu.ItemGroup, { key: key, title: label }, innerItems)); } return; } (0, _warning["default"])(child.type.isSelectOption, 'the children of `Select` should be `Select.Option` or `Select.OptGroup`, ' + "instead of `".concat(child.type.name || child.type.displayName || child.type, "`.")); var childValue = (0, _util.getValuePropValue)(child); (0, _util.validateOptionValue)(childValue, _this.props); if (_this.filterOption(inputValue, child)) { var menuItem = /*#__PURE__*/_react["default"].createElement(_menu.Item, (0, _extends2["default"])({ checkable: props.dropdownMenuItemCheckable, style: _util.UNSELECTABLE_STYLE, attribute: _util.UNSELECTABLE_ATTRIBUTE, value: childValue, key: childValue, role: "option" }, child.props)); sel.push(menuItem); menuItems.push(menuItem); } if (tags && !child.props.disabled) { childrenKeys.push(childValue); } }); return sel; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "isChoiceRemove", function (selectedKey) { var choiceRemove = _this.props.choiceRemove; if (typeof choiceRemove === 'function') { return choiceRemove(selectedKey); } return choiceRemove; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderTopControlNode", function (isFloatLabel, floatLabel) { var _this$state = _this.state, value = _this$state.value, open = _this$state.open, inputValue = _this$state.inputValue; var props = _this.props; var tags = (0, _util.isTags)(props); var choiceTransitionName = props.choiceTransitionName, prefixCls = props.prefixCls, maxTagTextLength = props.maxTagTextLength, maxTagCount = props.maxTagCount, maxTagPlaceholder = props.maxTagPlaceholder, showSearch = props.showSearch, choiceRender = props.choiceRender, removeIcon = props.removeIcon; var className = "".concat(prefixCls, "-selection__rendered"); // search input is inside topControlNode in single, multiple & combobox. 2016/04/13 var innerNode = null; if ((0, _util.isSingleMode)(props)) { var selectedValue = null; if (value.length) { var showSelectedValue = false; var opacity = 1; if (!showSearch) { showSelectedValue = true; } else { if (open) { showSelectedValue = !inputValue; if (showSelectedValue) { opacity = 0.4; } } else { showSelectedValue = true; } } var singleValue = value && value[0]; var _this$getOptionInfoBy3 = _this.getOptionInfoBySingleValue(singleValue), label = _this$getOptionInfoBy3.label, title = _this$getOptionInfoBy3.title; selectedValue = /*#__PURE__*/_react["default"].createElement("div", { key: "value", className: "".concat(prefixCls, "-selection-selected-value"), title: (0, _util.toTitle)(title || label), style: { display: showSelectedValue ? 'block' : 'none', opacity: opacity } }, choiceRender ? choiceRender(label) : label); } else { selectedValue = /*#__PURE__*/_react["default"].createElement("div", { key: "value", className: "".concat(prefixCls, "-selection-selected-value") }); } if (!showSearch) { innerNode = [selectedValue]; } else { innerNode = [selectedValue, /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefixCls, "-search ").concat(prefixCls, "-search--inline"), key: "input", style: { display: open ? 'block' : 'none' } }, _this.getInputElement())]; } } else { var selectedValueNodes = []; var limitedCountValue = value; var maxTagPlaceholderEl; if (maxTagCount !== undefined && value.length > maxTagCount) { limitedCountValue = limitedCountValue.slice(0, maxTagCount); var omittedValues = _this.getVLForOnChange(value.slice(maxTagCount, value.length)); var content = "+ ".concat(value.length - maxTagCount, " ..."); if (maxTagPlaceholder) { content = typeof maxTagPlaceholder === 'function' ? maxTagPlaceholder(omittedValues) : maxTagPlaceholder; } maxTagPlaceholderEl = /*#__PURE__*/_react["default"].createElement("li", (0, _extends2["default"])({ style: _util.UNSELECTABLE_STYLE }, _util.UNSELECTABLE_ATTRIBUTE, { onMouseDown: _util.preventDefaultEvent, className: "".concat(prefixCls, "-selection__choice ").concat(prefixCls, "-selection__choice__disabled ").concat(prefixCls, "-selection__max"), key: 'maxTagPlaceholder', title: (0, _util.toTitle)(content) }), /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefixCls, "-selection__choice__content") }, content)); } if ((0, _util.isMultipleOrTags)(props)) { selectedValueNodes = limitedCountValue.map(function (singleValue, index) { var info = _this.getOptionInfoBySingleValue(singleValue); var content = info.label; var title = info.title || content; if (maxTagTextLength && typeof content === 'string' && content.length > maxTagTextLength) { content = "".concat(content.slice(0, maxTagTextLength), "..."); } var disabled = _this.isChildDisabled(singleValue); var choiceClassName = disabled ? "".concat(prefixCls, "-selection__choice ").concat(prefixCls, "-selection__choice__disabled") : "".concat(prefixCls, "-selection__choice"); var li = /*#__PURE__*/_react["default"].createElement("li", (0, _extends2["default"])({ style: _util.UNSELECTABLE_STYLE }, _util.UNSELECTABLE_ATTRIBUTE, { onMouseDown: _util.preventDefaultEvent, onClick: _this.handleChoiceItemClick.bind((0, _assertThisInitialized2["default"])(_this), singleValue), className: choiceClassName, key: singleValue, title: (0, _util.toTitle)(title) }), /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefixCls, "-selection__choice__content") }, content), disabled || !_this.isChoiceRemove(singleValue) ? null : /*#__PURE__*/_react["default"].createElement("span", { className: "".concat(prefixCls, "-selection__choice__remove"), onClick: _this.removeSelected.bind((0, _assertThisInitialized2["default"])(_this), singleValue, index) }, removeIcon || /*#__PURE__*/_react["default"].createElement("i", { className: "icon icon-cancel" }))); return /*#__PURE__*/_react["default"].createElement(_ripple["default"], { key: singleValue + limitedCountValue.slice(0, index).filter(function (foundValue) { return foundValue === singleValue; }).length }, choiceRender ? choiceRender(li, singleValue) : li); }); } if (maxTagPlaceholderEl) { selectedValueNodes.push(maxTagPlaceholderEl); } selectedValueNodes.push( /*#__PURE__*/_react["default"].createElement("li", { className: "".concat(prefixCls, "-search ").concat(prefixCls, "-search--inline"), key: "__input" }, _this.getInputElement())); if ((0, _util.isMultipleOrTags)(props) && choiceTransitionName) { innerNode = /*#__PURE__*/_react["default"].createElement(_animate["default"], { onLeave: _this.onChoiceAnimationLeave, component: "ul", transitionName: choiceTransitionName }, selectedValueNodes); } else { innerNode = /*#__PURE__*/_react["default"].createElement("ul", null, selectedValueNodes); } } return /*#__PURE__*/_react["default"].createElement("div", { className: className, ref: _this.saveTopCtrlRef }, _this.getPlaceholderElement(floatLabel), innerNode, isFloatLabel && _this.renderClear(isFloatLabel), isFloatLabel && (tags || !props.showArrow ? null : /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({ key: "arrow", className: "".concat(prefixCls, "-arrow"), style: _util.UNSELECTABLE_STYLE }, _util.UNSELECTABLE_ATTRIBUTE, { onClick: _this.onArrowClick }), /*#__PURE__*/_react["default"].createElement("i", { className: "icon icon-arrow_drop_down" }), /*#__PURE__*/_react["default"].createElement("b", null)))); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "checkAll", function (event) { var name = event.target.getAttribute('name'); var props = _this.props; var state = _this.state; if (props.disabled) { return; } var newValues; var values = Select.getOptionsFromChildren(props.children).filter(function (option) { // 当这个选项为禁用时,全选和无不对这个选项做处理 return option.props.disabled !== true; }).map(function (option) { return (0, _util.getValuePropValue)(option); }); if (name === 'check-all') { newValues = new Set(state.value.concat(values)); _this.fireChange(Array.from(newValues)); } else if (name === 'check-none') { newValues = state.value.filter(function (e) { return values.indexOf(e) < 0; }); _this.fireChange(newValues); _this.focus(); } }); var _value = Select.getValueFromProps(_props, true); var _optionsInfo = Select.getOptionsInfoFromProps(_props); var filterValue = _props.filterValue || ''; _this.state = { value: _value, inputValue: _props.combobox ? Select.getInputValueForCombobox(_props, _optionsInfo, true // use default value ) : '', open: _props.defaultOpen, optionsInfo: _optionsInfo, filterValue: filterValue, // a flag for aviod redundant getOptionsInfoFromProps call skipBuildOptionsInfo: true }; _this.adjustOpenState(); _this.saveInputRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'inputRef'); _this.saveInputMirrorRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'inputMirrorRef'); _this.saveTopCtrlRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'topCtrlRef'); _this.saveSelectTriggerRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'selectTriggerRef'); _this.saveRootRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'rootRef'); _this.saveSelectionRef = (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'selectionRef'); return _this; } (0, _createClass2["default"])(Select, [{ key: "componentDidMount", value: function componentDidMount() { if (this.props.autoFocus) { this.focus(); } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if ((0, _util.isMultipleOrTags)(this.props)) { var inputNode = this.getInputDOMNode(); var mirrorNode = this.getInputMirrorDOMNode(); if (inputNode.value) { inputNode.style.width = ''; inputNode.style.width = "".concat(mirrorNode.clientWidth, "px"); } else { inputNode.style.width = ''; } this.onChoiceAnimationLeave(); } this.forcePopupAlign(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.clearFocusTime(); this.clearBlurTime(); this.clearAdjustTimer(); if (this.dropdownContainer) { (0, _reactDom.unmountComponentAtNode)(this.dropdownContainer); document.body.removeChild(this.dropdownContainer); this.dropdownContainer = null; } } }, { key: "getBuiltinPlacements", value: function getBuiltinPlacements() { var builtinPlacements = this.props.builtinPlacements; if (builtinPlacements) { return builtinPlacements; } if (!(0, _util.isTags)(this.props) && !(0, _util.isCombobox)(this.props)) { return BUILT_IN_PLACEMENTS; } } }, { key: "renderClear", value: function renderClear(isFloatLabel) { var _this$props5 = this.props, prefixCls = _this$props5.prefixCls, allowClear = _this$props5.allowClear, clearIcon = _this$props5.clearIcon; var _this$state2 = this.state, value = _this$state2.value, inputValue = _this$state2.inputValue; var Cmp = isFloatLabel ? _button["default"] : 'span'; var clear = /*#__PURE__*/_react["default"].createElement(Cmp, (0, _extends2["default"])({ key: "clear", className: isFloatLabel ? "".concat(prefixCls, "-clear") : "".concat(prefixCls, "-selection__clear"), style: _util.UNSELECTABLE_STYLE }, _util.UNSELECTABLE_ATTRIBUTE, { shape: "circle", icon: clearIcon ? undefined : 'close', size: "small", onClick: this.onClearSelection, onMouseDown: _util.preventDefaultEvent }), clearIcon); if (!allowClear) { return null; } if ((0, _util.isCombobox)(this.props)) { if (inputValue) { return clear; } return null; } if (inputValue || value.length) { return clear; } return null; } }, { key: "renderFloatLabel", value: function renderFloatLabel() { var label = this.props.label; var _this$props6 = this.props, prefixCls = _this$props6.prefixCls, border = _this$props6.border; if (label && border) { return /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefixCls, "-label-wrapper")