"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidatedNumberInput = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _eui = require("@elastic/eui"); var _i18n = require("@kbn/i18n"); var _lodash = _interopRequireDefault(require("lodash")); 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; you may not use this file except in compliance with the Elastic License * 2.0. */ function getErrorMessage(min, max) { return _i18n.i18n.translate('xpack.maps.validatedNumberInput.invalidClampErrorMessage', { defaultMessage: 'Must be between {min} and {max}', values: { min, max } }); } function isNumberValid(value, min, max) { const parsedValue = parseFloat(value.toString()); if (isNaN(parsedValue)) { return { isValid: false, errorMessage: getErrorMessage(min, max) }; } const isValid = parsedValue >= min && parsedValue <= max; return { parsedValue, isValid, errorMessage: isValid ? '' : getErrorMessage(min, max) }; } class ValidatedNumberInput extends _react.Component { constructor(props) { super(props); (0, _defineProperty2.default)(this, "_submit", _lodash.default.debounce(value => { this.props.onChange(value); }, 250)); (0, _defineProperty2.default)(this, "_onChange", e => { const value = e.target.value; const { isValid, errorMessage, parsedValue } = isNumberValid(value, this.props.min, this.props.max); this.setState({ value, errorMessage, isValid }); if (isValid) { this._submit(parsedValue); } }); const { isValid: _isValid, errorMessage: _errorMessage } = isNumberValid(props.initialValue, this.props.min, this.props.max); this.state = { value: props.initialValue, errorMessage: _errorMessage, isValid: _isValid }; } render() { return /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, { label: this.props.label, isInvalid: !this.state.isValid, error: this.state.errorMessage ? [this.state.errorMessage] : [], display: this.props.display, helpText: this.props.helpText }, /*#__PURE__*/_react.default.createElement(_eui.EuiFieldNumber, { isInvalid: !this.state.isValid, min: this.props.min, max: this.props.max, value: this.state.value, onChange: this._onChange, "aria-label": `${this.props.label} number input` })); } } exports.ValidatedNumberInput = ValidatedNumberInput;