UNPKG

@elastic/eui

Version:

Elastic UI Component Library

170 lines (169 loc) 7.96 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["children", "className", "disabled", "checked", "isFocused", "showIcons", "prepend", "append", "allowExclusions", "singleSelection", "onFocusBadge", "role", "searchable", "toolTipContent", "toolTipProps"], _excluded2 = ["children", "className"]; 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) { _defineProperty(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; } /* * 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, { useMemo } from 'react'; import { EuiI18n } from '../../i18n'; import { EuiScreenReaderOnly } from '../../accessibility'; import { EuiBadge } from '../../badge'; import { EuiListItemLayout } from '../../list_item_layout/_list_item_layout'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiSelectableListItem = function EuiSelectableListItem(_ref) { var children = _ref.children, className = _ref.className, disabled = _ref.disabled, checked = _ref.checked, isFocused = _ref.isFocused, _ref$showIcons = _ref.showIcons, showIcons = _ref$showIcons === void 0 ? true : _ref$showIcons, prepend = _ref.prepend, append = _ref.append, allowExclusions = _ref.allowExclusions, _ref$singleSelection = _ref.singleSelection, singleSelection = _ref$singleSelection === void 0 ? true : _ref$singleSelection, _ref$onFocusBadge = _ref.onFocusBadge, onFocusBadge = _ref$onFocusBadge === void 0 ? false : _ref$onFocusBadge, _ref$role = _ref.role, role = _ref$role === void 0 ? 'option' : _ref$role, searchable = _ref.searchable, toolTipContent = _ref.toolTipContent, toolTipProps = _ref.toolTipProps, rest = _objectWithoutProperties(_ref, _excluded); var classes = classNames('euiSelectableListItem', { 'euiSelectableListItem-isFocused': isFocused }, className); var onFocusBadgeNode = useMemo(function () { var defaultOnFocusBadgeProps = { 'aria-hidden': true, iconType: 'return', iconSide: 'left', color: 'hollow' }; if (onFocusBadge === true) { return ___EmotionJSX(EuiBadge, _extends({ className: "euiSelectableListItem__onFocusBadge" }, defaultOnFocusBadgeProps)); } else if (typeof onFocusBadge !== 'boolean' && !!onFocusBadge) { var _children = onFocusBadge.children, _className = onFocusBadge.className, restBadgeProps = _objectWithoutProperties(onFocusBadge, _excluded2); return ___EmotionJSX(EuiBadge, _extends({ className: classNames('euiSelectableListItem__onFocusBadge', _className) }, defaultOnFocusBadgeProps, restBadgeProps), _children); } }, [onFocusBadge]); var screenReaderText = useMemo(function () { 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; } return state || instructions ? ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("div", null, state || instructions ? '. ' : null, state, state && instructions ? ' ' : null, instructions)) : null; }, [checked, searchable, allowExclusions]); var hasToolTip = !!toolTipContent && !disabled; var showOnFocusBadge = isFocused && !disabled && !!onFocusBadgeNode; var listItemLayoutProps = _objectSpread({ element: 'li', role: role, className: classes, checked: checked, isDisabled: disabled, isFocused: !disabled && isFocused, isSelected: checked !== undefined, isSingleSelection: singleSelection, selectionMode: allowExclusions || checked === 'mixed' ? 'checked' : undefined, showIndicator: showIcons, prepend: prepend, prependProps: { className: 'euiSelectableListItem__prepend' }, append: (append || showOnFocusBadge) && ___EmotionJSX(React.Fragment, null, append, showOnFocusBadge ? onFocusBadgeNode : null), appendProps: { className: 'euiSelectableListItem__append' }, contentProps: { className: 'euiSelectableListItem__content' }, textProps: { className: 'euiSelectableListItem__text' }, tooltipProps: hasToolTip ? _objectSpread({ content: toolTipContent, anchorClassName: 'eui-fullWidth', position: 'left' }, toolTipProps) : undefined }, rest); return ___EmotionJSX(EuiListItemLayout, listItemLayoutProps, children, screenReaderText); };