UNPKG

@elastic/eui

Version:

Elastic UI Component Library

196 lines (194 loc) 9.55 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.EuiButtonGroupButton = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _services = require("../../../services"); var _button = require("../../../global_styling/mixins/_button"); var _inner_text = require("../../inner_text"); var _button_display = require("../button_display/_button_display"); var _button_group_button = require("./button_group_button.styles"); var _tool_tip = require("../../../components/tool_tip"); var _react2 = require("@emotion/react"); var _excluded = ["className", "id", "isDisabled", "isIconOnly", "isSelected", "label", "value", "size", "color", "toolTipContent", "toolTipProps", "contentProps"]; /* * 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 EuiButtonGroupButton = exports.EuiButtonGroupButton = function EuiButtonGroupButton(_ref) { var className = _ref.className, id = _ref.id, isDisabled = _ref.isDisabled, isIconOnly = _ref.isIconOnly, isSelected = _ref.isSelected, label = _ref.label, value = _ref.value, size = _ref.size, _ref$color = _ref.color, _color = _ref$color === void 0 ? 'primary' : _ref$color, toolTipContent = _ref.toolTipContent, toolTipProps = _ref.toolTipProps, contentProps = _ref.contentProps, rest = (0, _objectWithoutProperties2.default)(_ref, _excluded); var isCompressed = size === 'compressed'; var color = isDisabled ? 'disabled' : _color; var hasBorder = color !== 'text' && !isCompressed; var display = isSelected ? 'fill' : isCompressed || hasBorder ? 'empty' : 'base'; var hasToolTip = !!toolTipContent; var styles = (0, _services.useEuiMemoizedStyles)(_button_group_button.euiButtonGroupButtonStyles); var focusColorStyles = (0, _services.useEuiMemoizedStyles)(_button_group_button._compressedButtonFocusColors); var buttonColorStyles = (0, _button.useEuiButtonColorCSS)({ display: display })[color]; var cssStyles = [styles.euiButtonGroupButton, isIconOnly && styles.iconOnly.iconOnly, isIconOnly && styles.iconOnly[size], !isCompressed && (hasToolTip ? styles.uncompressed.hasToolTip : styles.uncompressed[size]), isCompressed ? styles.compressed : styles.uncompressed.uncompressed, isDisabled && isSelected ? styles.disabledAndSelected : buttonColorStyles, !isDisabled && isCompressed && focusColorStyles[color], hasBorder && styles.hasBorder]; var tooltipWrapperStyles = [styles.tooltipWrapper, !isCompressed && styles.uncompressed[size]]; var contentStyles = [styles.content.euiButtonGroupButton__content, isCompressed && styles.content.compressed]; var textStyles = [isIconOnly ? styles.text.euiButtonGroupButton__iconOnly : styles.text.euiButtonGroupButton__text]; var buttonClasses = (0, _classnames.default)('euiButtonGroupButton', { 'euiButtonGroupButton-isSelected': isSelected, 'euiButtonGroupButton-isIconOnly': isIconOnly }, className); /** * Because the selected buttons also increase their text weight to 'bold', * we don't want the whole button size to shift when selected, so we determine * the base width of the button via the `euiTextShift()` method in SASS. */ var _useInnerText = (0, _inner_text.useInnerText)(), _useInnerText2 = (0, _slicedToArray2.default)(_useInnerText, 2), buttonTextRef = _useInnerText2[0], innerText = _useInnerText2[1]; return (0, _react2.jsx)(EuiButtonGroupButtonWithToolTip, { toolTipContent: toolTipContent, toolTipProps: toolTipProps, wrapperCss: tooltipWrapperStyles, isSelected: isSelected }, (0, _react2.jsx)(_button_display.EuiButtonDisplay, (0, _extends2.default)({ css: cssStyles, className: buttonClasses, isDisabled: isDisabled, size: size === 'compressed' ? 's' : size, contentProps: _objectSpread(_objectSpread({}, contentProps), {}, { css: [contentStyles, contentProps === null || contentProps === void 0 ? void 0 : contentProps.css] }), textProps: { css: textStyles, ref: buttonTextRef, 'data-text': innerText }, title: innerText, "data-test-subj": id, isSelected: isSelected }, rest), label)); }; EuiButtonGroupButton.propTypes = { /** * Each option must have a unique `id` for maintaining selection */ id: _propTypes.default.string.isRequired, /** * Each option must have a `label` even for icons which will be applied as the `aria-label` */ label: _propTypes.default.node.isRequired, /** * The value of the radio input. */ value: _propTypes.default.any, /** * The type of the underlying HTML button */ type: _propTypes.default.any, /** * By default, will use the button text for the native browser title. * * This can be either customized or unset via `title: ''` if necessary. */ title: _propTypes.default.any, /** * Optional custom tooltip content for the button */ toolTipContent: _propTypes.default.node, /** * Optional props to pass to the underlying **[EuiToolTip](/#/display/tooltip)** */ toolTipProps: _propTypes.default.any, className: _propTypes.default.string, "aria-label": _propTypes.default.string, "data-test-subj": _propTypes.default.string, css: _propTypes.default.any, /** * Controls the disabled behavior via the native `disabled` attribute. */ isDisabled: _propTypes.default.bool, /** * NOTE: Beta feature, may be changed or removed in the future * * Changes the native `disabled` attribute to `aria-disabled` to preserve focusability. * This results in a semantically disabled button without the default browser handling of the disabled state. * * Use e.g. when a disabled button should have a tooltip. */ hasAriaDisabled: _propTypes.default.bool, /** * Styles the selected button to look selected (usually with `fill`) */ isSelected: _propTypes.default.bool, /** * Inherit from EuiButtonGroup */ color: _propTypes.default.any.isRequired, /** * Inherit from EuiButtonGroup */ size: _propTypes.default.any.isRequired, /** * Inherit from EuiButtonGroup */ isIconOnly: _propTypes.default.bool.isRequired, /** * Inherit from EuiButtonGroup */ onClick: _propTypes.default.func.isRequired, contentProps: _propTypes.default.shape({ className: _propTypes.default.string, "aria-label": _propTypes.default.string, "data-test-subj": _propTypes.default.string, css: _propTypes.default.any }) }; var EuiButtonGroupButtonWithToolTip = function EuiButtonGroupButtonWithToolTip(_ref2) { var _toolTipProps$anchorP, _toolTipProps$anchorP2; var toolTipContent = _ref2.toolTipContent, toolTipProps = _ref2.toolTipProps, wrapperCss = _ref2.wrapperCss, isSelected = _ref2.isSelected, children = _ref2.children; return toolTipContent ? (0, _react2.jsx)(_tool_tip.EuiToolTip, (0, _extends2.default)({ content: toolTipContent, position: "top" }, toolTipProps, { anchorProps: _objectSpread(_objectSpread({}, toolTipProps === null || toolTipProps === void 0 ? void 0 : toolTipProps.anchorProps), {}, { className: (0, _classnames.default)('euiButtonGroup__tooltipWrapper', { 'euiButtonGroup__tooltipWrapper-isSelected': isSelected }, toolTipProps === null || toolTipProps === void 0 || (_toolTipProps$anchorP = toolTipProps.anchorProps) === null || _toolTipProps$anchorP === void 0 ? void 0 : _toolTipProps$anchorP.className), css: [wrapperCss, toolTipProps === null || toolTipProps === void 0 || (_toolTipProps$anchorP2 = toolTipProps.anchorProps) === null || _toolTipProps$anchorP2 === void 0 ? void 0 : _toolTipProps$anchorP2.css] }) }), children) : children; }; EuiButtonGroupButtonWithToolTip.propTypes = { children: _propTypes.default.element.isRequired, wrapperCss: _propTypes.default.any.isRequired, isSelected: _propTypes.default.bool };