@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
288 lines (242 loc) • 15.1 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"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = require("./props/propTypes");
var _defaultProps = require("./props/defaultProps");
var _virtualizer = require("@zohodesk/virtualizer");
var _ListItem = _interopRequireDefault(require("../ListItem/ListItem"));
var _ListItemWithAvatar = _interopRequireDefault(require("../ListItem/ListItemWithAvatar"));
var _ListItemWithIcon = _interopRequireDefault(require("../ListItem/ListItemWithIcon"));
var _Layout = require("../Layout");
var _Common = require("../utils/Common");
var _utils = require("@zohodesk/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 _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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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); }
function SuggestionsVirtualizerContainer(_ref) {
var eleRef = _ref.eleRef,
children = _ref.children,
setVirtualizerContainerRefFunction = _ref.setVirtualizerContainerRefFunction;
(0, _react.useMemo)(function () {
typeof setVirtualizerContainerRefFunction === 'function' && setVirtualizerContainerRefFunction(eleRef);
}, [eleRef, setVirtualizerContainerRefFunction]);
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, children);
}
var Suggestions = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Suggestions, _React$PureComponent);
var _super = _createSuper(Suggestions);
function Suggestions(props) {
var _this;
_classCallCheck(this, Suggestions);
_this = _super.call(this, props);
_this.renderSuggestionList = _this.renderSuggestionList.bind(_assertThisInitialized(_this));
_this.renderVirtualizerSuggestionListItem = _this.renderVirtualizerSuggestionListItem.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(Suggestions, [{
key: "renderSuggestionList",
value: function renderSuggestionList(_ref2) {
var suggestion = _ref2.suggestion,
index = _ref2.index,
ref = _ref2.ref;
var _this$props = this.props,
getRef = _this$props.getRef,
hoverOption = _this$props.hoverOption,
onClick = _this$props.onClick,
onMouseEnter = _this$props.onMouseEnter,
needTick = _this$props.needTick,
needBorder = _this$props.needBorder,
_this$props$selectedO = _this$props.selectedOptions,
selectedOptions = _this$props$selectedO === void 0 ? _Common.DUMMY_ARRAY : _this$props$selectedO,
renderBeforeChildren = _this$props.renderBeforeChildren,
activeId = _this$props.activeId,
hoverId = _this$props.hoverId,
listItemSize = _this$props.listItemSize,
avatarPalette = _this$props.avatarPalette,
palette = _this$props.palette,
a11y = _this$props.a11y,
needMultiLineText = _this$props.needMultiLineText,
limit = _this$props.limit,
limitReachedMessage = _this$props.limitReachedMessage;
var id = suggestion.id,
value = suggestion.value,
secondaryValue = suggestion.secondaryValue,
photoURL = suggestion.photoURL,
icon = suggestion.icon,
optionType = suggestion.optionType,
iconSize = suggestion.iconSize,
isDisabled = suggestion.isDisabled,
listItemProps = suggestion.listItemProps,
_suggestion$listItemC = suggestion.listItemCustomProps,
listItemCustomProps = _suggestion$listItemC === void 0 ? _Common.DUMMY_OBJECT : _suggestion$listItemC;
var isActive = activeId === id || selectedOptions.indexOf(id) >= 0;
var isHighlight = hoverOption === index || id === hoverId ? true : false;
var selectedOptionsLength = selectedOptions.length;
var isLimitReached = selectedOptionsLength >= limit && !isActive;
var list_a11y = Object.assign({}, a11y, {
ariaSelected: isActive,
ariaLabel: value,
'data-a11y-list-active': isHighlight
});
var commonProps = _objectSpread({
isDisabled: isDisabled ? isDisabled : isLimitReached,
needMultiLineText: needMultiLineText
}, listItemCustomProps);
if (listItemProps) {
commonProps.customProps = {
ListItemProps: _objectSpread({}, listItemProps)
};
}
if (isLimitReached) {
commonProps.disableTitle = limitReachedMessage;
}
function getListItemRef(ele, index, id) {
ref && ref(ele);
if (typeof getRef === 'function') {
getRef(ele, index, id);
}
}
if (optionType === 'avatar') {
return /*#__PURE__*/_react["default"].createElement(_ListItemWithAvatar["default"], _extends({}, commonProps, {
autoHover: false,
getRef: getListItemRef,
highlight: isHighlight,
id: id,
imgSrc: photoURL,
key: "".concat(id, "avatarListItem"),
name: value,
onClick: onClick,
onMouseEnter: onMouseEnter,
value: value,
title: value,
needTick: needTick,
needBorder: needBorder,
active: isActive,
size: listItemSize,
avatarPalette: avatarPalette,
palette: palette,
a11y: list_a11y,
secondaryValue: secondaryValue
}));
} else if (optionType === 'icon') {
return /*#__PURE__*/_react["default"].createElement(_ListItemWithIcon["default"], _extends({}, commonProps, {
autoHover: false,
getRef: getListItemRef,
highlight: isHighlight,
id: id,
key: "".concat(id, "iconListItem"),
onClick: onClick,
onMouseEnter: onMouseEnter,
value: value,
title: value,
iconName: icon,
needTick: needTick,
needBorder: needBorder,
active: isActive,
iconSize: iconSize,
size: listItemSize,
palette: palette,
a11y: list_a11y,
secondaryValue: secondaryValue
}));
}
return /*#__PURE__*/_react["default"].createElement(_ListItem["default"], _extends({}, commonProps, {
autoHover: false,
getRef: getListItemRef,
highlight: isHighlight,
id: id,
key: "".concat(id, "listItem"),
onClick: onClick,
onMouseEnter: onMouseEnter,
value: value,
title: value,
needTick: needTick,
needBorder: needBorder,
active: isActive,
size: listItemSize,
palette: palette,
a11y: list_a11y,
secondaryValue: secondaryValue,
renderBeforeChildren: (0, _utils.renderNode)(renderBeforeChildren, suggestion)
}));
}
}, {
key: "renderVirtualizerSuggestionListItem",
value: function renderVirtualizerSuggestionListItem(_ref3) {
var index = _ref3.index,
ref = _ref3.ref;
var suggestions = this.props.suggestions;
var suggestion = suggestions[index];
return this.renderSuggestionList({
suggestion: suggestion,
index: index,
ref: ref
});
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
suggestions = _this$props2.suggestions,
dataId = _this$props2.dataId,
className = _this$props2.className,
isVirtualizerEnabled = _this$props2.isVirtualizerEnabled,
htmlId = _this$props2.htmlId,
a11y = _this$props2.a11y,
getVirtualizerPublicMethods = _this$props2.getVirtualizerPublicMethods,
setVirtualizerContainerRefFunction = _this$props2.setVirtualizerContainerRefFunction,
customClass = _this$props2.customClass;
var ariaParentRole = a11y.ariaParentRole,
ariaMultiselectable = a11y.ariaMultiselectable;
var _customClass$containe = customClass.containerClass,
containerClass = _customClass$containe === void 0 ? '' : _customClass$containe;
return /*#__PURE__*/_react["default"].createElement(_Layout.Container, {
isCover: false,
role: ariaParentRole,
id: htmlId,
tabindex: "0",
"aria-multiselectable": ariaMultiselectable,
className: containerClass
}, /*#__PURE__*/_react["default"].createElement(_Layout.Box, {
dataId: "".concat(dataId),
className: className ? className : ''
}, isVirtualizerEnabled ? /*#__PURE__*/_react["default"].createElement(_virtualizer.Virtualizer, {
containerType: SuggestionsVirtualizerContainer,
elementRenderer: this.renderVirtualizerSuggestionListItem,
getExposedPublicMethods: getVirtualizerPublicMethods,
elementsCount: suggestions.length,
isElementsFixedHeight: false,
dataId: "".concat(dataId, "_virtualizer"),
setVirtualizerContainerRefFunction: setVirtualizerContainerRefFunction
}) : suggestions.map(function (suggestion, index) {
return _this2.renderSuggestionList({
suggestion: suggestion,
index: index
});
})));
}
}]);
return Suggestions;
}(_react["default"].PureComponent);
exports["default"] = Suggestions;
Suggestions.propTypes = _propTypes.Suggestions_propTypes;
Suggestions.defaultProps = _defaultProps.Suggestions_defaultProps;