choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
1,508 lines (1,231 loc) • 57.7 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _classnames2 = _interopRequireDefault(require("classnames"));
var _componentClasses = _interopRequireDefault(require("component-classes"));
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 _button = _interopRequireDefault(require("../../button"));
var _ripple = _interopRequireDefault(require("../../ripple"));
var _util = require("./util");
var _SelectTrigger = _interopRequireDefault(require("./SelectTrigger"));
var _PropTypes = require("./PropTypes");
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 = (0, _getPrototypeOf2["default"])(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return (0, _possibleConstructorReturn2["default"])(this, result);
};
}
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', 'tl'],
offset: [0, 4],
overflow: {
adjustX: 0,
adjustY: 1
}
},
topLeft: {
points: ['bl', 'bl'],
offset: [0, -4],
overflow: {
adjustX: 0,
adjustY: 1
}
},
bottomRight: {
points: ['tr', 'tr'],
offset: [0, 4],
overflow: {
adjustX: 0,
adjustY: 1
}
}
};
var BUILT_IN_PLACEMENTS_LABEL = {
bottomLeft: {
points: ['tl', 'tl'],
offset: [0, 19],
overflow: {
adjustX: 0,
adjustY: 1
}
},
topLeft: {
points: ['bl', 'bl'],
offset: [0, -4],
overflow: {
adjustX: 0,
adjustY: 1
}
},
bottomRight: {
points: ['tr', 'tr'],
offset: [0, 19],
overflow: {
adjustX: 0,
adjustY: 1
}
}
};
var Select =
/*#__PURE__*/
function (_Component) {
(0, _inherits2["default"])(Select, _Component);
var _super = _createSuper(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), "componentWillReceiveProps", function (nextProps) {
var optionsInfo = _this.getOptionsInfoFromProps(nextProps);
_this.setState({
optionsInfo: optionsInfo
});
if ('value' in nextProps) {
var value = _this.getValueFromProps(nextProps);
_this.setState({
value: value
});
if (nextProps.combobox) {
_this.setState({
inputValue: value.length ? _this.getLabelBySingleValue(value[0], optionsInfo) : ''
});
}
}
if ('filterValue' in nextProps) {
_this.setState({
filterValue: nextProps.filterValue
});
}
});
(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), "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), "onInputChange", function (event) {
var val = event.target.value;
_this.onInputValueChange(val);
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onDropdownVisibleChange", function (open) {
var filter = _this.props.filter;
if (_this.needExpand) {
if (open && !_this._focused) {
_this.clearBlurTime();
_this.timeoutFocus();
_this._focused = true;
_this.updateFocusClassName();
}
if (filter) {
_this.onFilterChange('');
}
if (open && filter) {
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"].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;
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 (!_this.props.filter) {
_this.setInputValue(inputValue, true);
}
});
(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), "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._focused) {
return;
}
_this._focused = true;
_this.updateFocusClassName(); // 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._focused = false;
_this.updateFocusClassName();
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 = [firstOption.key];
_this.fireChange(value);
}
}
} else if ((0, _util.isMultipleOrTags)(props) && inputValue) {
if (_this.props.blurChange) {
// why not use setState?
_this.state.inputValue = _this.getInputDOMNode().value = '';
}
value = _this.getValueByInput(inputValue);
if (value !== undefined && _this.props.blurChange) {
_this.fireChange(value);
}
}
props.onBlur(_this.getVLForOnChange(value));
}, 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.selectTriggerRef.triggerRef.forcePopupAlign();
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getValueFromProps", function (props) {
var value = [];
if ('value' in props) {
value = (0, _util.toArray)(props.value);
} else {
value = (0, _util.toArray)(props.defaultValue);
}
if (props.labelInValue) {
value = value.map(function (v) {
return v.key;
});
}
if ((0, _util.isMultiple)(props)) {
value = value.filter(function (v) {
return !!v;
});
}
return value;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getOptionsInfoFromProps", function (props) {
var options = _this.getOptionsFromChildren(props.children);
var oldOptionsInfo = _this.state ? _this.state.optionsInfo : {};
var value = _this.state ? _this.state.value : [];
var optionsInfo = {};
options.forEach(function (option) {
var singleValue = (0, _util.getValuePropValue)(option);
optionsInfo[(0, _util.getMapKey)(singleValue)] = {
option: option,
value: singleValue,
label: _this.getLabelFromOption(option),
title: option.props.title
};
});
value.forEach(function (v) {
var key = (0, _util.getMapKey)(v);
if (!optionsInfo[key]) {
optionsInfo[key] = oldOptionsInfo[key];
}
});
return optionsInfo;
});
(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: _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), "getOptionsFromChildren", function (children) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
_react.Children.forEach(children, function (child) {
if (!child) {
return;
}
if (child.type.isSelectOptGroup) {
_this.getOptionsFromChildren(child.props.children, options);
} else {
options.push(child);
}
});
return options;
});
(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), "getLabelFromOption", function (option) {
return (0, _util.getPropValue)(option, _this.props.optionLabelProp);
});
(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 () {
var _this$props2 = _this.props,
placeholder = _this$props2.placeholder,
prefixCls = _this$props2.prefixCls,
border = _this$props2.border;
if (!border && placeholder) {
return _react["default"].createElement("div", {
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() : _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 _react["default"].createElement("div", {
className: "".concat(props.prefixCls, "-search__field__wrap")
}, (0, _react.cloneElement)(inputElement, {
ref: (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'inputRef'),
onChange: _this.onInputChange,
onKeyDown: chaining(_this.onInputKeyDown, inputElement.props.onKeyDown, _this.props.onInputKeyDown),
value: _this.state.inputValue,
disabled: props.disabled,
className: inputCls
}), _react["default"].createElement("span", {
ref: (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'inputMirrorRef'),
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 _assertThisInitialize2 = (0, _assertThisInitialized2["default"])(_this),
props = _assertThisInitialize2.props,
state = _assertThisInitialize2.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('');
}
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
});
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 (state) {
var _open = _this.props.open;
if (typeof _open === 'boolean') {
return _open;
}
var open = (state || _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), "updateFocusClassName", function () {
var _assertThisInitialize3 = (0, _assertThisInitialized2["default"])(_this),
rootRef = _assertThisInitialize3.rootRef,
props = _assertThisInitialize3.props; // avoid setState and its side effect
if (_this._focused) {
(0, _componentClasses["default"])(rootRef).add("".concat(props.prefixCls, "-focused"));
} else {
(0, _componentClasses["default"])(rootRef).remove("".concat(props.prefixCls, "-focused"));
}
});
(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._focused = true;
}
} else {
if (activeElement !== _this.selectionRef) {
_this.selectionRef.focus();
_this._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();
}
var props = _this.props;
if (props.disabled || _this.isChildDisabled(selectedKey)) {
return;
}
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
});
}
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), "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;
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 = _react["default"].createElement(_menu.Item, {
style: _util.UNSELECTABLE_STYLE,
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(_react["default"].createElement(_menu.Item, {
style: _util.UNSELECTABLE_STYLE,
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 = [_react["default"].createElement(_menu.Item, {
style: _util.UNSELECTABLE_STYLE,
attribute: _util.UNSELECTABLE_ATTRIBUTE,
disabled: true,
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 = "__RC_SELECT_GRP__".concat(child.key === null ? index : child.key, "__"); // if (!key && typeof label === 'string') {
// key = label;
// }
// else if (!label && key) {
// label = key;
// }
sel.push(_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 = _react["default"].createElement(_menu.Item, (0, _extends2["default"])({
style: _util.UNSELECTABLE_STYLE,
attribute: _util.UNSELECTABLE_ATTRIBUTE,
value: childValue,
key: childValue
}, 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 () {
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 prefixCls = props.prefixCls,
maxTagTextLength = props.maxTagTextLength,
maxTagCount = props.maxTagCount,
maxTagPlaceholder = props.maxTagPlaceholder,
showSearch = props.showSearch,
choiceRender = props.choiceRender;
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;
var opacity = 1;
if (value.length) {
var showSelectedValue = false;
if (showSearch && open) {
showSelectedValue = !inputValue;
if (showSelectedValue) {
opacity = 0.4;
}
}
}
var singleValue = value && value[0];
var _this$getOptionInfoBy3 = _this.getOptionInfoBySingleValue(singleValue),
label = _this$getOptionInfoBy3.label,
title = _this$getOptionInfoBy3.title;
selectedValue = _react["default"].createElement("div", {
key: "value",
className: "".concat(prefixCls, "-selection-selected-value"),
title: title || label,
style: {
opacity: opacity
}
}, choiceRender ? choiceRender(label) : label);
innerNode = [selectedValue];
} 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 = _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={content}
}), _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 = _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 // title={title}
}), _react["default"].createElement("div", {
className: "".concat(prefixCls, "-selection__choice__content")
}, content), disabled || !_this.isChoiceRemove(singleValue) ? null : _react["default"].createElement("span", {
className: "".concat(prefixCls, "-selection__choice__remove"),
onClick: _this.removeSelected.bind((0, _assertThisInitialized2["default"])(_this), singleValue, index)
}, _react["default"].createElement("i", {
className: "icon icon-cancel"
})));
return _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(_react["default"].createElement("li", {
className: "".concat(prefixCls, "-search ").concat(prefixCls, "-search--inline"),
key: "__input"
}, _this.getInputElement()));
} else {
selectedValueNodes.push(_react["default"].createElement("div", {
className: "".concat(prefixCls, "-search ").concat(prefixCls, "-search--inline"),
key: "__input",
style: {
display: 'inline-block'
}
}, _this.getInputElement()));
}
if ((0, _util.isMultipleOrTags)(props)) {
innerNode = _react["default"].createElement("ul", null, selectedValueNodes);
} else {
innerNode = selectedValueNodes;
}
}
return _react["default"].createElement("div", {
className: className,
ref: (0, _util.saveRef)((0, _assertThisInitialized2["default"])(_this), 'topCtrlRef')
}, _this.getPlaceholderElement(), innerNode, _this.renderClear(), tags || !props.showArrow ? null : _react["default"].createElement("span", (0, _extends2["default"])({
key: "arrow",
className: "".concat(prefixCls, "-arrow"),
style: _util.UNSELECTABLE_STYLE
}, _util.UNSELECTABLE_ATTRIBUTE, {
onClick: _this.onArrowClick
}), _react["default"].createElement("i", {
className: "icon icon-arrow_drop_down"
}), _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 = _this.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 = _this.getValueFromProps(_props);
var _optionsInfo = _this.getOptionsInfoFromProps(_props);
var _inputValue = '';
if (_props.combobox) {
_inputValue = _value.length ? _this.getLabelBySingleValue(_value[0], _optionsInfo) : '';
}
var _open2 = _props.open;
if (_open2 === undefined) {
_open2 = _props.defaultOpen;
}
var filterValue = _props.filterValue || '';
_this.state = {
value: _value,
inputValue: _inputValue,
open: _open2,
optionsInfo: _optionsInfo,
filterValue: filterValue
};
_this.adjustOpenState();
return _this;
}
(0, _createClass2["default"])(Select, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.autoFocus) {
this.focus();
}
}
}, {
key: "componentWillUpdate",
value: function componentWillUpdate(nextProps, nextState) {
this.props = nextProps;
this.state = nextState;
this.adjustOpenState();
}
}, {
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();
}
}
}, {
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 _this$props5 = this.props,
builtinPlacements = _this$props5.builtinPlacements,
label = _this$props5.label;
if (builtinPlacements) {
return builtinPlacements;
}
if (!(0, _util.isTags)(this.props) && !(0, _util.isCombobox)(this.props)) {
if (label) {
return BUILT_IN_PLACEMENTS_LABEL;
}
return BUILT_IN_PLACEMENTS;
}
}
}, {
key: "renderClear",
value: function renderClear() {
var _this$props6 = this.props,
prefixCls = _this$props6.prefixCls,
allowClear = _this$props6.allowClear;
var _this$state2 = this.state,
value = _this$state2.value,
inputValue = _this$state2.inputValue;
var clear = _react["default"].createElement(_button["defau