@beisen-phoenix/select
Version:
## 概述
449 lines (374 loc) • 14.3 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es6.object.assign");
require("core-js/modules/es7.array.includes");
require("core-js/modules/es6.string.includes");
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _tooltip = _interopRequireDefault(require("@beisen-phoenix/tooltip"));
var _SwitchArrow = _interopRequireDefault(require("./SwitchArrow"));
var _DelnputTxtIcon = _interopRequireDefault(require("./DelnputTxtIcon"));
var _Tags = _interopRequireDefault(require("./Tags"));
var _langs = _interopRequireDefault(require("../langs"));
var _StyledSelect = require("./StyledSelect");
var Select =
/*#__PURE__*/
function (_React$PureComponent) {
(0, _inherits2.default)(Select, _React$PureComponent);
function Select(props) {
var _this;
(0, _classCallCheck2.default)(this, Select);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Select).call(this, props));
_this.inputRef = _react.default.createRef();
_this.hiddenSpanRef = _react.default.createRef();
_this.ulRef = _react.default.createRef();
_this.emitPropsChangeMethod = function () {
var onChange = _this.props.onChange;
var _this$state = _this.state,
value = _this$state.value,
searchWord = _this$state.searchWord;
onChange && onChange({
value: value,
searchWord: searchWord
});
};
_this.handleValue = function () {
var isMultiSelect = _this.props.isMultiSelect;
var value = _this.state.value;
return Array.isArray(value) && isMultiSelect ? value : '';
};
_this.isShowToolTip = function () {
var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this),
hiddenSpanRef = _assertThisInitialize.hiddenSpanRef,
inputRef = _assertThisInitialize.inputRef;
if (hiddenSpanRef.current && inputRef.current && !_this.state.isFocus) {
var spanCurrent = hiddenSpanRef.current;
var inputCurrent = inputRef.current;
if (inputCurrent.clientWidth < spanCurrent.clientWidth) {
_this.setState({
isHasTooltip: true
});
return;
}
}
_this.state.isHasTooltip && _this.setState({
isHasTooltip: false
});
};
_this.inputing = function (evt) {
var searchWord = evt.target.value;
var _this$props = _this.props,
isSearch = _this$props.isSearch,
onSearch = _this$props.onSearch,
readOnly = _this$props.readOnly;
if (!isSearch || readOnly) return;
_this.setState({
searchWord: searchWord,
inputValue: searchWord // value: this.handleValue()
}, function () {
_this.inputRef.current.style.width = "".concat(_this.handleInputWidth(), "px");
});
onSearch && onSearch(searchWord);
};
_this.onBlur = function (e) {
_this.setState({
isFocus: false,
visibleTxt: true,
inputValue: '',
searchWord: '',
value: _this.clickDelet ? '' : _this.props.value
}, function () {
_this.clickDelet = false;
if (_this.props.isMultiSelect) {
_this.inputRef && _this.inputRef.current && (_this.inputRef.current.style.width = "10px");
}
});
};
_this.onKeyDown = function (e) {
var keyCode = e.keyCode,
target = e.target;
if (keyCode === 8 && !target.value) {
_this.removeItem(-1);
}
};
_this.onWrapperClick = function (e) {
var _this$props2 = _this.props,
disabled = _this$props2.disabled,
isSearch = _this$props2.isSearch,
isMultiSelect = _this$props2.isMultiSelect;
if (disabled) return;
_this.setState({
visibleTxt: false
});
if (_this.inputRef && _this.inputRef.current && isSearch) {
var _value = isMultiSelect ? _this.state.value : '';
_this.setState({
isFocus: true,
value: _value
});
_this.inputRef.current.focus();
}
e.nativeEvent.stopImmediatePropagation();
};
_this.onWrapperMouseEnter = function () {
_this.setState({
hover: true
});
};
_this.onWrapperMouseLeave = function () {
_this.setState({
hover: false
});
};
_this.deleteTxt = function (e) {
var _this$props3 = _this.props,
onDelete = _this$props3.onDelete,
isMultiSelect = _this$props3.isMultiSelect,
isSearch = _this$props3.isSearch,
onSearch = _this$props3.onSearch;
if (!isMultiSelect && isSearch && _this.state.isFocus) {
_this.onWrapperClick(e);
_this.setState({
inputValue: '',
searchWord: ''
}, function () {
onSearch && onSearch('');
});
return;
}
_this.clickDelet = true;
_this.setState({
inputValue: '',
value: _this.handleValue()
}, _this.emitPropsChangeMethod);
onDelete && onDelete('');
};
_this.removeItem = function (item) {
var value = _this.state.value;
var onDelete = _this.props.onDelete;
if (Array.isArray(value)) {
var copied = (0, _toConsumableArray2.default)(value);
if (typeof item === 'number' && !isNaN(item)) {
var temp = copied[item] || copied[copied.length - 1];
!temp.disabled && copied.splice(item, 1);
} else if ((0, _typeof2.default)(item) === 'object' && copied.includes(item) && !item.disabled) {
copied.splice(copied.indexOf(item), 1);
}
if (copied.length !== value.length) {
_this.setState({
value: copied
}, _this.emitPropsChangeMethod);
onDelete && onDelete(copied);
}
}
};
_this.renderMutiItems = function () {
var _this$props4 = _this.props,
isMultiSelect = _this$props4.isMultiSelect,
disabled = _this$props4.disabled,
size = _this$props4.size;
var value = _this.state.value;
if (Array.isArray(value) && isMultiSelect) {
return value.map(function (item) {
return _react.default.createElement(_Tags.default, {
selectDisabled: disabled,
key: item.value,
item: item,
onClick: _this.removeItem,
size: size
});
});
}
};
_this.renderTxt = function () {
var _this$props5 = _this.props,
size = _this$props5.size,
disabled = _this$props5.disabled,
isSearch = _this$props5.isSearch;
var _this$state2 = _this.state,
isFocus = _this$state2.isFocus,
value = _this$state2.value,
isHasTooltip = _this$state2.isHasTooltip,
inputValue = _this$state2.inputValue,
visibleTxt = _this$state2.visibleTxt,
hover = _this$state2.hover;
var tempProps = {
isFocus: isFocus,
size: size,
disabled: disabled,
isSearch: isSearch,
visibleTxt: visibleTxt
};
if (typeof value === 'string' && value.length) {
if (inputValue) return null;
var TempComponent = _react.default.createElement(_StyledSelect.StyledSpan, Object.assign({}, tempProps), _react.default.createElement(_StyledSelect.TipsSapn, null, value));
return value && isHasTooltip ? _react.default.createElement(_tooltip.default, {
title: value,
visible: hover && !isFocus
}, TempComponent) : TempComponent;
}
};
_this.handleInputWidth = function () {
if (_this.props.isMultiSelect) {
var _assertThisInitialize2 = (0, _assertThisInitialized2.default)(_this),
hiddenSpanRef = _assertThisInitialize2.hiddenSpanRef;
if (hiddenSpanRef.current) {
var current = hiddenSpanRef.current;
return current.clientWidth || 10;
}
return 10;
}
};
_this.switchDropDown = function () {// this.emitPropsChangeMethod();
};
_this.isShowPlaceHolder = function () {
var _this$state3 = _this.state,
inputValue = _this$state3.inputValue,
value = _this$state3.value;
if (inputValue) return;
if (value && typeof value === 'string') return;
if (Array.isArray(value) && value.length) return;
return true;
};
_this.getInputValue = function () {
var _this$state4 = _this.state,
inputValue = _this$state4.inputValue,
value = _this$state4.value;
if (inputValue) return inputValue;
if (value && typeof value === 'string' && !_this.props.disabled) {
return value;
}
return '';
};
var value = _this.props.value;
_this.state = {
value: value,
inputValue: '',
isFocus: false,
isHasTooltip: false,
visibleTxt: true,
hover: false,
searchWord: '',
inputWidth: 10
};
return _this;
}
(0, _createClass2.default)(Select, [{
key: "componentDidMount",
value: function componentDidMount() {
this.isShowToolTip();
document.addEventListener('click', this.onBlur);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
'value' in nextProps && this.props.value !== nextProps.value && this.setState({
value: nextProps.value
});
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
this.isShowToolTip();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
document.removeEventListener('click', this.onBlur);
}
}, {
key: "getDeleteIconState",
value: function getDeleteIconState() {
var _this$state5 = this.state,
inputValue = _this$state5.inputValue,
value = _this$state5.value;
var _this$props6 = this.props,
disabled = _this$props6.disabled,
readOnly = _this$props6.readOnly,
showDelete = _this$props6.showDelete,
isMultiSelect = _this$props6.isMultiSelect;
if (!showDelete || isMultiSelect) return false;
if (disabled || readOnly) return false;
if (inputValue) return true;
if (value && typeof value === 'string') return true;
}
}, {
key: "render",
value: function render() {
var _this$props7 = this.props,
placeHolder = _this$props7.placeHolder,
size = _this$props7.size,
disabled = _this$props7.disabled,
isMultiSelect = _this$props7.isMultiSelect,
isSearch = _this$props7.isSearch,
error = _this$props7.error,
isActive = _this$props7.isActive,
_this$props7$translat = _this$props7.translation,
translation = _this$props7$translat === void 0 ? _langs.default : _this$props7$translat;
var _this$state6 = this.state,
inputValue = _this$state6.inputValue,
value = _this$state6.value,
isFocus = _this$state6.isFocus;
var multiValueLength = isMultiSelect && Array.isArray(value) ? value.length : void 0;
var isShowDeleteIcon = this.getDeleteIconState();
var commonProps = {
disabled: disabled,
isMultiSelect: isMultiSelect,
size: size,
isSearch: isSearch,
isFocus: isFocus,
multiValueLength: multiValueLength,
isShowDeleteIcon: isShowDeleteIcon,
isActive: isActive
};
var hideInput = disabled && isMultiSelect;
return _react.default.createElement(_StyledSelect.Wrapper, Object.assign({}, commonProps, {
onMouseEnter: this.onWrapperMouseEnter,
onMouseLeave: this.onWrapperMouseLeave,
onClick: this.onWrapperClick,
inputValue: inputValue,
error: error
}), _react.default.createElement(_StyledSelect.StyledHiddenSpan, {
ref: this.hiddenSpanRef
}, inputValue || (typeof value === 'string' ? value : ' ')), _react.default.createElement(_StyledSelect.PlaceHolder, Object.assign({}, commonProps, {
isShowPlaceHolder: this.isShowPlaceHolder()
}), this.props.value && !isMultiSelect ? this.props.value : placeHolder || translation.placeHolder), _react.default.createElement(_StyledSelect.StyledUl, Object.assign({}, commonProps, {
ref: this.ulRef
}), this.renderMutiItems(), !hideInput && _react.default.createElement(_StyledSelect.InputWrapper, Object.assign({}, commonProps), _react.default.createElement(_StyledSelect.StyledInput, Object.assign({}, commonProps, {
onChange: this.inputing,
value: this.getInputValue(),
propsValue: value,
ref: this.inputRef,
onKeyDown: this.onKeyDown
}))), this.renderTxt()), _react.default.createElement(_SwitchArrow.default, Object.assign({}, commonProps, {
onClick: this.switchDropDown
})), _react.default.createElement(_DelnputTxtIcon.default, {
onClick: this.deleteTxt
}));
}
}]);
return Select;
}(_react.default.PureComponent);
exports.default = Select;
Select.defaultProps = {
size: 'normal',
disabled: false,
isMultiSelect: false,
placeHolder: '',
isSearch: false,
readOnly: false,
isActive: false,
showDelete: true,
translation: _langs.default
};