UNPKG

@elastic/eui

Version:

Elastic UI Component Library

259 lines (257 loc) 15.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.PADDING_SIZES = exports.EuiSelectableListItem = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireWildcard(require("react")); var _common = require("../../common"); var _i18n = require("../../i18n"); var _icon = require("../../icon"); var _accessibility = require("../../accessibility"); var _badge = require("../../badge"); var _react2 = require("@emotion/react"); var _excluded = ["children", "className", "disabled", "checked", "isFocused", "showIcons", "prepend", "append", "allowExclusions", "onFocusBadge", "paddingSize", "role", "searchable", "textWrap"], _excluded2 = ["children", "className"]; function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(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. */ 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' }; var PADDING_SIZES = (0, _common.keysOf)(paddingSizeToClassNameMap); exports.PADDING_SIZES = PADDING_SIZES; var EuiSelectableListItem = /*#__PURE__*/function (_Component) { (0, _inherits2.default)(EuiSelectableListItem, _Component); var _super = _createSuper(EuiSelectableListItem); function EuiSelectableListItem(props) { var _this; (0, _classCallCheck2.default)(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 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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; } (0, _createClass2.default)(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 = (0, _objectWithoutProperties2.default)(_this$props, _excluded); var classes = (0, _classnames.default)('euiSelectableListItem', { 'euiSelectableListItem-isFocused': isFocused }, paddingSizeToClassNameMap[paddingSize], className); var textClasses = (0, _classnames.default)('euiSelectableListItem__text', (0, _defineProperty2.default)({}, "euiSelectableListItem__text--".concat(textWrap), textWrap)); var optionIcon; if (showIcons) { var _resolveIconAndColor = resolveIconAndColor(checked), icon = _resolveIconAndColor.icon, color = _resolveIconAndColor.color; optionIcon = (0, _react2.jsx)(_icon.EuiIcon, { className: "euiSelectableListItem__icon", color: color, type: icon }); } var state; var instructions; var screenReaderStrings = { checked: { state: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.checkedOption", default: "Checked option." }), instructions: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.checkOptionInstructions", default: "To check this option, press Enter." }) }, unchecked: { instructions: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.uncheckOptionInstructions", default: "To uncheck this option, press Enter." }) }, excluded: { state: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.excludedOption", default: "Excluded option." }), instructions: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.excludeOptionInstructions", default: "To exclude this option, press Enter." }) }, mixed: { state: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.mixedOption", default: "Mixed (indeterminate) option." }), instructions: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.mixedOptionInstructions", default: "To check this option for all, press Enter once." }), uncheckInstructions: (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiSelectableListItem.mixedOptionUncheckInstructions", default: "To uncheck this option for all, press Enter twice." }), excludeInstructions: (0, _react2.jsx)(_i18n.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 = (0, _react2.jsx)(_react.default.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 = (0, _react2.jsx)("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 = (0, _react2.jsx)(_badge.EuiBadge, (0, _extends2.default)({ className: "euiSelectableListItem__onFocusBadge" }, defaultOnFocusBadgeProps)); } else if (!!onFocusBadge && onFocusBadge !== false) { var _children = onFocusBadge.children, _className = onFocusBadge.className, restBadgeProps = (0, _objectWithoutProperties2.default)(onFocusBadge, _excluded2); onFocusBadgeNode = (0, _react2.jsx)(_badge.EuiBadge, (0, _extends2.default)({ className: (0, _classnames.default)('euiSelectableListItem__onFocusBadge', _className) }, defaultOnFocusBadgeProps, restBadgeProps), _children); } // Only display the append wrapper if append exists or isFocused if (append || isFocused && !disabled) { appendNode = (0, _react2.jsx)("span", { className: "euiSelectableListItem__append" }, append, " ", isFocused && !disabled ? onFocusBadgeNode : null); } } var screenReaderText = (state || instructions) && (0, _react2.jsx)(_accessibility.EuiScreenReaderOnly, null, (0, _react2.jsx)("div", null, state || instructions ? '. ' : null, state, state && instructions ? ' ' : null, instructions)); return (0, _react2.jsx)("li", (0, _extends2.default)({ 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), (0, _react2.jsx)("span", { className: "euiSelectableListItem__content" }, optionIcon, prependNode, (0, _react2.jsx)("span", { className: textClasses }, children, screenReaderText), appendNode)); } }]); return EuiSelectableListItem; }(_react.Component); exports.EuiSelectableListItem = EuiSelectableListItem; (0, _defineProperty2.default)(EuiSelectableListItem, "defaultProps", { showIcons: true, onFocusBadge: true, textWrap: 'truncate' });