UNPKG

@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,037 lines (901 loc) • 58.4 kB
"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"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = require("./props/propTypes"); var _defaultProps = require("./props/defaultProps"); var _constants = require("./constants"); var _Loader = _interopRequireDefault(require("@zohodesk/svg/lib/Loader/Loader")); var _Popup = _interopRequireDefault(require("../Popup/Popup")); var _TextBoxIcon = _interopRequireDefault(require("../TextBoxIcon/TextBoxIcon")); var _Layout = require("../Layout"); var _DropDownHeading = _interopRequireDefault(require("../DropDown/DropDownHeading")); var _MultiSelectHeader = _interopRequireDefault(require("../MultiSelect/MultiSelectHeader")); var _SelectedOptions = _interopRequireDefault(require("../MultiSelect/SelectedOptions")); var _Suggestions = _interopRequireDefault(require("../MultiSelect/Suggestions")); var _EmptyState = _interopRequireDefault(require("../MultiSelect/EmptyState")); var _Card = _interopRequireWildcard(require("../Card/Card")); var _IdProvider = require("../Provider/IdProvider"); var _ResponsiveDropBox = _interopRequireDefault(require("../ResponsiveDropBox/ResponsiveDropBox")); var _CustomResponsive = require("../Responsive/CustomResponsive"); var _Common = require("./../utils/Common"); var _icons = require("@zohodesk/icons"); var _MultiSelectModule = _interopRequireDefault(require("./MultiSelect.module.css")); var _Common2 = require("../utils/Common.js"); var _dropDownUtils = require("../utils/dropDownUtils"); 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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 AdvancedGroupMultiSelect = /*#__PURE__*/function (_React$Component) { _inherits(AdvancedGroupMultiSelect, _React$Component); var _super = _createSuper(AdvancedGroupMultiSelect); function AdvancedGroupMultiSelect(props) { var _this$state; var _this; _classCallCheck(this, AdvancedGroupMultiSelect); _this = _super.call(this, props); _this.getNextAriaId = (0, _IdProvider.getUniqueId)(_assertThisInitialized(_this)); _this.objectConcat = (0, _dropDownUtils.makeObjectConcat)(); _this.getGroupSelectOptions = (0, _dropDownUtils.makeGetGroupSelectOptions)(); _this.getFilterSuggestions = (0, _dropDownUtils.makeGetGroupSelectFilterSuggestions)(); _this.getSelectedOptions = (0, _dropDownUtils.makeGetMultiSelectSelectedOptions)(); _this.getOptionIdChange = (0, _dropDownUtils.makeGetOptionIdChange)(); _this.formatSelectedOptions = (0, _dropDownUtils.makeFormatOptions)(); var _props$searchDebounce = props.searchDebounceTime, searchDebounceTime = _props$searchDebounce === void 0 ? 500 : _props$searchDebounce, selectedGroupOptions = props.selectedGroupOptions; var _this$handleGetGroupS = _this.handleGetGroupSelectOptions(props), revampedGroups = _this$handleGetGroupS.revampedGroups, normalizedAllOptions = _this$handleGetGroupS.normalizedAllOptions, normalizedFormatOptions = _this$handleGetGroupS.normalizedFormatOptions, allOptionIds = _this$handleGetGroupS.allOptionIds, groupIds = _this$handleGetGroupS.groupIds, groupDetails = _this$handleGetGroupS.normalizedGroupedOptions; var changedSelectedOptionDetails = _this.handleSelectedOptionDetailsIdChange(props, { groupIds: groupIds, groupDetails: groupDetails }); var _this$handleGetSelect = _this.handleGetSelectedOptions(changedSelectedOptionDetails), normalizedSelectedOptions = _this$handleGetSelect.normalizedSelectedOptions; var _this$handleSelectedO = _this.handleSelectedOptionIdChange(selectedGroupOptions, normalizedFormatOptions, normalizedSelectedOptions), formatSelectedOptions = _this$handleSelectedO.formatSelectedOptions, selectedOptionIds = _this$handleSelectedO.selectedOptionIds; _this.handleGetGroupSelectOptions = _this.handleGetGroupSelectOptions.bind(_assertThisInitialized(_this)); _this.selectedOptionContainerRef = _this.selectedOptionContainerRef.bind(_assertThisInitialized(_this)); _this.suggestionContainerRef = _this.suggestionContainerRef.bind(_assertThisInitialized(_this)); _this.suggestionItemRef = _this.suggestionItemRef.bind(_assertThisInitialized(_this)); _this.searchInputRef = _this.searchInputRef.bind(_assertThisInitialized(_this)); _this.handleScroll = _this.handleScroll.bind(_assertThisInitialized(_this)); _this.handleScrollFuncCall = (0, _Common2.debounce)(_this.handleScrollFuncCall.bind(_assertThisInitialized(_this)), 500); _this.handleInputCick = _this.handleInputCick.bind(_assertThisInitialized(_this)); _this.togglePopup = _this.togglePopup.bind(_assertThisInitialized(_this)); _this.handleSelectedOptionIdChange = _this.handleSelectedOptionIdChange.bind(_assertThisInitialized(_this)); _this.handleMouseEnter = _this.handleMouseEnter.bind(_assertThisInitialized(_this)); _this.handleSelectOption = _this.handleSelectOption.bind(_assertThisInitialized(_this)); _this.selectedOptionRef = _this.selectedOptionRef.bind(_assertThisInitialized(_this)); _this.handleClickSelectedOption = _this.handleClickSelectedOption.bind(_assertThisInitialized(_this)); _this.handleRemoveOption = _this.handleRemoveOption.bind(_assertThisInitialized(_this)); _this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_this)); _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this)); _this.handleFetchOptions = _this.handleFetchOptions.bind(_assertThisInitialized(_this)); _this.handleSearch = _this.handleSearch.bind(_assertThisInitialized(_this)); _this.handleSearchOptions = (0, _Common2.debounce)(_this.handleSearchOptions.bind(_assertThisInitialized(_this)), searchDebounceTime); _this.handleSelectAll = _this.handleSelectAll.bind(_assertThisInitialized(_this)); _this.handleDeselectAll = _this.handleDeselectAll.bind(_assertThisInitialized(_this)); _this.handleActive = _this.handleActive.bind(_assertThisInitialized(_this)); _this.handleInactive = _this.handleInactive.bind(_assertThisInitialized(_this)); _this.handleInputFocus = _this.handleInputFocus.bind(_assertThisInitialized(_this)); _this.state = (_this$state = { isActive: false, searchStr: '', revampedGroups: revampedGroups, normalizedAllOptions: normalizedAllOptions, normalizedFormatOptions: normalizedFormatOptions, allOptionIds: allOptionIds, selectedOptionIds: selectedOptionIds, formatSelectedOptions: formatSelectedOptions, hoverIndex: 0, highLightedSelectOptions: [], lastHighLightedSelectOption: '', shiftKeyPressHighLighted: 0 }, _defineProperty(_this$state, "isActive", false), _defineProperty(_this$state, "isFetchingOptions", false), _defineProperty(_this$state, "groupIds", groupIds), _defineProperty(_this$state, "groupDetails", groupDetails), _this$state); _this._isMounted = false; return _this; } _createClass(AdvancedGroupMultiSelect, [{ key: "handleGetSelectedOptions", value: function handleGetSelectedOptions(selectedOptionDetails) { var _this$handleFormatSel = this.handleFormatSelectedOptions(selectedOptionDetails, this.props), normalizedSelectedOptions = _this$handleFormatSel.normalizedFormatOptions; return { normalizedSelectedOptions: normalizedSelectedOptions }; } }, { key: "handleFormatSelectedOptions", value: function handleFormatSelectedOptions(selectedOptionDetails, props) { var valueField = props.valueField, textField = props.textField, allowValueFallback = props.allowValueFallback; return this.formatSelectedOptions({ options: selectedOptionDetails, valueField: valueField, textField: textField, allowValueFallback: allowValueFallback }); } }, { key: "handleSelectedOptionDetailsIdChange", value: function handleSelectedOptionDetailsIdChange(props, state) { var _this2 = this; var groupIds = state.groupIds, groupDetails = state.groupDetails; var _props$selectedOption = props.selectedOptionDetails, selectedOptionDetails = _props$selectedOption === void 0 ? {} : _props$selectedOption; var newSelectedOptionDetails = []; groupIds.forEach(function (groupId) { var _groupDetails$groupId = groupDetails[groupId], valueField = _groupDetails$groupId.valueField, textField = _groupDetails$groupId.textField, optionType = _groupDetails$groupId.optionType; var changedDetails = _this2.getOptionIdChange({ options: selectedOptionDetails[groupId], valueField: valueField, prefixText: groupId, textField: textField, optionType: optionType }); newSelectedOptionDetails = [].concat(_toConsumableArray(newSelectedOptionDetails), _toConsumableArray(changedDetails)); }); return newSelectedOptionDetails; } }, { key: "selectedOptionRef", value: function selectedOptionRef(el, id) { this["selectedOption_".concat(id)] = el; } }, { key: "suggestionContainerRef", value: function suggestionContainerRef(el) { this.suggestionContainer = el; } }, { key: "selectedOptionContainerRef", value: function selectedOptionContainerRef(el) { var getTargetRef = this.props.getTargetRef; this.selectedOptionContainer = el; getTargetRef(el); } }, { key: "suggestionItemRef", value: function suggestionItemRef(el, index, id) { this["suggestion_".concat(id)] = el; } }, { key: "searchInputRef", value: function searchInputRef(ref) { this.searchInput = ref; } }, { key: "handleSelectedOptionIdChange", value: function handleSelectedOptionIdChange(selectedGroupOptions, normalizedFormatOptions, normalizedSelectedOptions) { var _this$props = this.props, allowValueFallback = _this$props.allowValueFallback, limit = _this$props.limit; var newSelectedOptions = selectedGroupOptions.map(function (option) { return (0, _dropDownUtils.optionIdGrouping)(option.id, option.groupId); }); var selectedOptionsLength = newSelectedOptions.length; var formatOptions = this.objectConcat({ obj1: normalizedFormatOptions, obj2: normalizedSelectedOptions }); return this.getSelectedOptions({ selectedOptions: newSelectedOptions, normalizedFormatOptions: formatOptions, selectedOptionsLength: selectedOptionsLength, allowValueFallback: allowValueFallback, limit: limit }); } }, { key: "togglePopup", value: function togglePopup(e) { var _this$props2 = this.props, togglePopup = _this$props2.togglePopup, defaultDropBoxPosition = _this$props2.defaultDropBoxPosition, isReadOnly = _this$props2.isReadOnly; !isReadOnly && togglePopup(e, defaultDropBoxPosition ? "".concat(defaultDropBoxPosition, "Center") : null); } }, { key: "handleFilterSuggestions", value: function handleFilterSuggestions() { var _this$state2 = this.state, revampedGroups = _this$state2.revampedGroups, _this$state2$searchSt = _this$state2.searchStr, searchStr = _this$state2$searchSt === void 0 ? '' : _this$state2$searchSt, allOptionIds = _this$state2.allOptionIds; var needLocalSearch = this.props.needLocalSearch; if (searchStr && searchStr.trim().length) { searchStr = (0, _Common2.getSearchString)(searchStr); var _this$getFilterSugges = this.getFilterSuggestions({ revampedGroups: revampedGroups, searchStr: searchStr, needSearch: needLocalSearch }), suggestionGroups = _this$getFilterSugges.suggestionGroups, suggestionOptionIds = _this$getFilterSugges.suggestionOptionIds; return { suggestionGroups: suggestionGroups, suggestionOptionIds: suggestionOptionIds }; } return { suggestionGroups: revampedGroups, suggestionOptionIds: allOptionIds }; } }, { key: "handleGetGroupSelectOptions", value: function handleGetGroupSelectOptions(props) { var allowValueFallback = props.allowValueFallback, _props$groupedOptions = props.groupedOptions, groupedOptions = _props$groupedOptions === void 0 ? [] : _props$groupedOptions; return this.getGroupSelectOptions({ groupedOptions: groupedOptions, allowValueFallback: allowValueFallback }); } }, { key: "handleInputCick", value: function handleInputCick(e) { var highLightedSelectOptions = this.state.highLightedSelectOptions; highLightedSelectOptions.length ? this.setState({ highLightedSelectOptions: [], shiftKeyPressHighLighted: 0, lastHighLightedSelectOption: '' }) : this.togglePopup(e); } }, { key: "handleClickSelectedOption", value: function handleClickSelectedOption() { var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var e = arguments.length > 1 ? arguments[1] : undefined; var selectedOptionIds = this.state.selectedOptionIds; var _this$state3 = this.state, highLightedSelectOptions = _this$state3.highLightedSelectOptions, lastHighLightedSelectOption = _this$state3.lastHighLightedSelectOption; var metaKey = e.metaKey, ctrlKey = e.ctrlKey, shiftKey = e.shiftKey; if (e && shiftKey) { //shift+click var from = selectedOptionIds.indexOf(lastHighLightedSelectOption) >= 0 ? selectedOptionIds.indexOf(lastHighLightedSelectOption) : 0; var to = id && selectedOptionIds.indexOf(id) >= 0 ? selectedOptionIds.indexOf(id) : null; if (to >= 0 && to < from) { var _ref = [from, from = to]; to = _ref[0]; } to += 1; var newSelectedHighlights = to ? selectedOptionIds.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.searchInput && this.searchInput.focus({ preventScroll: true }); } }, { key: "handleRemoveOption", value: function handleRemoveOption(options) { var newOptions = !(0, _Common2.getIsEmptyValue)(options) && !Array.isArray(options) ? [options] : options; var isReadOnly = this.props.isReadOnly; var selectedOptionIds = this.state.selectedOptionIds; var _this$state4 = this.state, highLightedSelectOptions = _this$state4.highLightedSelectOptions, lastHighLightedSelectOption = _this$state4.lastHighLightedSelectOption, shiftKeyPressHighLighted = _this$state4.shiftKeyPressHighLighted; if (newOptions.length && !isReadOnly) { var newSelectedOptions = selectedOptionIds.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.searchInput && this.searchInput.focus({ preventScroll: true }); } }, { 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, hoverIndex = _this$state5.hoverIndex, searchStr = _this$state5.searchStr, highLightedSelectOptions = _this$state5.highLightedSelectOptions, lastHighLightedSelectOption = _this$state5.lastHighLightedSelectOption, shiftKeyPressHighLighted = _this$state5.shiftKeyPressHighLighted, selectedOptions = _this$state5.selectedOptionIds; var _this$props3 = this.props, isNextOptions = _this$props3.isNextOptions, getNextOptions = _this$props3.getNextOptions, isPopupOpen = _this$props3.isPopupOpen, isPopupOpenOnEnter = _this$props3.isPopupOpenOnEnter, onKeyDown = _this$props3.onKeyDown, limit = _this$props3.limit; var highLightedSelectOptionsLen = highLightedSelectOptions.length; if (isPopupOpen && (keyCode === 38 || keyCode === 40 || keyCode === 13 || keyCode === 27)) { var _this$handleFilterSug = this.handleFilterSuggestions(), suggestionOptionIds = _this$handleFilterSug.suggestionOptionIds; suggestions = suggestionOptionIds; } 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) { //up arrow /*if (hoverOption === 0) { //disable first to last option higlight !isNextOptions && this.setState({ hoverOption: suggestionsLen - 1 }); }*/ if (hoverIndex) { this.setState({ hoverIndex: hoverIndex - 1 }); } } else if (suggestionsLen && isPopupOpen && keyCode === 40) { //down arrow /*else if (hoverOption === suggestionsLen - 1 || hoverOption === null) { //disable last to first option higlight !isNextOptions && this.setState({ hoverOption: 0 }); }*/ if (isNextOptions && suggestionsLen >= 5 && hoverIndex === suggestionsLen - 3) { getNextOptions && this.handleFetchOptions(getNextOptions, searchStr); this.setState({ hoverIndex: hoverIndex + 1 }); } else if (suggestionsLen - 1 > hoverIndex) { this.setState({ hoverIndex: hoverIndex + 1 }); } } else if (keyCode === 13 && selectedOptions.length < limit) { //enter key var id = suggestions[hoverIndex] || {}; isPopupOpen && !(0, _Common2.getIsEmptyValue)(id) && this.handleSelectOption(id, null, null, 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, _Common2.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, _Common2.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, _Common2.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); } } }, { key: "handleMouseEnter", value: function handleMouseEnter(id, val, hoverOptionIndex, e) { e && e.preventDefault(); var _this$state6 = this.state, hoverIndex = _this$state6.hoverIndex, allOptionIds = _this$state6.allOptionIds; var newHoverIndex = allOptionIds.indexOf(id); hoverIndex !== newHoverIndex && this.setState({ hoverIndex: 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; if (!isFetchingOptions && APICall) { 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$props4 = this.props, onSearch = _this$props4.onSearch, isPopupOpen = _this$props4.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, _Common2.getSearchString)(searchStr); var valueStrRegex = (0, _Common2.getSearchString)(value); var isSearch = searchStrRegex !== valueStrRegex ? true : false; this.setState({ searchStr: value }, function () { if (!value) { onSearch && onSearch(''); } else if (isSearch && onSearch) { _this4.handleSearchOptions(); } }); } }, { key: "handleScroll", value: function handleScroll(e) { var isNextOptions = this.props.isNextOptions; if (e.target.scrollTop + e.target.offsetHeight > e.target.scrollHeight - 1 && isNextOptions) { this.handleScrollFuncCall(); } } }, { key: "handleScrollFuncCall", value: function handleScrollFuncCall() { var getNextOptions = this.props.getNextOptions; var searchStr = this.state.searchStr; getNextOptions && getNextOptions(searchStr); } }, { key: "handleSelectAll", value: function handleSelectAll(e) { e && e.preventDefault(); var _this$handleFilterSug2 = this.handleFilterSuggestions(), suggestionOptionIds = _this$handleFilterSug2.suggestionOptionIds; var selectedOptionIds = this.state.selectedOptionIds; var newSelectedOptions = suggestionOptionIds.filter(function (id) { return selectedOptionIds.indexOf(id) === -1; }); this.handleChange([].concat(_toConsumableArray(selectedOptionIds), _toConsumableArray(newSelectedOptions))); } }, { key: "handleDeselectAll", value: function handleDeselectAll(e) { e && e.preventDefault(); var highLightedSelectOptions = this.state.highLightedSelectOptions; if (highLightedSelectOptions.length) { this.setState({ highLightedSelectOptions: [], lastHighLightedSelectOption: '' }); } this.handleChange([]); } }, { key: "handleChange", value: function handleChange() { var selectedOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var e = arguments.length > 1 ? arguments[1] : undefined; var _this$props5 = this.props, onChange = _this$props5.onChange, needToCloseOnSelect = _this$props5.needToCloseOnSelect, togglePopup = _this$props5.togglePopup, isSearchClearOnSelect = _this$props5.isSearchClearOnSelect; var searchStr = this.state.searchStr; var newSelectedOptions = selectedOptions.map(function (option) { return (0, _dropDownUtils.extractOptionId)(option); }); onChange(_toConsumableArray(newSelectedOptions)); if (searchStr.trim() != '' && isSearchClearOnSelect) { this.handleSearch(''); } this.searchInput && this.searchInput.focus({ preventScroll: true }); if (needToCloseOnSelect) { e && togglePopup(e); } } }, { key: "handleSelectOption", value: function handleSelectOption(id, val, index, e) { e && e.preventDefault(); var selectedOptionIds = this.state.selectedOptionIds; if (selectedOptionIds.indexOf(id) === -1) { this.handleChange([].concat(_toConsumableArray(selectedOptionIds), [id]), e); } else { this.handleRemoveOption(id); } } }, { key: "handleActive", value: function handleActive(e) { var _this$state7 = this.state, searchStr = _this$state7.searchStr, isActive = _this$state7.isActive; if (!isActive) { this.setState({ isActive: true }); } var _ref4 = e || {}, target = _ref4.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$props6 = this.props, isDisabled = _this$props6.isDisabled, isReadOnly = _this$props6.isReadOnly; this.searchInput && !isDisabled && !isReadOnly && this.searchInput.focus({ preventScroll: true }); } }, { key: "componentDidMount", value: function componentDidMount() { this._isMounted = true; } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this._isMounted = false; } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this$props7 = this.props, groupedOptions = _this$props7.groupedOptions, selectedGroupOptions = _this$props7.selectedGroupOptions, isPopupOpen = _this$props7.isPopupOpen, selectedOptionDetails = _this$props7.selectedOptionDetails, searchStr = _this$props7.searchStr, notifyPopupToggle = _this$props7.notifyPopupToggle; var _this$state8 = this.state, normalizedFormatOptions = _this$state8.normalizedFormatOptions, hoverIndex = _this$state8.hoverIndex; var newOptionIds = this.state.allOptionIds; if (prevProps.groupedOptions != groupedOptions) { var _this$handleGetGroupS2 = this.handleGetGroupSelectOptions(this.props), revampedGroups = _this$handleGetGroupS2.revampedGroups, normalizedAllOptions = _this$handleGetGroupS2.normalizedAllOptions, formatNoramlizer = _this$handleGetGroupS2.normalizedFormatOptions, allOptionIds = _this$handleGetGroupS2.allOptionIds, groupIds = _this$handleGetGroupS2.groupIds, normalizedGroupedOptions = _this$handleGetGroupS2.normalizedGroupedOptions; normalizedFormatOptions = formatNoramlizer; newOptionIds = allOptionIds; this.setState({ revampedGroups: revampedGroups, normalizedAllOptions: normalizedAllOptions, normalizedFormatOptions: formatNoramlizer, allOptionIds: allOptionIds, groupIds: groupIds, groupDetails: normalizedGroupedOptions }); } if (prevProps.selectedOptionDetails != selectedOptionDetails || prevProps.selectedGroupOptions != selectedGroupOptions) { var _this$state9 = this.state, groupDetails = _this$state9.groupDetails, _groupIds = _this$state9.groupIds; var changedSelectedOptionDetails = this.handleSelectedOptionDetailsIdChange(this.props, { groupIds: _groupIds, groupDetails: groupDetails }); var _this$handleGetSelect2 = this.handleGetSelectedOptions(changedSelectedOptionDetails), normalizedSelectedOptions = _this$handleGetSelect2.normalizedSelectedOptions; var _this$handleSelectedO2 = this.handleSelectedOptionIdChange(selectedGroupOptions, normalizedFormatOptions, normalizedSelectedOptions), formatSelectedOptions = _this$handleSelectedO2.formatSelectedOptions, selectedOptionIds = _this$handleSelectedO2.selectedOptionIds; this.setState({ formatSelectedOptions: formatSelectedOptions, selectedOptionIds: selectedOptionIds }); } var hoverId = (0, _Common2.getIsEmptyValue)(newOptionIds[hoverIndex]) ? '' : newOptionIds[hoverIndex]; var selSuggestion = this["suggestion_".concat(hoverId)]; isPopupOpen && (0, _Common2.scrollTo)(this.suggestionContainer, selSuggestion); //When suggestions length less than 5, getNextOptions function call var _this$props8 = this.props, isNextOptions = _this$props8.isNextOptions, getNextOptions = _this$props8.getNextOptions, needLocalSearch = _this$props8.needLocalSearch; var _this$handleFilterSug3 = this.handleFilterSuggestions(), suggestions = _this$handleFilterSug3.suggestionOptionIds; var suggestionsLen = suggestions.length; if (isPopupOpen && suggestionsLen <= 5 && isNextOptions && getNextOptions && !needLocalSearch) { this.handleFetchOptions(getNextOptions, searchStr); } if (prevProps.isPopupOpen != isPopupOpen) { notifyPopupToggle && notifyPopupToggle(isPopupOpen); } } }, { key: "responsiveFunc", value: function responsiveFunc(_ref5) { var mediaQueryOR = _ref5.mediaQueryOR; return { tabletMode: mediaQueryOR([{ maxWidth: 700 }]) }; } }, { key: "render", value: function render() { var _this5 = this; var _this$props9 = this.props, size = _this$props9.size, textBoxSize = _this$props9.textBoxSize, selectedGroupOptions = _this$props9.selectedGroupOptions, placeHolder = _this$props9.placeHolder, dataId = _this$props9.dataId, dataSelectorId = _this$props9.dataSelectorId, isReadOnly = _this$props9.isReadOnly, isDisabled = _this$props9.isDisabled, isPopupOpen = _this$props9.isPopupOpen, isPopupReady = _this$props9.isPopupReady, defaultDropBoxPosition = _this$props9.defaultDropBoxPosition, removeClose = _this$props9.removeClose, needResponsive = _this$props9.needResponsive, animationStyle = _this$props9.animationStyle, needSelectAll = _this$props9.needSelectAll, selectAllText = _this$props9.selectAllText, dropBoxSize = _this$props9.dropBoxSize, position = _this$props9.position, getContainerRef = _this$props9.getContainerRef, isAbsolutePositioningNeeded = _this$props9.isAbsolutePositioningNeeded, positionsOffset = _this$props9.positionsOffset, targetOffset = _this$props9.targetOffset, isRestrictScroll = _this$props9.isRestrictScroll, emptyMessage = _this$props9.emptyMessage, searchEmptyMessage = _this$props9.searchEmptyMessage, renderCustomEmptyState = _this$props9.renderCustomEmptyState, title = _this$props9.title, variant = _this$props9.variant, children = _this$props9.children, listItemSize = _this$props9.listItemSize, isDataLoaded = _this$props9.isDataLoaded, needBorder = _this$props9.needBorder, isNextOptions = _this$props9.isNextOptions, getFooter = _this$props9.getFooter, customClass = _this$props9.customClass, borderColor = _this$props9.borderColor, isPadding = _this$props9.isPadding, htmlId = _this$props9.htmlId, i18nKeys = _this$props9.i18nKeys, a11y = _this$props9.a11y, palette = _this$props9.palette, needEffect = _this$props9.needEffect, autoComplete = _this$props9.autoComplete, getTargetRef = _this$props9.getTargetRef, isFocus = _this$props9.isFocus, limit = _this$props9.limit, customProps = _this$props9.customProps; var _customProps$Suggesti = customProps.SuggestionsProps, SuggestionsProps = _customProps$Suggesti === void 0 ? _Common.DUMMY_OBJECT : _customProps$Suggesti; var _i18nKeys = i18nKeys, _i18nKeys$clearText = _i18nKeys.clearText, clearText = _i18nKeys$clearText === void 0 ? _constants.MULTISELECT_I18N_KEYS.clearText : _i18nKeys$clearText, _i18nKeys$limitReache = _i18nKeys.limitReachedMessage, limitReachedMessage = _i18nKeys$limitReache === void 0 ? _constants.MULTISELECT_I18N_KEYS.limitReachedMessage : _i18nKeys$limitReache; var _a11y$clearLabel = a11y.clearLabel, clearLabel = _a11y$clearLabel === void 0 ? _constants.MULTISELECT_I18N_KEYS.clearText : _a11y$clearLabel; i18nKeys = Object.assign({}, i18nKeys, { emptyText: i18nKeys.emptyText || emptyMessage, searchEmptyText: i18nKeys.searchEmptyText || searchEmptyMessage }); var _this$state10 = this.state, isActive = _this$state10.isActive, searchStr = _this$state10.searchStr, _this$state10$revampe = _this$state10.revampedGroups, revampedGroups = _this$state10$revampe === void 0 ? [] : _this$state10$revampe, selectedOptionIds = _this$state10.selectedOptionIds, formatSelectedOptions = _this$state10.formatSelectedOptions, hoverIndex = _this$state10.hoverIndex, highLightedSelectOptions = _this$state10.highLightedSelectOptions, isFetchingOptions = _this$state10.isFetchingOptions; var _this$handleFilterSug4 = this.handleFilterSuggestions(), suggestionGroups = _this$handleFilterSug4.suggestionGroups, suggestionOptionIds = _this$handleFilterSug4.suggestionOptionIds; var setAriaId = this.getNextAriaId(); var ariaErrorId = this.getNextAriaId(); var isShowClearIcon = !isReadOnly && !isDisabled && selectedGroupOptions.length > 1; var isEditable = !(isReadOnly || isDisabled); return /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(_MultiSelectModule["default"].wrapper, " ").concat(isDisabled ? _MultiSelectModule["default"].disabled : '', " ").concat(needEffect && !(isDisabled || isReadOnly) ? _MultiSelectModule["default"].effect : ''), "data-id": "".concat(isDisabled ? "".concat(dataId, "_disabled") : isReadOnly ? "".concat(dataId, "_readOnly") : dataId), "data-test-id": "".concat(isDisabled ? "".concat(dataId, "_disabled") : isReadOnly ? "".concat(dataId, "_readOnly") : dataId), "data-title": isDisabled ? title : null, onClick: this.handleInputFocus, "data-selector-id": dataSelectorId }, children ? /*#__PURE__*/_react["default"].createElement(_Layout.Container, { align: "vertical", alignBox: "row", onClick: this.togglePopup, className: customClass, eleRef: getTargetRef }, children) : /*#__PURE__*/_react["default"].createElement(_Layout.Container, { align: "vertical", alignBox: "row", className: "".concat(_MultiSelectModule["default"].container, " ").concat(_MultiSelectModule["default"][size], " ").concat(isActive && needBorder || isEditable && isFocus && needBorder ? _MultiSelectModule["default"].active : '', " ").concat(needBorder ? _MultiSelectModule["default"].hasBorder : '', " ").concat(_MultiSelectModule["default"]["borderColor_".concat(borderColor)], " ").concat(custo