UNPKG

@zohodesk/components

Version:

In this Package, we Provide Some Basic Components to Build Web App

408 lines (357 loc) • 20.2 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 _defaultProps = require("./props/defaultProps"); var _propTypes = require("./props/propTypes"); var _Select = require("./Select"); var _Popup = _interopRequireDefault(require("../Popup/Popup")); var _Tag = _interopRequireDefault(require("../Tag/Tag")); var _TextBox = _interopRequireDefault(require("../TextBox/TextBox")); var _TextBoxIcon = _interopRequireDefault(require("../TextBoxIcon/TextBoxIcon")); var _Layout = require("../Layout"); var _Card = _interopRequireWildcard(require("../Card/Card")); var _Suggestions = _interopRequireDefault(require("../MultiSelect/Suggestions")); var _EmptyState = _interopRequireDefault(require("../MultiSelect/EmptyState")); var _DropDownHeading = _interopRequireDefault(require("../DropDown/DropDownHeading")); var _IdProvider = require("../Provider/IdProvider"); var _ResponsiveDropBox = _interopRequireDefault(require("../ResponsiveDropBox/ResponsiveDropBox")); var _CustomResponsive = require("../Responsive/CustomResponsive"); var _Loader = _interopRequireDefault(require("@zohodesk/svg/lib/Loader/Loader")); var _MultiSelectModule = _interopRequireDefault(require("../MultiSelect/MultiSelect.module.css")); var _SelectModule = _interopRequireDefault(require("./Select.module.css")); 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 _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); } var SelectWithAvatarComponent = /*#__PURE__*/function (_SelectComponent) { _inherits(SelectWithAvatarComponent, _SelectComponent); var _super = _createSuper(SelectWithAvatarComponent); function SelectWithAvatarComponent(props) { var _this; _classCallCheck(this, SelectWithAvatarComponent); _this = _super.call(this, props); _this.isSelectWithAvatarComponent = true; _this.handleFormatOptions = _this.handleFormatOptions.bind(_assertThisInitialized(_this)); _this.handleActive = _this.handleActive.bind(_assertThisInitialized(_this)); _this.handleInactive = _this.handleInactive.bind(_assertThisInitialized(_this)); _this.handleSearchChange = _this.handleSearchChange.bind(_assertThisInitialized(_this)); _this.handleRemoveOption = _this.handleRemoveOption.bind(_assertThisInitialized(_this)); _this.getNextAriaId = (0, _IdProvider.getUniqueId)(_assertThisInitialized(_this)); return _this; } _createClass(SelectWithAvatarComponent, [{ key: "handleFormatOptions", value: function handleFormatOptions(props) { var options = props.options, valueField = props.valueField, textField = props.textField, imageField = props.imageField, allowValueFallback = props.allowValueFallback; return this.formatOptions({ options: options, valueField: valueField, textField: textField, imageField: imageField, optionType: 'avatar', allowValueFallback: allowValueFallback }); } }, { key: "handleActive", value: function handleActive(e) { var isActive = this.state.isActive; if (!isActive) { this.setState({ isActive: true }); } var _ref = e || {}, target = _ref.target; target && target.setSelectionRange(target, 0); var onFocus = this.props.onFocus; onFocus && onFocus(this.state.searchStr); } }, { key: "handleInactive", value: function handleInactive() { var isActive = this.state.isActive; if (isActive) { this.setState({ isActive: false }); } } }, { key: "handleSearchChange", value: function handleSearchChange(e) { var isPopupOpen = this.props.isPopupOpen; !isPopupOpen && this.togglePopup(e); this.handleSearch(e); } }, { key: "handleRemoveOption", value: function handleRemoveOption(e) { var keyCode = e.keyCode; var _this$state$searchStr = this.state.searchStr, searchStr = _this$state$searchStr === void 0 ? '' : _this$state$searchStr; var isDefaultSelectValue = this.props.isDefaultSelectValue; if (keyCode === 8 && searchStr.length === 0 && !isDefaultSelectValue) { this.handleChange(''); } else { this.handleKeyDown(e); } } }, { key: "responsiveFunc", value: function responsiveFunc(_ref2) { var mediaQueryOR = _ref2.mediaQueryOR; return { tabletMode: mediaQueryOR([{ maxWidth: 700 }]) }; } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, dropBoxSize = _this$props.dropBoxSize, emptyMessage = _this$props.emptyMessage, searchEmptyMessage = _this$props.searchEmptyMessage, needBorder = _this$props.needBorder, position = _this$props.position, defaultDropBoxPosition = _this$props.defaultDropBoxPosition, getContainerRef = _this$props.getContainerRef, isPopupOpen = _this$props.isPopupOpen, removeClose = _this$props.removeClose, isPopupReady = _this$props.isPopupReady, isAbsolutePositioningNeeded = _this$props.isAbsolutePositioningNeeded, positionsOffset = _this$props.positionsOffset, targetOffset = _this$props.targetOffset, isRestrictScroll = _this$props.isRestrictScroll, animationStyle = _this$props.animationStyle, textBoxSize = _this$props.textBoxSize, textBoxVariant = _this$props.textBoxVariant, maxLength = _this$props.maxLength, isDisabled = _this$props.isDisabled, title = _this$props.title, size = _this$props.size, isReadOnly = _this$props.isReadOnly, placeHolder = _this$props.placeHolder, className = _this$props.className, needResponsive = _this$props.needResponsive, dataId = _this$props.dataId, borderColor = _this$props.borderColor, searchBoxPlaceHolder = _this$props.searchBoxPlaceHolder, searchBoxSize = _this$props.searchBoxSize, needSearch = _this$props.needSearch, textBoxClass = _this$props.textBoxClass, onRemove = _this$props.onRemove, avatarPalette = _this$props.avatarPalette, tagSize = _this$props.tagSize, groupName = _this$props.groupName, i18nKeys = _this$props.i18nKeys, htmlId = _this$props.htmlId, needEffect = _this$props.needEffect, isLoading = _this$props.isLoading, dataSelectorId = _this$props.dataSelectorId, getTargetRef = _this$props.getTargetRef; i18nKeys = Object.assign({}, i18nKeys, { emptyText: i18nKeys.emptyText || emptyMessage, searchEmptyText: i18nKeys.searchEmptyText || searchEmptyMessage }); var _this$state = this.state, hoverIndex = _this$state.hoverIndex, searchStr = _this$state.searchStr, isFetchingOptions = _this$state.isFetchingOptions, selectedId = _this$state.selectedId, isActive = _this$state.isActive, options = _this$state.options; var suggestions = this.handleFilterSuggestions(); var _ref3 = this.normalizedFormatOptions[selectedId] || {}, id = _ref3.id, text = _ref3.value, photoURL = _ref3.photoURL; var setAriaId = this.getNextAriaId(); var ariaErrorId = this.getNextAriaId(); return /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(_MultiSelectModule["default"].wrapper, " ").concat(isDisabled ? _MultiSelectModule["default"].disabled : '', " ").concat(isReadOnly ? _MultiSelectModule["default"].readOnly : '', " ").concat(needEffect && !(isDisabled || isReadOnly) ? _MultiSelectModule["default"].effect : '', " ").concat(className ? className : ''), "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, "data-selector-id": dataSelectorId }, /*#__PURE__*/_react["default"].createElement(_Layout.Container, { align: "vertical", alignBox: "row", className: "".concat(_MultiSelectModule["default"].container, " ").concat(_MultiSelectModule["default"][size], " ").concat(_MultiSelectModule["default"]["borderColor_".concat(borderColor)], " ").concat(isActive && needBorder ? _MultiSelectModule["default"].active : '', " ").concat(textBoxClass ? textBoxClass : '', " ").concat(needBorder ? _MultiSelectModule["default"].hasBorder : ''), eleRef: getTargetRef, onClick: !isDisabled && this.togglePopup }, selectedId ? /*#__PURE__*/_react["default"].createElement(_Layout.Box, { className: _MultiSelectModule["default"].tag, "data-id": "".concat(dataId, "_SelectTag"), "data-test-id": "".concat(dataId, "_SelectTag"), key: "".concat(id, "tag") }, /*#__PURE__*/_react["default"].createElement(_Tag["default"], { avatarPalette: avatarPalette, hasAvatar: true, id: id, imageURL: photoURL, initial: text, onRemove: onRemove, text: text, disabled: isReadOnly || isDisabled, size: tagSize })) : null, /*#__PURE__*/_react["default"].createElement(_Layout.Box, { flexible: true, className: _MultiSelectModule["default"].textBox, dataId: "".concat(dataId, "_textBox") }, /*#__PURE__*/_react["default"].createElement(_TextBox["default"], { a11y: { ariaActivedescendant: selectedId, role: 'combobox', ariaControls: setAriaId, ariaOwns: setAriaId, ariaExpanded: !isReadOnly && !isDisabled && isPopupOpen ? true : false, ariaHaspopup: true, ariaReadonly: true }, isDisabled: isDisabled, needEffect: isReadOnly || isDisabled ? false : true, inputRef: this.valueInputRef, maxLength: maxLength, needBorder: false, onBlur: this.handleInactive, onChange: this.handleSearchChange, onFocus: this.handleActive, onKeyDown: this.handleRemoveOption, onKeyPress: this.handleValueInputChange, placeHolder: !selectedId ? placeHolder : '', size: textBoxSize, variant: textBoxVariant, needReadOnlyStyle: isReadOnly ? true : false, isReadOnly: true, isClickable: isReadOnly || isDisabled ? false : true, borderColor: borderColor, htmlId: htmlId, autoComplete: false }))), !isReadOnly && !isDisabled && isPopupOpen ? /*#__PURE__*/_react["default"].createElement(_CustomResponsive.ResponsiveReceiver, { query: this.responsiveFunc, responsiveId: "Helmet" }, function (_ref4) { var tabletMode = _ref4.tabletMode; return /*#__PURE__*/_react["default"].createElement(_ResponsiveDropBox["default"], { animationStyle: animationStyle, boxPosition: position || "".concat(defaultDropBoxPosition, "Center"), getRef: getContainerRef, isActive: isPopupReady, isAnimate: true, isArrow: false, onClick: removeClose, needResponsive: needResponsive, isAbsolutePositioningNeeded: isAbsolutePositioningNeeded, positionsOffset: positionsOffset, targetOffset: targetOffset, isRestrictScroll: isRestrictScroll, isPadding: false, isResponsivePadding: true, alignBox: "row", dataId: "".concat(dataId, "_dropbox") }, isLoading ? /*#__PURE__*/_react["default"].createElement(_Layout.Container, { align: "both", className: _MultiSelectModule["default"].loader }, /*#__PURE__*/_react["default"].createElement(_Loader["default"], null)) : /*#__PURE__*/_react["default"].createElement(_Layout.Box, { flexible: true }, /*#__PURE__*/_react["default"].createElement(_Card["default"], { onScroll: _this2.handleScroll }, needSearch ? /*#__PURE__*/_react["default"].createElement(_Card.CardHeader, null, /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(_SelectModule["default"].search, " ").concat(_SelectModule["default"][size]) }, /*#__PURE__*/_react["default"].createElement(_TextBoxIcon["default"], { inputRef: _this2.searchInputRef, maxLength: maxLength, onChange: _this2.handleSearch, onKeyDown: _this2.handleKeyDown, placeHolder: searchBoxPlaceHolder, size: searchBoxSize, value: searchStr, onClear: _this2.handleClearSearch, dataId: "".concat(dataId, "_search"), a11y: { ariaAutocomplete: 'list', ariaControls: setAriaId, ariaDescribedby: ariaErrorId }, autoComplete: false }))) : null, /*#__PURE__*/_react["default"].createElement(_Card.CardContent, { shrink: true, customClass: !tabletMode && dropBoxSize ? _MultiSelectModule["default"][dropBoxSize] : '', eleRef: _this2.suggestionContainerRef }, groupName ? /*#__PURE__*/_react["default"].createElement("div", { className: _MultiSelectModule["default"].title }, /*#__PURE__*/_react["default"].createElement(_DropDownHeading["default"], { text: groupName, a11y: { role: 'heading' } })) : null, suggestions.length ? /*#__PURE__*/_react["default"].createElement(_Suggestions["default"], { dataId: "".concat(dataId, "_Options"), activeId: selectedId, suggestions: suggestions, getRef: _this2.suggestionItemRef, hoverOption: hoverIndex, onClick: _this2.handleChange, onMouseEnter: _this2.handleMouseEnter, needTick: true, needBorder: false, selectedOptions: [selectedId], className: _SelectModule["default"].listItemContainer, htmlId: setAriaId, a11y: { ariaParentRole: 'listbox', role: 'option' } }) : /*#__PURE__*/_react["default"].createElement(_EmptyState["default"], { isLoading: isFetchingOptions, options: options, searchString: searchStr, suggestions: suggestions, dataId: dataId, i18nKeys: i18nKeys, htmlId: ariaErrorId }), isFetchingOptions && /*#__PURE__*/_react["default"].createElement(_Layout.Container, { isCover: false, align: "both" }, /*#__PURE__*/_react["default"].createElement(_Loader["default"], null)))))); }) : null); } }]); return SelectWithAvatarComponent; }(_Select.SelectComponent); SelectWithAvatarComponent.propTypes = _propTypes.SelectWithAvatar_propTypes; SelectWithAvatarComponent.defaultProps = _defaultProps.SelectWithAvatar_defaultProps; SelectWithAvatarComponent.displayName = 'SelectWithAvatar'; var SelectWithAvatar = (0, _Popup["default"])(SelectWithAvatarComponent); SelectWithAvatar.defaultProps = SelectWithAvatarComponent.defaultProps; SelectWithAvatar.propTypes = SelectWithAvatarComponent.propTypes; // if (__DOCS__) { // SelectWithAvatar.docs = { // componentGroup: 'Form Elements', // folderName: 'Style Guide' // }; // // eslint-disable-next-line react/forbid-foreign-prop-types // SelectWithAvatar.propTypes = SelectWithAvatarComponent.propTypes; // } var _default = SelectWithAvatar; exports["default"] = _default;