UNPKG

@elastic/eui

Version:

Elastic UI Component Library

324 lines (320 loc) 18.1 kB
var _excluded = ["key", "label", "color", "onClick"]; function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /* * 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 React, { Component } from 'react'; import PropTypes from "prop-types"; import classNames from 'classnames'; import AutosizeInput from 'react-input-autosize'; import { keys } from '../../../services'; import { EuiScreenReaderOnly } from '../../accessibility'; import { EuiFormControlLayout } from '../../form/form_control_layout'; import { getFormControlClassNameForIconCount } from '../../form/form_control_layout/_num_icons'; import { EuiComboBoxPill } from './combo_box_pill'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiComboBoxInput = /*#__PURE__*/function (_Component) { _inherits(EuiComboBoxInput, _Component); var _super = _createSuper(EuiComboBoxInput); function EuiComboBoxInput() { var _this; _classCallCheck(this, EuiComboBoxInput); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { hasFocus: false }); _defineProperty(_assertThisInitialized(_this), "updatePosition", function () { // Wait a beat for the DOM to update, since we depend on DOM elements' bounds. requestAnimationFrame(function () { _this.props.updatePosition(); }); }); _defineProperty(_assertThisInitialized(_this), "onFocus", function (event) { _this.props.onFocus(event); _this.setState({ hasFocus: true }); }); _defineProperty(_assertThisInitialized(_this), "onBlur", function (event) { if (_this.props.onBlur) { _this.props.onBlur(event); } _this.setState({ hasFocus: false }); }); _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) { var _this$props = _this.props, searchValue = _this$props.searchValue, selectedOptions = _this$props.selectedOptions, onRemoveOption = _this$props.onRemoveOption, singleSelection = _this$props.singleSelection, isListOpen = _this$props.isListOpen, onOpenListClick = _this$props.onOpenListClick; // When backspacing from an empty input, delete the last pill option in the list var searchIsEmpty = !searchValue.length; var hasPills = selectedOptions.length; if (event.key === keys.BACKSPACE && searchIsEmpty && hasPills) { onRemoveOption(selectedOptions[selectedOptions.length - 1]); if (!!singleSelection && !isListOpen) { onOpenListClick(); } } }); _defineProperty(_assertThisInitialized(_this), "inputOnChange", function (event) { var _this$props2 = _this.props, onChange = _this$props2.onChange, searchValue = _this$props2.searchValue; if (onChange) { onChange(event.target.value); } }); _defineProperty(_assertThisInitialized(_this), "inputRefCallback", function (ref) { var autoSizeInputRef = _this.props.autoSizeInputRef; if (autoSizeInputRef) { autoSizeInputRef(ref); } }); return _this; } _createClass(EuiComboBoxInput, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var searchValue = prevProps.searchValue; // We need to update the position of everything if the user enters enough input to change // the size of the input. if (searchValue !== this.props.searchValue) { this.updatePosition(); } } }, { key: "render", value: function render() { var _this$props3 = this.props, compressed = _this$props3.compressed, focusedOptionId = _this$props3.focusedOptionId, fullWidth = _this$props3.fullWidth, hasSelectedOptions = _this$props3.hasSelectedOptions, id = _this$props3.id, inputRef = _this$props3.inputRef, isDisabled = _this$props3.isDisabled, isListOpen = _this$props3.isListOpen, noIcon = _this$props3.noIcon, onClear = _this$props3.onClear, onClick = _this$props3.onClick, onCloseListClick = _this$props3.onCloseListClick, onOpenListClick = _this$props3.onOpenListClick, onRemoveOption = _this$props3.onRemoveOption, placeholder = _this$props3.placeholder, rootId = _this$props3.rootId, searchValue = _this$props3.searchValue, selectedOptions = _this$props3.selectedOptions, singleSelectionProp = _this$props3.singleSelection, toggleButtonRef = _this$props3.toggleButtonRef, value = _this$props3.value, prepend = _this$props3.prepend, append = _this$props3.append, isLoading = _this$props3.isLoading, isInvalid = _this$props3.isInvalid, autoFocus = _this$props3.autoFocus, ariaLabel = _this$props3['aria-label'], ariaLabelledby = _this$props3['aria-labelledby']; var singleSelection = Boolean(singleSelectionProp); var asPlainText = singleSelectionProp && _typeof(singleSelectionProp) === 'object' && singleSelectionProp.asPlainText || false; var pills = selectedOptions ? selectedOptions.map(function (option) { var key = option.key, label = option.label, color = option.color, onClick = option.onClick, rest = _objectWithoutProperties(option, _excluded); var pillOnClose = isDisabled || singleSelection || onClick ? undefined : onRemoveOption; return ___EmotionJSX(EuiComboBoxPill, _extends({ option: option, onClose: pillOnClose, key: key !== null && key !== void 0 ? key : label.toLowerCase(), color: color, onClick: onClick, onClickAriaLabel: onClick ? 'Change' : undefined, asPlainText: asPlainText }, rest), label); }) : null; var removeOptionMessage; var removeOptionMessageId; if (this.state.hasFocus) { var readPlaceholder = placeholder ? "".concat(placeholder, ".") : ''; var removeOptionMessageContent = "Combo box. Selected. ".concat(searchValue ? "".concat(searchValue, ". Selected. ") : '').concat(selectedOptions && selectedOptions.length > 0 ? "".concat(value, ". Press Backspace to delete ").concat(selectedOptions[selectedOptions.length - 1].label, ". ") : '', "Combo box input. ").concat(readPlaceholder, " Type some text or, to display a list of choices, press Down Arrow. ") + 'To exit the list of choices, press Escape.'; removeOptionMessageId = rootId('removeOptionMessage'); // aria-live="assertive" will read this message aloud immediately once it enters the DOM. // We'll render to the DOM when the input gains focus and remove it when the input loses focus. // We'll use aria-hidden to prevent default aria information from being read by the screen // reader. removeOptionMessage = ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("span", { "aria-live": "polite", id: removeOptionMessageId }, removeOptionMessageContent)); } var placeholderMessage; if (placeholder && selectedOptions && !selectedOptions.length && !searchValue) { placeholderMessage = ___EmotionJSX("p", { className: "euiComboBoxPlaceholder" }, placeholder); } var clickProps = {}; if (!isDisabled && onClear && hasSelectedOptions) { clickProps.clear = { 'data-test-subj': 'comboBoxClearButton', onClick: onClear }; } var icon; if (!noIcon && !isDisabled) { icon = { 'aria-label': isListOpen ? 'Close list of options' : 'Open list of options', 'data-test-subj': 'comboBoxToggleListButton', disabled: isDisabled, onClick: isListOpen && !isDisabled ? onCloseListClick : onOpenListClick, ref: toggleButtonRef, side: 'right', tabIndex: -1, type: 'arrowDown' }; } var numIconsClass = getFormControlClassNameForIconCount({ isDropdown: !noIcon, clear: !!clickProps.clear, isInvalid: isInvalid, isLoading: isLoading }); var wrapClasses = classNames('euiComboBox__inputWrap', numIconsClass, { 'euiComboBox__inputWrap--compressed': compressed, 'euiComboBox__inputWrap--fullWidth': fullWidth, 'euiComboBox__inputWrap--noWrap': singleSelection, 'euiComboBox__inputWrap--inGroup': prepend || append }); return ___EmotionJSX(EuiFormControlLayout, _extends({ icon: icon }, clickProps, { inputId: id, isLoading: isLoading, isInvalid: isInvalid, compressed: compressed, fullWidth: fullWidth, prepend: prepend, append: append }), ___EmotionJSX("div", { className: wrapClasses, "data-test-subj": "comboBoxInput", onClick: onClick, tabIndex: -1 // becomes onBlur event's relatedTarget, otherwise relatedTarget is null when clicking on this div }, !singleSelection || !searchValue ? pills : null, placeholderMessage, ___EmotionJSX(AutosizeInput, { "aria-activedescendant": focusedOptionId, "aria-autocomplete": "list", "aria-controls": isListOpen ? rootId('listbox') : '', "aria-expanded": isListOpen, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, "aria-invalid": isInvalid, className: "euiComboBox__input", "data-test-subj": "comboBoxSearchInput", disabled: isDisabled, id: id, inputRef: inputRef, onBlur: this.onBlur, onChange: this.inputOnChange, onFocus: this.onFocus, onKeyDown: this.onKeyDown, ref: this.inputRefCallback, role: "combobox", style: { fontSize: 14 }, value: searchValue, autoFocus: autoFocus }), removeOptionMessage)); } }]); return EuiComboBoxInput; }(Component); EuiComboBoxInput.propTypes = { autoSizeInputRef: PropTypes.any, compressed: PropTypes.bool.isRequired, focusedOptionId: PropTypes.string, fullWidth: PropTypes.bool, hasSelectedOptions: PropTypes.bool.isRequired, id: PropTypes.string, inputRef: PropTypes.any, isDisabled: PropTypes.bool, isListOpen: PropTypes.bool.isRequired, noIcon: PropTypes.bool.isRequired, onBlur: PropTypes.any, onChange: PropTypes.func, onClear: PropTypes.func, onClick: PropTypes.func, onCloseListClick: PropTypes.func.isRequired, onFocus: PropTypes.any.isRequired, onOpenListClick: PropTypes.func.isRequired, onRemoveOption: PropTypes.func.isRequired, placeholder: PropTypes.string, rootId: PropTypes.any.isRequired, searchValue: PropTypes.string.isRequired, selectedOptions: PropTypes.arrayOf(PropTypes.shape({ isGroupLabelOption: PropTypes.bool, label: PropTypes.string.isRequired, key: PropTypes.string, options: PropTypes.arrayOf(PropTypes.shape({ isGroupLabelOption: PropTypes.bool, label: PropTypes.string.isRequired, key: PropTypes.string, options: PropTypes.arrayOf(PropTypes.any.isRequired), value: PropTypes.any, className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any }).isRequired), value: PropTypes.any, className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any }).isRequired).isRequired, singleSelection: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.shape({ asPlainText: PropTypes.bool }).isRequired]), toggleButtonRef: PropTypes.any, updatePosition: PropTypes.func.isRequired, value: PropTypes.string, prepend: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired).isRequired]), append: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired).isRequired]), isLoading: PropTypes.bool, isInvalid: PropTypes.bool, autoFocus: PropTypes.bool, "aria-label": PropTypes.string, "aria-labelledby": PropTypes.string, className: PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any };