UNPKG

@elastic/eui

Version:

Elastic UI Component Library

249 lines (247 loc) 11.1 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["children", "className", "disabled", "checked", "isFocused", "showIcons", "prepend", "append", "allowExclusions", "onFocusBadge", "paddingSize", "role", "searchable", "textWrap"], _excluded2 = ["children", "className"]; 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 _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; } } /* * 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 React, { Component } from 'react'; import { keysOf } from '../../common'; import { EuiI18n } from '../../i18n'; import { EuiIcon } from '../../icon'; import { EuiScreenReaderOnly } from '../../accessibility'; import { EuiBadge } from '../../badge'; import { jsx as ___EmotionJSX } from "@emotion/react"; function resolveIconAndColor(checked) { switch (checked) { case 'on': return { icon: 'check', color: 'text' }; case 'off': return { icon: 'cross', color: 'text' }; case 'mixed': return { icon: 'minus', color: 'text' }; case undefined: default: return { icon: 'empty' }; } } var paddingSizeToClassNameMap = { none: null, s: 'euiSelectableListItem--paddingSmall' }; export var PADDING_SIZES = keysOf(paddingSizeToClassNameMap); export var EuiSelectableListItem = /*#__PURE__*/function (_Component) { _inherits(EuiSelectableListItem, _Component); var _super = _createSuper(EuiSelectableListItem); function EuiSelectableListItem(props) { var _this; _classCallCheck(this, EuiSelectableListItem); _this = _super.call(this, props); // aria-checked is intended to be used with role="checkbox" but // the MDN documentation lists it as a possibility for role="option". // See https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-checked // and https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/option_role _defineProperty(_assertThisInitialized(_this), "isChecked", function (role, checked) { var rolesThatCanBeMixed = ['option', 'checkbox', 'menuitemcheckbox']; var rolesThatCanBeChecked = [].concat(rolesThatCanBeMixed, ['radio', 'menuitemradio', 'switch']); if (!rolesThatCanBeChecked.includes(role)) return undefined; switch (checked) { case 'on': case 'off': return true; case 'mixed': if (rolesThatCanBeMixed.includes(role)) { return 'mixed'; } else { return false; } default: return false; } }); return _this; } _createClass(EuiSelectableListItem, [{ key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, className = _this$props.className, disabled = _this$props.disabled, checked = _this$props.checked, isFocused = _this$props.isFocused, showIcons = _this$props.showIcons, prepend = _this$props.prepend, append = _this$props.append, allowExclusions = _this$props.allowExclusions, onFocusBadge = _this$props.onFocusBadge, _this$props$paddingSi = _this$props.paddingSize, paddingSize = _this$props$paddingSi === void 0 ? 's' : _this$props$paddingSi, _this$props$role = _this$props.role, role = _this$props$role === void 0 ? 'option' : _this$props$role, searchable = _this$props.searchable, textWrap = _this$props.textWrap, rest = _objectWithoutProperties(_this$props, _excluded); var classes = classNames('euiSelectableListItem', { 'euiSelectableListItem-isFocused': isFocused }, paddingSizeToClassNameMap[paddingSize], className); var textClasses = classNames('euiSelectableListItem__text', _defineProperty({}, "euiSelectableListItem__text--".concat(textWrap), textWrap)); var optionIcon; if (showIcons) { var _resolveIconAndColor = resolveIconAndColor(checked), icon = _resolveIconAndColor.icon, color = _resolveIconAndColor.color; optionIcon = ___EmotionJSX(EuiIcon, { className: "euiSelectableListItem__icon", color: color, type: icon }); } var state; var instructions; var screenReaderStrings = { checked: { state: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.checkedOption", default: "Checked option." }), instructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.checkOptionInstructions", default: "To check this option, press Enter." }) }, unchecked: { instructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.uncheckOptionInstructions", default: "To uncheck this option, press Enter." }) }, excluded: { state: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.excludedOption", default: "Excluded option." }), instructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.excludeOptionInstructions", default: "To exclude this option, press Enter." }) }, mixed: { state: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.mixedOption", default: "Mixed (indeterminate) option." }), instructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.mixedOptionInstructions", default: "To check this option for all, press Enter once." }), uncheckInstructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.mixedOptionUncheckInstructions", default: "To uncheck this option for all, press Enter twice." }), excludeInstructions: ___EmotionJSX(EuiI18n, { token: "euiSelectableListItem.mixedOptionExcludeInstructions", default: "To exclude this option for all, press Enter twice." }) } }; switch (checked) { case 'on': state = screenReaderStrings.checked.state; instructions = allowExclusions ? screenReaderStrings.excluded.instructions : searchable ? screenReaderStrings.unchecked.instructions : undefined; break; case 'off': state = screenReaderStrings.excluded.state; instructions = screenReaderStrings.unchecked.instructions; break; case 'mixed': state = screenReaderStrings.mixed.state; instructions = ___EmotionJSX(React.Fragment, null, screenReaderStrings.mixed.instructions, ' ', allowExclusions ? screenReaderStrings.mixed.excludeInstructions : screenReaderStrings.mixed.uncheckInstructions); break; case undefined: default: instructions = allowExclusions || searchable ? screenReaderStrings.checked.instructions : undefined; break; } var prependNode; if (prepend) { prependNode = ___EmotionJSX("span", { className: "euiSelectableListItem__prepend" }, prepend); } var appendNode; if (append || !!onFocusBadge) { var onFocusBadgeNode; var defaultOnFocusBadgeProps = { 'aria-hidden': true, iconType: 'returnKey', iconSide: 'left', color: 'hollow' }; if (onFocusBadge === true) { onFocusBadgeNode = ___EmotionJSX(EuiBadge, _extends({ className: "euiSelectableListItem__onFocusBadge" }, defaultOnFocusBadgeProps)); } else if (!!onFocusBadge && onFocusBadge !== false) { var _children = onFocusBadge.children, _className = onFocusBadge.className, restBadgeProps = _objectWithoutProperties(onFocusBadge, _excluded2); onFocusBadgeNode = ___EmotionJSX(EuiBadge, _extends({ className: classNames('euiSelectableListItem__onFocusBadge', _className) }, defaultOnFocusBadgeProps, restBadgeProps), _children); } // Only display the append wrapper if append exists or isFocused if (append || isFocused && !disabled) { appendNode = ___EmotionJSX("span", { className: "euiSelectableListItem__append" }, append, " ", isFocused && !disabled ? onFocusBadgeNode : null); } } var screenReaderText = (state || instructions) && ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("div", null, state || instructions ? '. ' : null, state, state && instructions ? ' ' : null, instructions)); return ___EmotionJSX("li", _extends({ role: role, "aria-disabled": disabled, "aria-checked": this.isChecked(role, checked) // Whether the item is "checked" , "aria-selected": !disabled && isFocused // Whether the item has keyboard focus per W3 spec , className: classes }, rest), ___EmotionJSX("span", { className: "euiSelectableListItem__content" }, optionIcon, prependNode, ___EmotionJSX("span", { className: textClasses }, children, screenReaderText), appendNode)); } }]); return EuiSelectableListItem; }(Component); _defineProperty(EuiSelectableListItem, "defaultProps", { showIcons: true, onFocusBadge: true, textWrap: 'truncate' });