UNPKG

@elastic/eui

Version:

Elastic UI Component Library

173 lines (169 loc) 7.53 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["label", "isActive", "isDisabled", "href", "target", "rel", "className", "css", "iconType", "icon", "iconProps", "extraAction", "onClick", "size", "color", "showToolTip", "wrapText", "buttonRef", "toolTipText"], _excluded2 = ["iconType", "alwaysShow", "isDisabled"]; /* * 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. */ import React, { Fragment } from 'react'; import classNames from 'classnames'; import { EuiIcon } from '../icon'; import { EuiToolTip } from '../tool_tip'; import { useInnerText } from '../inner_text'; import { EuiListGroupItemExtraAction } from './list_group_item_extra_action'; import { getSecureRelForTarget, useEuiTheme, cloneElementWithCss } from '../../services'; import { validateHref } from '../../services/security/href_validator'; import { euiListGroupItemStyles, euiListGroupItemIconStyles, euiListGroupItemInnerStyles, euiListGroupItemTooltipStyles, euiListGroupItemLabelStyles } from './list_group_item.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var SIZES = ['xs', 's', 'm', 'l']; export var COLORS = ['primary', 'text', 'subdued']; export var EuiListGroupItem = function EuiListGroupItem(_ref) { 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, target = _ref.target, rel = _ref.rel, className = _ref.className, customCss = _ref.css, iconType = _ref.iconType, icon = _ref.icon, iconProps = _ref.iconProps, extraAction = _ref.extraAction, onClick = _ref.onClick, _ref$size = _ref.size, size = _ref$size === void 0 ? 'm' : _ref$size, _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, rest = _objectWithoutProperties(_ref, _excluded); var isClickable = !!(href || onClick); var isHrefValid = !href || validateHref(href); var isDisabled = _isDisabled || !isHrefValid; var euiTheme = useEuiTheme(); var iconStyles = euiListGroupItemIconStyles(euiTheme); var cssIconStyles = [iconStyles.euiListGroupItem__icon, iconProps === null || iconProps === void 0 ? void 0 : iconProps.css]; var iconNode; if (iconType) { iconNode = ___EmotionJSX(EuiIcon, _extends({ color: "inherit" // forces the icon to inherit its parent color }, iconProps, { type: iconType, className: classNames('euiListGroupItem__icon', iconProps === null || iconProps === void 0 ? void 0 : iconProps.className), css: cssIconStyles })); 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 = cloneElementWithCss(icon, { css: cssIconStyles, className: classNames('euiListGroupItem__icon', icon.props.className) }); } var extraActionNode; if (extraAction) { var _iconType = extraAction.iconType, alwaysShow = extraAction.alwaysShow, actionIsDisabled = extraAction.isDisabled, _rest = _objectWithoutProperties(extraAction, _excluded2); // EuiListGroupItemExtraActionProps extends EuiButtonIconPropsForButton // which doesn't have the color `subdued` so we need to assign a valid color // the most similar is `text` so we'll use that var extraActionColor = color === 'subdued' ? 'text' : color; extraActionNode = ___EmotionJSX(EuiListGroupItemExtraAction, _extends({ color: extraActionColor, iconType: _iconType, alwaysShow: alwaysShow }, _rest, { isDisabled: actionIsDisabled, parentIsDisabled: isDisabled })); } var labelStyles = euiListGroupItemLabelStyles(); var cssLabelStyles = [labelStyles.euiListGroupItem__label, wrapText ? labelStyles.wrapText : labelStyles.truncate]; // 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 = useInnerText(), _useInnerText2 = _slicedToArray(_useInnerText, 2), ref = _useInnerText2[0], innerText = _useInnerText2[1]; var shouldRenderTitle = !wrapText && !showToolTip; var labelContent = shouldRenderTitle ? ___EmotionJSX("span", { ref: ref, className: "euiListGroupItem__label", css: cssLabelStyles, title: typeof label === 'string' ? label : innerText }, label) : ___EmotionJSX("span", { className: "euiListGroupItem__label", css: cssLabelStyles }, label); // Handle the variety of interaction behavior var itemContent; var innerStyles = euiListGroupItemInnerStyles(euiTheme); var cssInnerStyles = [innerStyles.euiListGroupItem__inner, innerStyles[size], !isDisabled && innerStyles[color], isActive && innerStyles.isActive, isDisabled && innerStyles.isDisabled, isClickable && !isDisabled && innerStyles.isClickable]; if (href && !isDisabled) { itemContent = ___EmotionJSX("a", _extends({ className: "euiListGroupItem__button", css: cssInnerStyles, href: href, target: target, rel: getSecureRelForTarget({ href: href, rel: rel, target: target }), onClick: onClick }, rest), iconNode, labelContent); } else if (href && isDisabled || onClick) { itemContent = ___EmotionJSX("button", _extends({ type: "button", className: "euiListGroupItem__button", css: cssInnerStyles, disabled: isDisabled, onClick: onClick, ref: buttonRef }, rest), iconNode, labelContent); } else { itemContent = ___EmotionJSX("span", _extends({ className: "euiListGroupItem__text", css: cssInnerStyles }, rest), iconNode, labelContent); } var styles = euiListGroupItemStyles(euiTheme); var cssStyles = [styles.euiListGroupItem, !isDisabled && isActive && styles.colors.isActive[color], !isDisabled && isClickable && styles.colors.isClickable[color], styles[size], customCss]; var classes = classNames('euiListGroupItem', className); if (showToolTip) { var tooltipStyles = euiListGroupItemTooltipStyles(); var cssTooltipStyles = [tooltipStyles.euiListGroupItem__tooltip]; itemContent = ___EmotionJSX("li", { className: classes, css: cssStyles }, ___EmotionJSX(EuiToolTip, { anchorClassName: "euiListGroupItem__tooltip", anchorProps: { css: cssTooltipStyles }, content: toolTipText !== null && toolTipText !== void 0 ? toolTipText : label, position: "right", delay: "long" }, itemContent)); } else { itemContent = ___EmotionJSX("li", { className: classes, css: cssStyles }, itemContent, extraActionNode); } return ___EmotionJSX(Fragment, null, itemContent); };