@zohodesk/components
Version:
In this Package, we Provide Some Basic Components to Build Web App
219 lines (185 loc) • 13.3 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 _defaultProps = require("./props/defaultProps");
var _propTypes = require("./props/propTypes");
var _icons = require("@zohodesk/icons");
var _Avatar = _interopRequireDefault(require("../Avatar/Avatar"));
var _Layout = require("../Layout");
var _Button = _interopRequireDefault(require("../semantic/Button/Button"));
var _IdProvider = require("../Provider/IdProvider");
var _TagModule = _interopRequireDefault(require("./Tag.module.css"));
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 _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 css-modules/no-unused-class: [2, { markAsUsed: ['small', 'medium',
* 'closedanger', 'closeprimary', 'default', 'danger', 'secondary',
* 'pureDotted', 'primaryDotted'] }]
*/
/*
* eslint css-modules/no-unused-class: [2, { markAsUsed: ['small', 'medium',
* 'closedanger', 'closeprimary', 'default', 'danger', 'secondary',
* 'pureDotted', 'primaryDotted'] }]
*/
var Tag = /*#__PURE__*/function (_PureComponent) {
_inherits(Tag, _PureComponent);
var _super = _createSuper(Tag);
function Tag(props) {
var _this;
_classCallCheck(this, Tag);
_this = _super.call(this, props);
_this.handleSelect = _this.handleSelect.bind(_assertThisInitialized(_this));
_this.handleRemove = _this.handleRemove.bind(_assertThisInitialized(_this));
_this.getRef = _this.getRef.bind(_assertThisInitialized(_this));
_this.getNextAriaId = (0, _IdProvider.getUniqueId)(_assertThisInitialized(_this));
return _this;
}
_createClass(Tag, [{
key: "handleSelect",
value: function handleSelect(e) {
var _this$props = this.props,
id = _this$props.id,
_this$props$onSelectT = _this$props.onSelectTag,
onSelectTag = _this$props$onSelectT === void 0 ? null : _this$props$onSelectT;
e && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation();
onSelectTag && onSelectTag(id, e);
}
}, {
key: "handleRemove",
value: function handleRemove(e) {
var _this$props2 = this.props,
onRemove = _this$props2.onRemove,
id = _this$props2.id;
e && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation();
onRemove && onRemove(id, e);
}
}, {
key: "getRef",
value: function getRef(el) {
var _this$props3 = this.props,
getRef = _this$props3.getRef,
id = _this$props3.id;
getRef && getRef(el, id);
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
imageURL = _this$props4.imageURL,
hasAvatar = _this$props4.hasAvatar,
text = _this$props4.text,
onRemove = _this$props4.onRemove,
initial = _this$props4.initial,
active = _this$props4.active,
size = _this$props4.size,
rounded = _this$props4.rounded,
palette = _this$props4.palette,
disabled = _this$props4.disabled,
closePalette = _this$props4.closePalette,
onSelectTag = _this$props4.onSelectTag,
closeTitle = _this$props4.closeTitle,
dataId = _this$props4.dataId,
dataSelectorId = _this$props4.dataSelectorId,
iconName = _this$props4.iconName,
iconSize = _this$props4.iconSize,
tooltip = _this$props4.tooltip,
avatarPalette = _this$props4.avatarPalette,
customClass = _this$props4.customClass,
customAttributes = _this$props4.customAttributes,
a11y = _this$props4.a11y,
needEffect = _this$props4.needEffect,
isReadOnly = _this$props4.isReadOnly,
customProps = _this$props4.customProps;
var _customProps$avatarPr = customProps.avatarProps,
avatarProps = _customProps$avatarPr === void 0 ? {} : _customProps$avatarPr,
_customProps$iconProp = customProps.iconProps,
iconProps = _customProps$iconProp === void 0 ? {} : _customProps$iconProp;
var _customClass$customTa = customClass.customTag,
customTag = _customClass$customTa === void 0 ? '' : _customClass$customTa,
_customClass$customTa2 = customClass.customTagClose,
customTagClose = _customClass$customTa2 === void 0 ? '' : _customClass$customTa2,
_customClass$customTa3 = customClass.customTagIcon,
customTagIcon = _customClass$customTa3 === void 0 ? '' : _customClass$customTa3,
_customClass$customAv = customClass.customAvatar,
customAvatar = _customClass$customAv === void 0 ? '' : _customClass$customAv;
var textSizes = size === 'small' ? _TagModule["default"].smalltext : _TagModule["default"].mediumtext;
var getAriaId = this.getNextAriaId();
var _a11y$clearLabel = a11y.clearLabel,
clearLabel = _a11y$clearLabel === void 0 ? 'Delete' : _a11y$clearLabel;
var isDarkPalette = palette === 'dark';
return /*#__PURE__*/_react["default"].createElement("div", _extends({
className: "".concat(_TagModule["default"].container, " ").concat(needEffect && !isReadOnly ? _TagModule["default"].effect : _TagModule["default"].readonly, " ").concat(active ? "".concat(_TagModule["default"].selected, " ").concat(_TagModule["default"]["active".concat(palette)]) : '', " ").concat(onRemove ? active ? "".concat(_TagModule["default"]["active".concat(size, "Effect")]) : '' : '', " ").concat(_TagModule["default"][size], " ").concat(rounded ? _TagModule["default"].lgRadius : _TagModule["default"].smRadius, " ").concat(_TagModule["default"][palette], " ").concat(disabled ? _TagModule["default"].disabled : '', " ").concat(onSelectTag ? _TagModule["default"].pointer : '', " ").concat(customTag),
"data-id": active ? "".concat(dataId, "_TagSelected") : "".concat(dataId, "_Tag"),
"data-test-id": active ? "".concat(dataId, "_TagSelected") : "".concat(dataId, "_Tag"),
onClick: this.handleSelect,
ref: this.getRef,
"data-title": tooltip ? tooltip : text,
tabIndex: disabled ? '-1' : '0',
"aria-labelledby": getAriaId,
"data-selector-id": dataSelectorId
}, customAttributes), hasAvatar ? /*#__PURE__*/_react["default"].createElement("div", {
className: _TagModule["default"].avatar
}, /*#__PURE__*/_react["default"].createElement(_Avatar["default"], _extends({
palette: isDarkPalette ? 'info' : avatarPalette,
name: initial,
size: "small",
src: imageURL,
textPalette: isDarkPalette ? 'white' : '',
customClass: customAvatar
}, avatarProps))) : null, iconName ? /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(_TagModule["default"].icon, " ").concat(customTagIcon),
"aria-hidden": true
}, /*#__PURE__*/_react["default"].createElement(_icons.Icon, _extends({
name: iconName,
size: iconSize
}, iconProps))) : null, /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(_TagModule["default"].text, " ").concat(textSizes),
"aria-hidden": true,
id: getAriaId
}, text), onRemove && /*#__PURE__*/_react["default"].createElement(_Button["default"], {
customClass: "".concat(_TagModule["default"].close, " ").concat(active ? _TagModule["default"]["active".concat(closePalette, "Close")] : '', "\n ").concat(rounded ? _TagModule["default"].lgRadiusClose : _TagModule["default"].smRadiusClose, " ").concat(isDarkPalette ? _TagModule["default"].darkTagClose : '', "\n ").concat(customTagClose, " ").concat(_TagModule["default"]["close".concat(closePalette)]),
dataId: "".concat(dataId, "_RemoveTag"),
title: closeTitle,
onClick: this.handleRemove,
a11y: {
ariaLabel: clearLabel
}
}, /*#__PURE__*/_react["default"].createElement(_Layout.Container, {
align: "both"
}, /*#__PURE__*/_react["default"].createElement(_icons.Icon, {
isBold: true,
name: "ZD-close2",
size: "8"
}))));
}
}]);
return Tag;
}(_react.PureComponent);
exports["default"] = Tag;
Tag.defaultProps = _defaultProps.defaultProps;
Tag.propTypes = _propTypes.propTypes; // if (__DOCS__) {
// Tag.docs = {
// componentGroup: 'Molecule',
// folderName: 'Style Guide'
// };
// }