@zohodesk/components
Version:
Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development
1,073 lines (931 loc) • 62.8 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.MultiSelectComponent = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = require("./props/propTypes");
var _defaultProps = require("./props/defaultProps");
var _defaultProps2 = require("./MobileHeader/props/defaultProps");
var _constants = require("./constants");
var _Popup = _interopRequireDefault(require("../Popup/Popup"));
var _TextBoxIcon = _interopRequireDefault(require("../TextBoxIcon/TextBoxIcon"));
var _Layout = require("../Layout");
var _Card = _interopRequireWildcard(require("../Card/Card"));
var _SelectedOptions = _interopRequireDefault(require("./SelectedOptions"));
var _Suggestions = _interopRequireDefault(require("./Suggestions"));
var _EmptyState = _interopRequireDefault(require("./EmptyState"));
var _CssProvider = _interopRequireDefault(require("../Provider/CssProvider"));
var _IdProvider = require("../Provider/IdProvider");
var _ResponsiveDropBox = _interopRequireDefault(require("../ResponsiveDropBox/ResponsiveDropBox"));
var _MultiSelectHeader = _interopRequireDefault(require("./MultiSelectHeader"));
var _Loader = _interopRequireDefault(require("@zohodesk/svg/lib/Loader/Loader"));
var _isMobilePopover = _interopRequireDefault(require("../DropBox/utils/isMobilePopover"));
var _icons = require("@zohodesk/icons");
var _MultiSelectModule = _interopRequireDefault(require("./MultiSelect.module.css"));
var _Common = require("../utils/Common.js");
var _dropDownUtils = require("../utils/dropDownUtils");
var _MobileHeader = _interopRequireDefault(require("./MobileHeader/MobileHeader"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/* eslint-disable react/forbid-component-props */
/* eslint-disable react/no-unused-prop-types */
var dummyArray = [];
var a11yProps = {
role: 'option'
};
var MultiSelectComponent = /*#__PURE__*/function (_React$Component) {
_inherits(MultiSelectComponent, _React$Component);
var _super = _createSuper(MultiSelectComponent);
function MultiSelectComponent(props) {
var _this;
_classCallCheck(this, MultiSelectComponent);
_this = _super.call(this, props);
_this.getNextAriaId = (0, _IdProvider.getUniqueId)(_assertThisInitialized(_this));
_this.getFilterSuggestions = (0, _dropDownUtils.makeGetMultiSelectFilterSuggestions)();
_this.formatOptions = (0, _dropDownUtils.makeFormatOptions)();
_this.getSelectedOptions = (0, _dropDownUtils.makeGetMultiSelectSelectedOptions)();
_this.getIsShowClearIcon = (0, _dropDownUtils.makeGetIsShowClearIcon)(); //Use in AdvancedMultiSelect component
_this.objectConcat = (0, _dropDownUtils.makeObjectConcat)();
_this.formatSelectedOptions = (0, _dropDownUtils.makeFormatOptions)();
var _this$props = _this.props,
selectedOptions = _this$props.selectedOptions,
_this$props$searchDeb = _this$props.searchDebounceTime,
searchDebounceTime = _this$props$searchDeb === void 0 ? 500 : _this$props$searchDeb;
var _this$handleFormatOpt = _this.handleFormatOptions(_this.props),
allOptions = _this$handleFormatOpt.allOptions,
normalizedAllOptions = _this$handleFormatOpt.normalizedAllOptions,
normalizedFormatOptions = _this$handleFormatOpt.normalizedFormatOptions;
var _this$handleGetSelect = _this.handleGetSelectedOptions(selectedOptions, normalizedFormatOptions, _this.props),
formatSelectedOptions = _this$handleGetSelect.formatSelectedOptions,
normalizedSelectedOptions = _this$handleGetSelect.normalizedSelectedOptions,
selectedOptionIds = _this$handleGetSelect.selectedOptionIds;
_this.state = {
options: allOptions,
optionsNormalize: normalizedAllOptions,
selectedOptions: formatSelectedOptions,
selectedOptionIds: selectedOptionIds,
searchStr: '',
hoverOption: 0,
isFetchingOptions: false,
highLightedSelectOptions: [],
lastHighLightedSelectOption: '',
shiftKeyPressHighLighted: 0,
isActive: false
};
_this.allSelectedOptionsDetails = normalizedSelectedOptions;
_this.suggestionsOrder = [];
_this._isMounted = false;
_Common.bind.apply(_assertThisInitialized(_this), ['handleInputCick', 'handleFilterSuggestions', 'handleKeyDown', 'handleSelectAll', 'handleDeselectAll', 'handleSelectOption', 'handleRemoveOption', 'handleSearch', 'handleClickSelectedOption', 'handleChange', 'handlePopupClose', 'togglePopup', 'selectedOptionRef', 'searchInputRef', 'suggestionContainerRef', 'suggestionItemRef', 'selectedOptionContainerRef', 'handleActive', 'handleInactive', 'handleMouseEnter', 'handleFetchOptions', 'handleFormatOptions', 'handleGetSelectedOptions', 'handleComponentDidUpdate', 'handleInputFocus', 'handleExposedPublicMethods', 'getSelectionUI', 'moveFocusToTextbox']);
_this.handleSearchOptions = (0, _Common.debounce)(_this.handleSearchOptions.bind(_assertThisInitialized(_this)), searchDebounceTime);
_this.handleScroll = _this.handleScroll.bind(_assertThisInitialized(_this));
_this.handleScrollFuncCall = (0, _Common.debounce)(_this.handleScrollFuncCall.bind(_assertThisInitialized(_this)), 500);
_this.getVirtualizerPublicMethods = _this.getVirtualizerPublicMethods.bind(_assertThisInitialized(_this));
_this.scrollToHighlightedIndex = _this.scrollToHighlightedIndex.bind(_assertThisInitialized(_this));
_this.setSuggestionsVirtualizerContainerRefFunction = _this.setSuggestionsVirtualizerContainerRefFunction.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(MultiSelectComponent, [{
key: "componentDidMount",
value: function componentDidMount() {
// let { suggestionContainer } = this;
this._isMounted = true;
this.handleExposedPublicMethods(); // suggestionContainer &&
// suggestionContainer.addEventListener('scroll', this.handleScroll);
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
var _this2 = this;
var selectedOptions = nextProps.selectedOptions,
options = nextProps.options,
valueField = nextProps.valueField,
textField = nextProps.textField,
prefixText = nextProps.prefixText;
var oldProps = this.props;
if (selectedOptions !== oldProps.selectedOptions || options !== oldProps.options || valueField !== oldProps.valueField || textField !== oldProps.textField || prefixText !== oldProps.prefixText //For GroupMultiSelect component
) {
var _this$handleFormatOpt2 = this.handleFormatOptions(nextProps),
allOptions = _this$handleFormatOpt2.allOptions,
normalizedAllOptions = _this$handleFormatOpt2.normalizedAllOptions,
normalizedFormatOptions = _this$handleFormatOpt2.normalizedFormatOptions;
var oldAllSelectedOptionsDetails = this.allSelectedOptionsDetails;
var optionsDetails = Object.assign({}, oldAllSelectedOptionsDetails, normalizedFormatOptions);
var _this$handleGetSelect2 = this.handleGetSelectedOptions(selectedOptions, optionsDetails, nextProps),
formatSelectedOptions = _this$handleGetSelect2.formatSelectedOptions,
normalizedSelectedOptions = _this$handleGetSelect2.normalizedSelectedOptions,
selectedOptionIds = _this$handleGetSelect2.selectedOptionIds;
this.allSelectedOptionsDetails = Object.assign(oldAllSelectedOptionsDetails, normalizedSelectedOptions);
this.setState({
options: allOptions,
optionsNormalize: normalizedAllOptions,
selectedOptions: formatSelectedOptions,
selectedOptionIds: selectedOptionIds
}, function () {
var hoverOption = _this2.state.hoverOption;
var suggestions = _this2.handleFilterSuggestions();
var suggestionsLen = suggestions.length;
var _ref = suggestions[hoverOption] || {},
id = _ref.id;
var newHoverOption = !(0, _Common.getIsEmptyValue)(id) ? hoverOption : suggestionsLen ? suggestionsLen - 1 : 0;
_this2.setState({
hoverOption: newHoverOption
});
});
}
}
}, {
key: "scrollToHighlightedIndex",
value: function scrollToHighlightedIndex() {
var suggestionContainer = this.suggestionContainer,
selectedOptionContainer = this.selectedOptionContainer,
suggestionsOrder = this.suggestionsOrder;
if (!suggestionContainer) {
return;
}
var _this$state = this.state,
hoverOption = _this$state.hoverOption,
highLightedSelectOptions = _this$state.highLightedSelectOptions,
selectedOptions = _this$state.selectedOptions;
var isVirtualizerEnabled = this.props.isVirtualizerEnabled;
var hoverId = suggestionsOrder[hoverOption] || '';
var lastHighLightedSelectOption = highLightedSelectOptions.slice(-1).length ? highLightedSelectOptions.slice(-1)[0] : null;
var selectedOption = this["selectedOption_".concat(lastHighLightedSelectOption)];
selectedOptions.length && selectedOption && (0, _Common.scrollTo)(selectedOptionContainer, selectedOption);
var selectedSuggestion = this["suggestion_".concat(hoverId)];
if (selectedSuggestion) {
(0, _Common.scrollTo)(suggestionContainer, selectedSuggestion);
} else {
// Hack: Virtualizer not having options order properly all the time, so scrollToIndex method used here.
if (isVirtualizerEnabled && !!this.virtualizerMethods) {
this.virtualizerMethods.scrollToIndex(hoverOption, 'auto', {
speed: 0,
duration: 0,
onComplete: function onComplete() {
(0, _Common.scrollTo)(suggestionContainer, selectedSuggestion);
}
});
}
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this$state2 = this.state,
searchStr = _this$state2.searchStr,
hoverOption = _this$state2.hoverOption;
var _this$props2 = this.props,
needLocalSearch = _this$props2.needLocalSearch,
isPopupOpen = _this$props2.isPopupOpen,
onDropBoxClose = _this$props2.onDropBoxClose,
onDropBoxOpen = _this$props2.onDropBoxOpen,
isSearchClearOnClose = _this$props2.isSearchClearOnClose; //handle dropbox open & close
if (prevProps.isPopupOpen !== isPopupOpen) {
if (isPopupOpen) {
this.scrollToHighlightedIndex();
onDropBoxOpen && this.handleFetchOptions(onDropBoxOpen, searchStr);
} else {
this.setState({
hoverOption: 0
});
isSearchClearOnClose && searchStr && this.handleSearch('');
onDropBoxClose && onDropBoxClose();
}
}
if (prevState.hoverOption !== hoverOption && isPopupOpen) {
this.scrollToHighlightedIndex();
} //When suggestions length less than 5, getNextOptions function call
var _this$props3 = this.props,
isNextOptions = _this$props3.isNextOptions,
getNextOptions = _this$props3.getNextOptions; // let { searchStr } = this.state;
var suggestions = this.handleFilterSuggestions();
var suggestionsLen = suggestions.length;
if (isPopupOpen && suggestionsLen <= 5 && isNextOptions && getNextOptions && !needLocalSearch) {
this.handleFetchOptions(getNextOptions, searchStr);
} //Need To MultiselectNew Component
this.handleComponentDidUpdate(prevProps, prevState); // if (isPopupOpen && isNextOptions && prevProps.selectedOptions.length !== selectedOptions.length) {
// let { scrollHeight, clientHeight } = this.suggestionContainer || {};
// let isElementScrollable = scrollHeight > clientHeight;
// if (!isElementScrollable) {
// this.handleScrollFuncCall();
// }
// }
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
// let { suggestionContainer } = this;
this._isMounted = false; // suggestionContainer &&
// suggestionContainer.removeEventListener('scroll', this.handleScroll);
}
}, {
key: "handleComponentDidUpdate",
value: function handleComponentDidUpdate() {
return;
}
}, {
key: "handleFormatOptions",
value: function handleFormatOptions(props) {
var options = props.options,
valueField = props.valueField,
textField = props.textField,
secondaryField = props.secondaryField,
prefixText = props.prefixText,
disabledOptions = props.disabledOptions,
allowValueFallback = props.allowValueFallback,
searchFields = props.searchFields;
return this.formatOptions({
options: options,
valueField: valueField,
textField: textField,
prefixText: prefixText,
optionType: 'default',
disabledOptions: disabledOptions,
allowValueFallback: allowValueFallback,
secondaryField: secondaryField,
searchFields: searchFields
});
}
}, {
key: "handleGetSelectedOptions",
value: function handleGetSelectedOptions(selectedOptions, normalizedFormatOptions, props) {
var allowValueFallback = props.allowValueFallback,
limit = props.limit;
return this.getSelectedOptions({
selectedOptions: selectedOptions,
normalizedFormatOptions: normalizedFormatOptions,
allowValueFallback: allowValueFallback,
limit: limit
});
}
}, {
key: "handleInputCick",
value: function handleInputCick(e) {
var removeClose = this.props.removeClose;
var _this$state3 = this.state,
highLightedSelectOptions = _this$state3.highLightedSelectOptions,
_this$state3$searchSt = _this$state3.searchStr,
searchStr = _this$state3$searchSt === void 0 ? '' : _this$state3$searchSt;
if (highLightedSelectOptions.length) {
this.setState({
highLightedSelectOptions: [],
shiftKeyPressHighLighted: 0,
lastHighLightedSelectOption: ''
});
removeClose(e);
} else if (searchStr.length) {
removeClose(e);
} else {
this.togglePopup(e);
}
}
}, {
key: "handleFilterSuggestions",
value: function handleFilterSuggestions() {
var _this$state4 = this.state,
_this$state4$options = _this$state4.options,
options = _this$state4$options === void 0 ? dummyArray : _this$state4$options,
_this$state4$searchSt = _this$state4.searchStr,
searchStr = _this$state4$searchSt === void 0 ? '' : _this$state4$searchSt;
var _this$props4 = this.props,
selectedOptions = _this$props4.selectedOptions,
needLocalSearch = _this$props4.needLocalSearch,
keepSelectedOptions = _this$props4.keepSelectedOptions,
searchFields = _this$props4.searchFields;
var _this$getFilterSugges = this.getFilterSuggestions({
options: options,
selectedOptions: selectedOptions,
searchStr: (0, _Common.getSearchString)(searchStr),
needSearch: needLocalSearch,
keepSelectedOptions: keepSelectedOptions,
searchFields: searchFields
}),
suggestions = _this$getFilterSugges.suggestions,
suggestionIds = _this$getFilterSugges.suggestionIds;
this.suggestionsOrder = suggestionIds;
return suggestions;
}
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
var keyCode = e.keyCode,
ctrlKey = e.ctrlKey,
metaKey = e.metaKey,
shiftKey = e.shiftKey;
var suggestions = [];
var _this$state5 = this.state,
hoverOption = _this$state5.hoverOption,
searchStr = _this$state5.searchStr,
highLightedSelectOptions = _this$state5.highLightedSelectOptions,
lastHighLightedSelectOption = _this$state5.lastHighLightedSelectOption,
shiftKeyPressHighLighted = _this$state5.shiftKeyPressHighLighted,
selectedOptions = _this$state5.selectedOptionIds;
var _this$props5 = this.props,
isNextOptions = _this$props5.isNextOptions,
getNextOptions = _this$props5.getNextOptions,
isPopupOpen = _this$props5.isPopupOpen,
isPopupOpenOnEnter = _this$props5.isPopupOpenOnEnter,
onKeyDown = _this$props5.onKeyDown,
limit = _this$props5.limit;
var allowKeyboardActions = !limit || limit && selectedOptions.length < limit;
var highLightedSelectOptionsLen = highLightedSelectOptions.length;
if (isPopupOpen && (keyCode === 38 || keyCode === 40 || keyCode === 13 || keyCode === 27 || keyCode === 9)) {
suggestions = this.handleFilterSuggestions();
}
if (!isPopupOpen && !isPopupOpenOnEnter) {
onKeyDown && onKeyDown(e);
}
if (!isPopupOpen && keyCode === 40) {
//down arrow press popup open
e.preventDefault(); //prevent body scroll
this.togglePopup(e);
}
var suggestionsLen = suggestions.length;
if (suggestionsLen && isPopupOpen && keyCode === 38 && allowKeyboardActions) {
//up arrow
/*if (hoverOption === 0) { //disable first to last option higlight
!isNextOptions && this.setState({ hoverOption: suggestionsLen - 1 });
}*/
if (hoverOption) {
this.setState({
hoverOption: hoverOption - 1
});
}
} else if (suggestionsLen && isPopupOpen && keyCode === 40 && allowKeyboardActions) {
//down arrow
/*else if (hoverOption === suggestionsLen - 1 || hoverOption === null) {
//disable last to first option higlight
!isNextOptions && this.setState({ hoverOption: 0 });
}*/
if (isNextOptions && suggestionsLen >= 5 && hoverOption === suggestionsLen - 3) {
getNextOptions && this.handleFetchOptions(getNextOptions, searchStr);
this.setState({
hoverOption: hoverOption + 1
});
} else if (suggestionsLen - 1 > hoverOption) {
this.setState({
hoverOption: hoverOption + 1
});
}
} else if (keyCode === 13 && allowKeyboardActions) {
//enter ke
var selectedOption = suggestions[hoverOption] || {};
var id = selectedOption.id;
isPopupOpen && !(0, _Common.getIsEmptyValue)(id) && this.handleSelectOption(id, e);
!isPopupOpen && isPopupOpenOnEnter && this.togglePopup(e);
} else if (selectedOptions.length && keyCode === 8 && !searchStr.length) {
//backspace key
if (highLightedSelectOptionsLen) {
this.handleRemoveOption(highLightedSelectOptions);
} else {
this.handleRemoveOption(selectedOptions.slice(-1)); // this.setState({
// highLightedSelectOptions: selectedOptions.slice(-1)
// });
}
} else if (selectedOptions && keyCode === 65 && (ctrlKey || metaKey) && !searchStr.length) {
//ctrl+a key
this.setState({
highLightedSelectOptions: selectedOptions,
shiftKeyPressHighLighted: 0
});
} else if (keyCode === 39 && shiftKey && selectedOptions.length && !searchStr.length) {
//shift+right arrow=39
var lastHighLightedSelectOptionIndex = lastHighLightedSelectOption && selectedOptions.indexOf(lastHighLightedSelectOption) >= 0 ? selectedOptions.indexOf(lastHighLightedSelectOption) : 0;
var newShiftKeyPressHighLighted = shiftKeyPressHighLighted ? shiftKeyPressHighLighted : shiftKeyPressHighLighted + 1;
var newHighLightedSelectOption = lastHighLightedSelectOptionIndex !== null ? selectedOptions[lastHighLightedSelectOptionIndex + newShiftKeyPressHighLighted] : selectedOptions[0];
if (!(0, _Common.getIsEmptyValue)(newHighLightedSelectOption)) {
var newLastHighLightedSelectOption = lastHighLightedSelectOption ? lastHighLightedSelectOption : selectedOptions[0];
highLightedSelectOptions = !shiftKeyPressHighLighted ? [newLastHighLightedSelectOption] : highLightedSelectOptions;
var isRemove = highLightedSelectOptions.indexOf(newHighLightedSelectOption) >= 0 && newHighLightedSelectOption !== lastHighLightedSelectOption ? true : false;
var newHighLightedSelectOptions = isRemove ? highLightedSelectOptions.filter(function (option) {
return option !== newHighLightedSelectOption;
}) : [].concat(_toConsumableArray(highLightedSelectOptions), [newHighLightedSelectOption]);
this.setState({
highLightedSelectOptions: newHighLightedSelectOptions,
shiftKeyPressHighLighted: newShiftKeyPressHighLighted + 1,
lastHighLightedSelectOption: newLastHighLightedSelectOption
});
}
} else if (keyCode === 37 && shiftKey && selectedOptions.length && !searchStr.length) {
// shift+left arrow=37
var _lastHighLightedSelectOptionIndex = lastHighLightedSelectOption ? selectedOptions.indexOf(lastHighLightedSelectOption) : selectedOptions.length - 1;
var _newShiftKeyPressHighLighted = shiftKeyPressHighLighted !== 1 ? shiftKeyPressHighLighted : shiftKeyPressHighLighted - 1;
var _newHighLightedSelectOption = selectedOptions[_lastHighLightedSelectOptionIndex + _newShiftKeyPressHighLighted - 1];
if (!(0, _Common.getIsEmptyValue)(_newHighLightedSelectOption)) {
var _newLastHighLightedSelectOption = lastHighLightedSelectOption ? lastHighLightedSelectOption : selectedOptions.slice(-1)[0];
highLightedSelectOptions = !shiftKeyPressHighLighted ? [_newLastHighLightedSelectOption] : highLightedSelectOptions;
var _isRemove = highLightedSelectOptions.indexOf(_newHighLightedSelectOption) >= 0 && _newHighLightedSelectOption !== lastHighLightedSelectOption ? true : false;
var _newHighLightedSelectOptions = _isRemove ? highLightedSelectOptions.filter(function (option) {
return option !== _newHighLightedSelectOption;
}) : [].concat(_toConsumableArray(highLightedSelectOptions), [_newHighLightedSelectOption]);
this.setState({
highLightedSelectOptions: _newHighLightedSelectOptions,
shiftKeyPressHighLighted: _newShiftKeyPressHighLighted - 1,
lastHighLightedSelectOption: _newLastHighLightedSelectOption
});
}
} else if ((keyCode === 39 || keyCode === 37) && selectedOptions.length && !searchStr.length) {
var isRightArrow = keyCode === 39 ? true : false; // let isLefttArrow = keyCode === 37 ? true : false;
if (highLightedSelectOptions.length) {
var _highLightedSelectOpt = highLightedSelectOptions.slice(-1),
_highLightedSelectOpt2 = _slicedToArray(_highLightedSelectOpt, 1),
_lastHighLightedSelectOption = _highLightedSelectOpt2[0];
var _lastHighLightedSelectOptionIndex2 = selectedOptions.indexOf(_lastHighLightedSelectOption);
var newLastHighLightedSelectOptionIndex = isRightArrow ? _lastHighLightedSelectOptionIndex2 === selectedOptions.length - 1 ? _lastHighLightedSelectOptionIndex2 : _lastHighLightedSelectOptionIndex2 + 1 : _lastHighLightedSelectOptionIndex2 - 1;
var _newLastHighLightedSelectOption2 = selectedOptions[newLastHighLightedSelectOptionIndex];
var isEmptyHighlighted = isRightArrow && highLightedSelectOptions.length === 1 && selectedOptions.slice(-1)[0] === _lastHighLightedSelectOption ? true : false;
if (!(0, _Common.getIsEmptyValue)(_newLastHighLightedSelectOption2)) {
this.setState({
lastHighLightedSelectOption: isEmptyHighlighted ? '' : _newLastHighLightedSelectOption2,
highLightedSelectOptions: isEmptyHighlighted ? [] : [_newLastHighLightedSelectOption2],
shiftKeyPressHighLighted: 0
});
}
} else {
var _ref2 = isRightArrow ? selectedOptions : selectedOptions.slice(-1),
_ref3 = _slicedToArray(_ref2, 1),
_newLastHighLightedSelectOption3 = _ref3[0];
this.setState({
lastHighLightedSelectOption: _newLastHighLightedSelectOption3,
highLightedSelectOptions: [_newLastHighLightedSelectOption3],
shiftKeyPressHighLighted: 0
});
}
} else if (keyCode === 27) {// this.handlePopupClose(e);
} else if (keyCode === 9) {
this.handlePopupClose(e);
}
}
}, {
key: "handleSelectAll",
value: function handleSelectAll(e) {
e && e.preventDefault();
var suggestions = this.handleFilterSuggestions();
var selectedOptions = this.props.selectedOptions;
var newSelectedOptions = [];
suggestions.forEach(function (option) {
var id = option.id;
if (selectedOptions.indexOf(id) === -1) {
newSelectedOptions.push(id);
}
});
this.handleChange([].concat(_toConsumableArray(selectedOptions), newSelectedOptions)); // this.handlePopupClose(e);
}
}, {
key: "handleDeselectAll",
value: function handleDeselectAll(e) {
e && e.preventDefault();
var removeClose = this.props.removeClose;
var highLightedSelectOptions = this.state.highLightedSelectOptions;
if (highLightedSelectOptions.length) {
this.setState({
highLightedSelectOptions: [],
lastHighLightedSelectOption: ''
});
}
removeClose(e);
this.handleChange([]);
}
}, {
key: "handleSelectOption",
value: function handleSelectOption(option, value, index, e) {
var _this$props6 = this.props,
selectedOptions = _this$props6.selectedOptions,
isSearchClearOnSelect = _this$props6.isSearchClearOnSelect,
keepSelectedOptions = _this$props6.keepSelectedOptions;
var searchStr = this.state.searchStr;
if (searchStr.trim() != '' && isSearchClearOnSelect) {
this.handleSearch('');
}
if (keepSelectedOptions && selectedOptions.indexOf(option) != -1) {
var newSelectedOptions = selectedOptions.filter(function (id) {
return id != option;
});
this.handleChange(newSelectedOptions, e);
} else {
this.handleChange([].concat(_toConsumableArray(selectedOptions), [option]), e);
}
}
}, {
key: "handleRemoveOption",
value: function handleRemoveOption(options) {
var newOptions = !(0, _Common.getIsEmptyValue)(options) && !Array.isArray(options) ? [options] : options;
var _this$props7 = this.props,
selectedOptions = _this$props7.selectedOptions,
isReadOnly = _this$props7.isReadOnly;
var _this$state6 = this.state,
highLightedSelectOptions = _this$state6.highLightedSelectOptions,
lastHighLightedSelectOption = _this$state6.lastHighLightedSelectOption,
shiftKeyPressHighLighted = _this$state6.shiftKeyPressHighLighted;
if (newOptions.length && !isReadOnly) {
var newSelectedOptions = selectedOptions.filter(function (option) {
return newOptions.indexOf(option) === -1;
});
var newHighLightedSelectOptions = highLightedSelectOptions.filter(function (option) {
return newSelectedOptions.indexOf(option) >= 0;
});
var isHighlightedRemoved = false;
var newOptionsLen = newOptions.length;
for (var i = 0; i < newOptionsLen; i++) {
var removedOption = newOptions[i];
if (highLightedSelectOptions.indexOf(removedOption) >= 0) {
isHighlightedRemoved = true;
break;
}
}
this.setState({
lastHighLightedSelectOption: newSelectedOptions.indexOf(lastHighLightedSelectOption) >= 0 && !isHighlightedRemoved ? lastHighLightedSelectOption : '',
highLightedSelectOptions: isHighlightedRemoved ? [] : newHighLightedSelectOptions,
shiftKeyPressHighLighted: isHighlightedRemoved ? 0 : shiftKeyPressHighLighted
});
this.handleChange(newSelectedOptions);
}
this.moveFocusToTextbox();
}
}, {
key: "handleMouseEnter",
value: function handleMouseEnter(id, val, hoverOptionIndex, e) {
e && e.preventDefault();
var hoverOption = this.state.hoverOption;
var suggestionsOrder = this.suggestionsOrder;
var newHoverIndex = suggestionsOrder.indexOf(id);
if (newHoverIndex !== hoverOption) {
this.setState({
hoverOption: newHoverIndex
});
}
}
}, {
key: "handleFetchOptions",
value: function handleFetchOptions() {
var _this3 = this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var APICall = args[0],
searchStr = args[1];
var isFetchingOptions = this.state.isFetchingOptions;
var _isMounted = this._isMounted;
var isForce = isFetchingOptions && searchStr ? true : false;
if (!isFetchingOptions && APICall || isForce) {
this.setState({
isFetchingOptions: true
});
try {
return APICall(searchStr).then(function () {
_isMounted && _this3.setState({
isFetchingOptions: false
});
}, function () {
_isMounted && _this3.setState({
isFetchingOptions: false
});
});
} catch (e) {
_isMounted && this.setState({
isFetchingOptions: false
});
}
}
}
}, {
key: "handleSearchOptions",
value: function handleSearchOptions() {
var onSearch = this.props.onSearch;
var searchStr = this.state.searchStr;
searchStr && this.handleFetchOptions(onSearch, searchStr);
}
}, {
key: "handleSearch",
value: function handleSearch(value, e) {
var _this4 = this;
var _this$props8 = this.props,
onSearch = _this$props8.onSearch,
isPopupOpen = _this$props8.isPopupOpen;
!isPopupOpen && e && this.togglePopup(e);
var _this$state$searchStr = this.state.searchStr,
searchStr = _this$state$searchStr === void 0 ? '' : _this$state$searchStr;
var searchStrRegex = (0, _Common.getSearchString)(searchStr);
var valueStrRegex = (0, _Common.getSearchString)(value);
var isSearch = searchStrRegex !== valueStrRegex ? true : false;
this.setState({
searchStr: value
}, function () {
if (!value) {
onSearch && onSearch('');
} else if (isSearch && onSearch) {
_this4.handleSearchOptions();
}
});
}
}, {
key: "handleClickSelectedOption",
value: function handleClickSelectedOption() {
var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var e = arguments.length > 1 ? arguments[1] : undefined;
var selectedOptions = this.props.selectedOptions;
var _this$state7 = this.state,
highLightedSelectOptions = _this$state7.highLightedSelectOptions,
lastHighLightedSelectOption = _this$state7.lastHighLightedSelectOption;
var metaKey = e.metaKey,
ctrlKey = e.ctrlKey,
shiftKey = e.shiftKey;
if (e && shiftKey) {
//shift+click
var from = selectedOptions.indexOf(lastHighLightedSelectOption) >= 0 ? selectedOptions.indexOf(lastHighLightedSelectOption) : 0;
var to = id && selectedOptions.indexOf(id) >= 0 ? selectedOptions.indexOf(id) : null;
if (to >= 0 && to < from) {
var _ref4 = [from, from = to];
to = _ref4[0];
}
to += 1;
var newSelectedHighlights = to ? selectedOptions.slice(from, to) : [];
to && this.setState({
highLightedSelectOptions: newSelectedHighlights,
lastHighLightedSelectOption: id
});
} else if (e && (ctrlKey || metaKey)) {
//ctrl+click
var isRemove = highLightedSelectOptions.indexOf(id) >= 0;
var _newSelectedHighlights = [];
if (isRemove) {
lastHighLightedSelectOption = id === lastHighLightedSelectOption ? '' : lastHighLightedSelectOption;
_newSelectedHighlights = highLightedSelectOptions.filter(function (option) {
return option !== id;
});
} else {
lastHighLightedSelectOption = id;
_newSelectedHighlights = [].concat(_toConsumableArray(highLightedSelectOptions), [id]);
}
this.setState({
highLightedSelectOptions: _newSelectedHighlights,
lastHighLightedSelectOption: lastHighLightedSelectOption
});
} else {
this.setState({
highLightedSelectOptions: [id],
lastHighLightedSelectOption: id
});
}
this.setState({
shiftKeyPressHighLighted: 0
});
this.moveFocusToTextbox();
}
}, {
key: "handleScroll",
value: function handleScroll(e) {
var ele = e.target;
var isScrollReachedBottom = (0, _Common.findScrollEnd)(ele);
isScrollReachedBottom && this.handleScrollFuncCall();
}
}, {
key: "handleScrollFuncCall",
value: function handleScrollFuncCall() {
var _this$props9 = this.props,
getNextOptions = _this$props9.getNextOptions,
isNextOptions = _this$props9.isNextOptions;
var searchStr = this.state.searchStr;
isNextOptions && getNextOptions && this.handleFetchOptions(getNextOptions, searchStr);
}
}, {
key: "handleChange",
value: function handleChange() {
var selectedOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var e = arguments.length > 1 ? arguments[1] : undefined;
var optionsNormalize = this.state.optionsNormalize;
var _this$props10 = this.props,
onChange = _this$props10.onChange,
needToCloseOnSelect = _this$props10.needToCloseOnSelect,
togglePopup = _this$props10.togglePopup,
propSelectedOptions = _this$props10.selectedOptions,
_this$props10$disable = _this$props10.disabledOptions,
disabledOptions = _this$props10$disable === void 0 ? dummyArray : _this$props10$disable,
limit = _this$props10.limit;
var _filterSelectedOption = (0, _dropDownUtils.filterSelectedOptions)({
selectedOptions: selectedOptions,
propSelectedOptions: propSelectedOptions,
disabledOptions: disabledOptions,
limit: limit
}),
newSelectedOptions = _filterSelectedOption.newSelectedOptions;
var selectedOptionsLen = newSelectedOptions.length;
var allSelectedOptionsDetails = [];
for (var i = 0; i < selectedOptionsLen; i++) {
var id = newSelectedOptions[i];
allSelectedOptionsDetails.push(optionsNormalize[id]);
}
onChange && onChange(newSelectedOptions, allSelectedOptionsDetails); // this.setState({ searchStr: '' });
this.moveFocusToTextbox();
if (needToCloseOnSelect) {
togglePopup(e);
}
}
}, {
key: "togglePopup",
value: function togglePopup(e) {
var _this$props11 = this.props,
togglePopup = _this$props11.togglePopup,
defaultDropBoxPosition = _this$props11.defaultDropBoxPosition,
isReadOnly = _this$props11.isReadOnly;
!isReadOnly && togglePopup(e, defaultDropBoxPosition ? "".concat(defaultDropBoxPosition) : null);
}
}, {
key: "handlePopupClose",
value: function handlePopupClose(e) {
var closePopupOnly = this.props.closePopupOnly;
closePopupOnly(e);
}
}, {
key: "searchInputRef",
value: function searchInputRef(el) {
var getRef = this.props.getRef;
this.searchInput = el;
getRef && getRef(el);
}
}, {
key: "selectedOptionContainerRef",
value: function selectedOptionContainerRef(el) {
var getTargetRef = this.props.getTargetRef;
this.selectedOptionContainer = el;
getTargetRef(el);
}
}, {
key: "selectedOptionRef",
value: function selectedOptionRef(el, id) {
this["selectedOption_".concat(id)] = el;
}
}, {
key: "suggestionContainerRef",
value: function suggestionContainerRef(el) {
this.suggestionContainer = el;
typeof this.setSuggestionsVirtualizerRef === 'function' && this.setSuggestionsVirtualizerRef(el);
}
}, {
key: "suggestionItemRef",
value: function suggestionItemRef(el, index, id) {
this["suggestion_".concat(id)] = el;
}
}, {
key: "handleActive",
value: function handleActive(e) {
var _this$state8 = this.state,
searchStr = _this$state8.searchStr,
isActive = _this$state8.isActive;
if (!isActive) {
this.setState({
isActive: true
});
}
var _ref5 = e || {},
target = _ref5.target;
target && target.setSelectionRange(target, 0);
var onFocus = this.props.onFocus;
onFocus && onFocus(searchStr);
}
}, {
key: "handleInactive",
value: function handleInactive() {
var isActive = this.state.isActive;
if (isActive) {
this.setState({
isActive: false
});
}
}
}, {
key: "handleInputFocus",
value: function handleInputFocus() {
var _this$props12 = this.props,
isDisabled = _this$props12.isDisabled,
isReadOnly = _this$props12.isReadOnly;
!isDisabled && !isReadOnly && this.moveFocusToTextbox();
}
}, {
key: "moveFocusToTextbox",
value: function moveFocusToTextbox() {
this.searchInput && this.searchInput.focus({
preventScroll: true
});
}
}, {
key: "handleExposedPublicMethods",
value: function handleExposedPublicMethods() {
var _this$props13 = this.props,
getPublicMethods = _this$props13.getPublicMethods,
openPopupOnly = _this$props13.openPopupOnly;
getPublicMethods && getPublicMethods({
openPopupOnly: openPopupOnly
});
}
}, {
key: "getVirtualizerPublicMethods",
value: function getVirtualizerPublicMethods(methods) {
this.virtualizerMethods = methods;
}
}, {
key: "setSuggestionsVirtualizerContainerRefFunction",
value: function setSuggestionsVirtualizerContainerRefFunction(refFunc) {
this.setSuggestionsVirtualizerRef = refFunc;
this.suggestionContainer && refFunc(this.suggestionContainer);
}
}, {
key: "getSelectionUI",
value: function getSelectionUI() {
var isResponsive = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var _this$props14 = this.props,
size = _this$props14.size,
needBorder = _this$props14.needBorder,
disableAction = _this$props14.disableAction,
borderColor = _this$props14.borderColor,
placeHolder = _this$props14.placeHolder,
textBoxSize = _this$props14.textBoxSize,
variant = _this$props14.variant,
textBoxClass = _this$props14.textBoxClass,
needAutoFocus = _this$props14.needAutoFocus,
htmlId = _this$props14.htmlId,
children = _this$props14.children,
customChildrenClass = _this$props14.customChildrenClass,
autoComplete = _this$props14.autoComplete,
a11y = _this$props14.a11y,
disabledOptions = _this$props14.disabledOptions,
i18nKeys = _this$props14.i18nKeys,
isReadOnly = _this$props14.isReadOnly,
isDisabled = _this$props14.isDisabled,
palette = _this$props14.palette,
dataId = _this$props14.dataId,
setAriaId = _this$props14.setAriaId,
isPopupOpen = _this$props14.isPopupOpen,
ariaErrorId = _this$props14.ariaErrorId,
customProps = _this$props14.customProps,
renderCustomSelectedValue = _this$props14.renderCustomSelectedValue,
isFocus = _this$props14.isFocus,
isPopupReady = _this$props14.isPopupReady,
renderCustomClearComponent = _this$props14.renderCustomClearComponent,
renderCustomToggleIndicator = _this$props14.renderCustomToggleIndicator;
var _this$state9 = this.state,
isActive = _this$state9.isActive,
selectedOptions = _this$state9.selectedOptions,
highLightedSelectOptions = _this$state9.highLightedSelectOptions,
searchStr = _this$state9.searchStr;
var _i18nKeys$clearText = i18nKeys.clearText,
clearText = _i18nKeys$clearText === void 0 ? _constants.MULTISELECT_I18N_KEYS.clearText : _i18nKeys$clearText;
var _a11y$clearLabel = a11y.clearLabel,
clearLabel = _a11y$clearLabel === void 0 ? _constants.MULTISELECT_I18N_KEYS.clearLabel : _a11y$clearLabel,
ariaLabelledby = a11y.ariaLabelledby;
var _this$getIsShowClearI = this.getIsShowClearIcon({
selectedOptions: selectedOptions,
disabledOptions: disabledOptions
}),
isShowClear = _this$getIsShowClearI.isShowClearIcon;
var isShowClearIcon = !isReadOnly && !isDisabled && !disableAction && isShowClear;
var isEditable = !(isReadOnly || isDisabled || disableAction);
var _customProps$TextBoxI = customProps.TextBoxIconProps,
TextBoxIconProps = _customProps$TextBoxI === void 0 ? {} : _customProps$TextBoxI;
return /*#__PURE__*/_react["default"].createElement(_Layout.Container, {
align: "vertical",
alignBox: "row",
className: "".concat(_MultiSelectModule["default"].container, " ").concat(_MultiSelectModule["default"][size], " ").concat(needBorder ? !disableAction ? _MultiSelectModule["default"]["borderColor_".concat(borderColor)] : _MultiSelectModule["default"].borderColor_transparent : '', " ").concat(isActive && needBorder || isResponsive || isEditable && isFocus && needBorder ? _MultiSelectModule["default"].active : '', " ").concat(needBorder ? _MultiSelectModule["default"].hasBorder : '', " ").concat(textBoxClass, " "),
eleRef: this.selectedOptionContainerRef,
wrap: "wrap"
}, /*#__PURE__*/_react["default"].createElement(_SelectedOptions["default"], {
selectedOptions: selectedOptions,
highLightedSelectOptions: highLightedSelectOptions,
isReadOnly: isReadOnly,
getRef: this.selectedOptionRef,
onRemove: this.handleRemoveOption,
onSelect: this.handleClickSelectedOption,
size: size,
palette: palette,
dataId: "".concat(dataId, "_selectedOptions"),
renderCustomSelectedValue: renderCustomSelectedValue,
isPopupReady: isPopupReady,
isPopupOpen: isPopupOpen,
isDisabled: isDisabled
}), /*#__PURE__*/_react["default"].createElement(_Layout.Box, {
flexible: true,
className: _MultiSelectModule["default"].wrapper,
adju