@elastic/eui
Version:
Elastic UI Component Library
98 lines (96 loc) • 4.97 kB
JavaScript
var _excluded = ["className", "options", "id", "name", "fullWidth", "isLoading", "isInvalid", "readOnly", "defaultValue", "compressed", "value", "placeholder", "prepend", "append", "disabled"];
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; }
/*
* 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, { Fragment } from 'react';
import classNames from 'classnames';
import { EuiFormControlLayout } from '../form_control_layout';
import { getFormControlClassNameForIconCount } from '../form_control_layout/_num_icons';
import { useFormContext } from '../eui_form_context';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiSuperSelectControl = function EuiSuperSelectControl(props) {
var _useFormContext = useFormContext(),
defaultFullWidth = _useFormContext.defaultFullWidth;
var className = props.className,
_props$options = props.options,
options = _props$options === void 0 ? [] : _props$options,
id = props.id,
name = props.name,
_props$fullWidth = props.fullWidth,
fullWidth = _props$fullWidth === void 0 ? defaultFullWidth : _props$fullWidth,
_props$isLoading = props.isLoading,
isLoading = _props$isLoading === void 0 ? false : _props$isLoading,
_props$isInvalid = props.isInvalid,
isInvalid = _props$isInvalid === void 0 ? false : _props$isInvalid,
readOnly = props.readOnly,
defaultValue = props.defaultValue,
_props$compressed = props.compressed,
compressed = _props$compressed === void 0 ? false : _props$compressed,
value = props.value,
placeholder = props.placeholder,
prepend = props.prepend,
append = props.append,
disabled = props.disabled,
rest = _objectWithoutProperties(props, _excluded);
var numIconsClass = getFormControlClassNameForIconCount({
isInvalid: isInvalid,
isLoading: isLoading,
isDropdown: true
});
var classes = classNames('euiSuperSelectControl', numIconsClass, {
'euiSuperSelectControl--fullWidth': fullWidth,
'euiSuperSelectControl--compressed': compressed,
'euiSuperSelectControl--inGroup': prepend || append,
'euiSuperSelectControl-isLoading': isLoading,
'euiSuperSelectControl-isInvalid': isInvalid
}, className);
// React HTML input can not have both value and defaultValue properties.
// https://reactjs.org/docs/uncontrolled-components.html#default-values
var selectDefaultValue;
if (value == null) {
selectDefaultValue = defaultValue || '';
}
var selectedValue;
if (value) {
var selectedOption = options.find(function (option) {
return option.value === value;
});
selectedValue = selectedOption ? selectedOption.inputDisplay : selectedValue;
}
var showPlaceholder = !!placeholder && !selectedValue;
return ___EmotionJSX(Fragment, null, ___EmotionJSX("input", {
type: "hidden",
id: id,
name: name,
defaultValue: selectDefaultValue,
value: value,
readOnly: readOnly
}), ___EmotionJSX(EuiFormControlLayout, {
isDropdown: true,
fullWidth: fullWidth,
isLoading: isLoading,
isInvalid: isInvalid,
isDisabled: disabled,
readOnly: readOnly,
compressed: compressed,
prepend: prepend,
append: append
}, ___EmotionJSX("button", _extends({
type: "button",
className: classes,
"aria-haspopup": "listbox",
disabled: disabled || readOnly
// @ts-ignore Using as a selector only for mixin use
,
readOnly: readOnly
}, rest), showPlaceholder ? ___EmotionJSX("span", {
className: "euiSuperSelectControl__placeholder"
}, placeholder) : selectedValue)));
};