UNPKG

@elastic/eui

Version:

Elastic UI Component Library

176 lines (174 loc) 8.52 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.EuiListGroupItem = exports.COLORS = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _services = require("../../services"); var _href_validator = require("../../services/security/href_validator"); var _inner_text = require("../inner_text"); var _icon = require("../icon"); var _list_item_layout = require("../list_item_layout/_list_item_layout"); var _list_group_item_extra_action = require("./list_group_item_extra_action"); var _list_group_item = require("./list_group_item.styles"); var _react2 = require("@emotion/react"); var _excluded = ["label", "isActive", "isDisabled", "href", "rel", "target", "external", "className", "css", "style", "iconType", "icon", "iconProps", "extraAction", "onClick", "color", "showToolTip", "wrapText", "buttonRef", "toolTipText", "toolTipProps"], _excluded2 = ["iconType", "alwaysShow", "isDisabled"], _excluded3 = ["className", "css"]; /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var COLORS = exports.COLORS = ['primary', 'text', 'subdued']; var EuiListGroupItem = exports.EuiListGroupItem = function EuiListGroupItem(_ref) { var _toolTipProps$anchorP; var label = _ref.label, _ref$isActive = _ref.isActive, isActive = _ref$isActive === void 0 ? false : _ref$isActive, _ref$isDisabled = _ref.isDisabled, _isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled, href = _ref.href, rel = _ref.rel, target = _ref.target, external = _ref.external, className = _ref.className, customCss = _ref.css, style = _ref.style, iconType = _ref.iconType, icon = _ref.icon, iconProps = _ref.iconProps, extraAction = _ref.extraAction, onClick = _ref.onClick, _ref$color = _ref.color, color = _ref$color === void 0 ? 'text' : _ref$color, _ref$showToolTip = _ref.showToolTip, showToolTip = _ref$showToolTip === void 0 ? false : _ref$showToolTip, wrapText = _ref.wrapText, buttonRef = _ref.buttonRef, toolTipText = _ref.toolTipText, toolTipProps = _ref.toolTipProps, rest = (0, _objectWithoutProperties2.default)(_ref, _excluded); var isHrefValid = !href || (0, _href_validator.validateHref)(href); var isDisabled = _isDisabled || !isHrefValid; var iconNode; if (iconType) { iconNode = (0, _react2.jsx)(_icon.EuiIcon, (0, _extends2.default)({ color: "inherit" // forces the icon to inherit its parent color }, iconProps, { type: iconType, className: (0, _classnames.default)('euiListGroupItem__icon', iconProps === null || iconProps === void 0 ? void 0 : iconProps.className), css: iconProps === null || iconProps === void 0 ? void 0 : iconProps.css })); if (icon) { console.warn('Both `iconType` and `icon` were passed to EuiListGroupItem but only one can exist. The `iconType` was used.'); } } else if (icon) { iconNode = (0, _services.cloneElementWithCss)(icon, { css: iconProps === null || iconProps === void 0 ? void 0 : iconProps.css, className: (0, _classnames.default)('euiListGroupItem__icon', icon.props.className) }); } var extraActionNode; if (extraAction) { var _iconType = extraAction.iconType, alwaysShow = extraAction.alwaysShow, actionIsDisabled = extraAction.isDisabled, _rest = (0, _objectWithoutProperties2.default)(extraAction, _excluded2); extraActionNode = (0, _react2.jsx)(_list_group_item_extra_action.EuiListGroupItemExtraAction, (0, _extends2.default)({ color: color === 'subdued' ? 'text' : color, iconType: _iconType, alwaysShow: alwaysShow }, _rest, { isDisabled: actionIsDisabled, parentIsDisabled: isDisabled })); } // Only add the label as the title attribute if it's possibly truncated // Also ensure the value of the title attribute is a string var _useInnerText = (0, _inner_text.useInnerText)(), _useInnerText2 = (0, _slicedToArray2.default)(_useInnerText, 2), ref = _useInnerText2[0], innerText = _useInnerText2[1]; var shouldRenderTitle = !wrapText && !showToolTip; var labelProps = { ref: shouldRenderTitle ? ref : undefined, className: 'euiListGroupItem__label', title: shouldRenderTitle ? typeof label === 'string' ? label : innerText : undefined }; var styles = (0, _services.useEuiMemoizedStyles)(_list_group_item.euiListGroupItemStyles); var cssInnerStyles = [styles.euiListGroupItem__inner, !isDisabled && !isActive && styles[color]]; var itemProps = {}; if (href && !isDisabled) { itemProps = _objectSpread({ element: 'a', className: 'euiListGroupItem__button', css: cssInnerStyles, href: href, target: target, rel: (0, _services.getSecureRelForTarget)({ href: href, rel: rel, target: target }), external: external, onClick: onClick }, rest); } else if (href && isDisabled || onClick) { itemProps = _objectSpread({ element: 'button', type: 'button', className: 'euiListGroupItem__button', css: cssInnerStyles, onClick: onClick, ref: buttonRef }, rest); } else { itemProps = _objectSpread({ element: 'li', className: 'euiListGroupItem__text', css: cssInnerStyles }, rest); } var _itemProps = itemProps, itemClassName = _itemProps.className, itemCss = _itemProps.css, restItemProps = (0, _objectWithoutProperties2.default)(_itemProps, _excluded3); var classes = (0, _classnames.default)('euiListGroupItem', className, itemClassName); var cssStyles = [styles.euiListGroupItem, itemCss, customCss]; var tooltipStyles = _list_group_item.euiListGroupItemTooltipStyles; var _tooltipProps = showToolTip ? _objectSpread(_objectSpread({ content: toolTipText !== null && toolTipText !== void 0 ? toolTipText : label, position: 'right' }, toolTipProps), {}, { anchorClassName: (0, _classnames.default)('euiListGroupItem__tooltip', toolTipProps === null || toolTipProps === void 0 ? void 0 : toolTipProps.anchorClassName), anchorProps: _objectSpread(_objectSpread({}, toolTipProps === null || toolTipProps === void 0 ? void 0 : toolTipProps.anchorProps), {}, { css: [tooltipStyles.euiListGroupItem__tooltip, toolTipProps === null || toolTipProps === void 0 || (_toolTipProps$anchorP = toolTipProps.anchorProps) === null || _toolTipProps$anchorP === void 0 ? void 0 : _toolTipProps$anchorP.css] }) }) : undefined; return (0, _react2.jsx)(_list_item_layout.EuiListItemLayout, (0, _extends2.default)({ className: classes, wrapperElement: "li", css: cssStyles, style: style }, restItemProps, { tooltipProps: _tooltipProps, showIndicator: false, isSelected: isActive, isDisabled: isDisabled, prepend: iconNode, extraAction: extraActionNode, textWrap: wrapText ? 'wrap' : 'truncate', textProps: labelProps }), label); };