"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ListControl = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _lodash = _interopRequireDefault(require("lodash")); var _i18nReact = require("@kbn/i18n-react"); var _eui = require("@elastic/eui"); var _i18n = require("@kbn/i18n"); var _form_row = require("./form_row"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; } /* * 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. */ class ListControlUi extends _react.PureComponent { constructor(props) { super(props); (0, _defineProperty2.default)(this, "isMounted", false); (0, _defineProperty2.default)(this, "state", { isLoading: false }); (0, _defineProperty2.default)(this, "textInput", void 0); (0, _defineProperty2.default)(this, "componentDidMount", () => { if (this.textInput) { this.textInput.setAttribute('focusable', 'false'); // remove when #59039 is fixed } this.isMounted = true; }); (0, _defineProperty2.default)(this, "componentWillUnmount", () => { this.isMounted = false; }); (0, _defineProperty2.default)(this, "setTextInputRef", ref => { this.textInput = ref; }); (0, _defineProperty2.default)(this, "handleOnChange", selectedOptions => { const selectedValues = selectedOptions.map(({ value }) => { return value; }); this.props.stageFilter(this.props.controlIndex, selectedValues); }); (0, _defineProperty2.default)(this, "debouncedFetch", _lodash.default.debounce(async searchValue => { if (this.props.fetchOptions) { await this.props.fetchOptions(searchValue); } if (this.isMounted) { this.setState({ isLoading: false }); } }, 300)); (0, _defineProperty2.default)(this, "onSearchChange", searchValue => { this.setState({ isLoading: true }, this.debouncedFetch.bind(null, searchValue)); }); this.textInput = null; } renderControl() { var _this$props$options; const { intl } = this.props; if (this.props.disableMsg) { return /*#__PURE__*/_react.default.createElement(_eui.EuiFieldText, { "aria-label": intl.formatMessage({ id: 'inputControl.vis.listControl.selectTextPlaceholder', defaultMessage: 'Select...' }), placeholder: intl.formatMessage({ id: 'inputControl.vis.listControl.selectTextPlaceholder', defaultMessage: 'Select...' }), disabled: true }); } const options = (_this$props$options = this.props.options) === null || _this$props$options === void 0 ? void 0 : _this$props$options.map(option => { return { label: this.props.formatOptionLabel(option).toString(), value: option, ['data-test-subj']: `option_${option.toString().replace(' ', '_')}` }; }).sort((a, b) => { return a.label.toLowerCase().localeCompare(b.label.toLowerCase()); }); const selectedOptions = this.props.selectedOptions.map(selectedOption => { return { label: this.props.formatOptionLabel(selectedOption).toString(), value: selectedOption }; }); return /*#__PURE__*/_react.default.createElement(_eui.EuiComboBox, { placeholder: intl.formatMessage({ id: 'inputControl.vis.listControl.selectPlaceholder', defaultMessage: 'Select...' }), options: options, isLoading: this.state.isLoading, async: this.props.dynamicOptions, onSearchChange: this.props.dynamicOptions ? this.onSearchChange : undefined, selectedOptions: selectedOptions, onChange: this.handleOnChange, singleSelection: !this.props.multiselect, "data-test-subj": `listControlSelect${this.props.controlIndex}`, inputRef: this.setTextInputRef }); } render() { const partialResultsWarningMessage = _i18n.i18n.translate('inputControl.vis.listControl.partialResultsWarningMessage', { defaultMessage: 'Terms list might be incomplete because the request is taking too long. ' + 'Adjust the autocomplete settings in kibana.yml for complete results.' }); return /*#__PURE__*/_react.default.createElement(_eui.EuiThemeProvider, { colorMode: this.props.isDarkMode ? 'dark' : 'light' }, /*#__PURE__*/_react.default.createElement(_form_row.FormRow, { id: this.props.id, label: this.props.label, warningMsg: this.props.partialResults ? partialResultsWarningMessage : undefined, controlIndex: this.props.controlIndex, disableMsg: this.props.disableMsg }, this.renderControl())); } } (0, _defineProperty2.default)(ListControlUi, "defaultProps", { dynamicOptions: false, multiselect: true, selectedOptions: [], options: [] }); const ListControl = (0, _i18nReact.injectI18n)(ListControlUi); exports.ListControl = ListControl;