UNPKG

@elastic/eui

Version:

Elastic UI Component Library

199 lines (198 loc) 8.62 kB
var _excluded = ["className", "buttonSize", "color", "idSelected", "idToSelectedMap", "isDisabled", "hasAriaDisabled", "isFullWidth", "isIconOnly", "legend", "name", "onChange", "options", "type"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } /* * 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 classNames from 'classnames'; import PropTypes from "prop-types"; import React from 'react'; import { useEuiMemoizedStyles } from '../../../services'; import { EuiScreenReaderOnly } from '../../accessibility'; import { EuiButtonGroupButton } from './button_group_button'; import { euiButtonGroupStyles, euiButtonGroupButtonsStyles } from './button_group.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiButtonGroup = function EuiButtonGroup(_ref) { var className = _ref.className, _ref$buttonSize = _ref.buttonSize, buttonSize = _ref$buttonSize === void 0 ? 's' : _ref$buttonSize, _ref$color = _ref.color, color = _ref$color === void 0 ? 'text' : _ref$color, _ref$idSelected = _ref.idSelected, idSelected = _ref$idSelected === void 0 ? '' : _ref$idSelected, _ref$idToSelectedMap = _ref.idToSelectedMap, idToSelectedMap = _ref$idToSelectedMap === void 0 ? {} : _ref$idToSelectedMap, _ref$isDisabled = _ref.isDisabled, isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled, _ref$hasAriaDisabled = _ref.hasAriaDisabled, hasAriaDisabled = _ref$hasAriaDisabled === void 0 ? false : _ref$hasAriaDisabled, _ref$isFullWidth = _ref.isFullWidth, isFullWidth = _ref$isFullWidth === void 0 ? false : _ref$isFullWidth, _ref$isIconOnly = _ref.isIconOnly, isIconOnly = _ref$isIconOnly === void 0 ? false : _ref$isIconOnly, legend = _ref.legend, name = _ref.name, onChange = _ref.onChange, _ref$options = _ref.options, options = _ref$options === void 0 ? [] : _ref$options, _ref$type = _ref.type, type = _ref$type === void 0 ? 'single' : _ref$type, rest = _objectWithoutProperties(_ref, _excluded); var wrapperCssStyles = [euiButtonGroupStyles.euiButtonGroup, isFullWidth && euiButtonGroupStyles.fullWidth]; var styles = useEuiMemoizedStyles(euiButtonGroupButtonsStyles); var cssStyles = [styles.euiButtonGroup__buttons, isFullWidth && styles.fullWidth, styles[buttonSize]]; var classes = classNames('euiButtonGroup', { 'euiButtonGroup-isDisabled': isDisabled }, className); var typeIsSingle = type === 'single'; var groupDisabledProps = { disabled: hasAriaDisabled ? undefined : isDisabled, 'aria-disabled': hasAriaDisabled ? isDisabled : undefined }; return ___EmotionJSX("fieldset", _extends({ css: wrapperCssStyles, className: classes }, rest, groupDisabledProps), ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("legend", null, legend)), ___EmotionJSX("div", { css: cssStyles, className: "euiButtonGroup__buttons" }, options.map(function (option) { return ___EmotionJSX(EuiButtonGroupButton, _extends({ key: option.id, isDisabled: isDisabled, hasAriaDisabled: hasAriaDisabled }, option, { onClick: typeIsSingle ? function () { return onChange(option.id, option.value); } : function () { return onChange(option.id); }, isSelected: typeIsSingle ? option.id === idSelected : idToSelectedMap[option.id], color: color, size: buttonSize, isIconOnly: isIconOnly })); }))); }; EuiButtonGroup.propTypes = { className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any, isDisabled: PropTypes.bool, hasAriaDisabled: PropTypes.bool, /** * Typical sizing is `s`. Medium `m` size should be reserved for major features. * `compressed` is meant to be used alongside and within compressed forms. */ buttonSize: PropTypes.oneOf(["s", "m", "compressed"]), /** * Expands the whole group to the full width of the container. * Each button gets equal widths no matter the content */ isFullWidth: PropTypes.bool, /** * Hides the label to only show the `iconType` provided by the `option` */ isIconOnly: PropTypes.bool, /** * A hidden group title (required for accessibility) */ legend: PropTypes.string.isRequired, /** * Any of the named color palette options. * * Do not use the following colors for standalone buttons directly, * they exist to serve other components: * - accent * - warning */ color: PropTypes.any, /** * Default for `type` is single so it can also be excluded */ /** * Actual type is `'single' | 'multi'`. * Determines how the selection of the group should be handled. * With `'single'` only one option can be selected at a time (similar to radio group). * With `'multi'` multiple options selected (similar to checkbox group). */ type: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.oneOf(["single", "multi"]), PropTypes.oneOf(["single"])]), PropTypes.oneOf(["multi"])]), /** * An array of {@link EuiButtonGroupOptionProps} */ options: PropTypes.arrayOf(PropTypes.shape({ /** * Each option must have a unique `id` for maintaining selection */ id: PropTypes.string.isRequired, /** * Each option must have a `label` even for icons which will be applied as the `aria-label` */ label: PropTypes.node.isRequired, /** * The value of the radio input. */ value: PropTypes.any, /** * The type of the underlying HTML button */ type: PropTypes.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.any, /** * Optional custom tooltip content for the button */ toolTipContent: PropTypes.node, /** * Optional props to pass to the underlying **[EuiToolTip](/#/display/tooltip)** */ toolTipProps: PropTypes.any, className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any, /** * Controls the disabled behavior via the native `disabled` attribute. */ isDisabled: PropTypes.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.bool }).isRequired).isRequired, /** * @deprecated No longer needed. You can safely remove this prop entirely */ name: PropTypes.string, /** * Styles the selected option to look selected (usually with `fill`) * Required by and only used in `type='single'`. */ idSelected: PropTypes.string, /** * Multi: Returns the `id` of the clicked option */ /** * Single: Returns the `id` of the clicked option and the `value` */ onChange: PropTypes.func, /** * A map of `id`s as keys with the selected boolean values. * Required by and only used in `type='multi'`. */ idToSelectedMap: PropTypes.shape({}) };