@elastic/eui
Version:
Elastic UI Component Library
114 lines (112 loc) • 4.79 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["className", "icon", "id", "placeholder", "name", "min", "max", "step", "value", "isInvalid", "fullWidth", "isLoading", "compressed", "prepend", "append", "inputRef", "readOnly", "controlOnly", "onKeyUp", "onBlur"];
/*
* 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, { useState, useCallback } from 'react';
import classNames from 'classnames';
import { EuiValidatableControl } from '../validatable_control';
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 EuiFieldNumber = function EuiFieldNumber(props) {
var _useFormContext = useFormContext(),
defaultFullWidth = _useFormContext.defaultFullWidth;
var className = props.className,
icon = props.icon,
id = props.id,
placeholder = props.placeholder,
name = props.name,
min = props.min,
max = props.max,
_props$step = props.step,
step = _props$step === void 0 ? 'any' : _props$step,
value = props.value,
isInvalid = props.isInvalid,
_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$compressed = props.compressed,
compressed = _props$compressed === void 0 ? false : _props$compressed,
prepend = props.prepend,
append = props.append,
inputRef = props.inputRef,
readOnly = props.readOnly,
controlOnly = props.controlOnly,
_onKeyUp = props.onKeyUp,
_onBlur = props.onBlur,
rest = _objectWithoutProperties(props, _excluded);
// Attempt to determine additional invalid state. The native number input
// will set :invalid state automatically, but we need to also set
// `aria-invalid` as well as display an icon. We also want to *not* set this on
// EuiValidatableControl, in order to not override custom validity messages
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
isNativelyInvalid = _useState2[0],
setIsNativelyInvalid = _useState2[1];
var checkNativeValidity = useCallback(function (inputEl) {
// Prefer `undefined` over `false` so that the `aria-invalid` prop unsets completely
var isInvalid = !inputEl.validity.valid || undefined;
setIsNativelyInvalid(isInvalid);
}, []);
var numIconsClass = controlOnly ? false : getFormControlClassNameForIconCount({
isInvalid: isInvalid || isNativelyInvalid,
isLoading: isLoading
});
var classes = classNames('euiFieldNumber', className, numIconsClass, {
'euiFieldNumber--withIcon': icon,
'euiFieldNumber--fullWidth': fullWidth,
'euiFieldNumber--compressed': compressed,
'euiFieldNumber--inGroup': prepend || append,
'euiFieldNumber-isLoading': isLoading
});
var control = ___EmotionJSX(EuiValidatableControl, {
isInvalid: isInvalid
}, ___EmotionJSX("input", _extends({
type: "number",
id: id,
name: name,
min: min,
max: max,
step: step,
value: value,
placeholder: placeholder,
readOnly: readOnly,
className: classes,
ref: inputRef,
"aria-invalid": isInvalid || isNativelyInvalid,
onKeyUp: function onKeyUp(e) {
// Note that we can't use `onChange` because browsers don't emit change events
// for invalid text - see https://github.com/facebook/react/issues/16554
_onKeyUp === null || _onKeyUp === void 0 ? void 0 : _onKeyUp(e);
checkNativeValidity(e.currentTarget);
},
onBlur: function onBlur(e) {
// Browsers can also set/determine validity (e.g. when `step` is undefined) on focus blur
_onBlur === null || _onBlur === void 0 ? void 0 : _onBlur(e);
checkNativeValidity(e.currentTarget);
}
}, rest)));
if (controlOnly) {
return control;
}
return ___EmotionJSX(EuiFormControlLayout, {
icon: icon,
fullWidth: fullWidth,
isLoading: isLoading,
isInvalid: isInvalid || isNativelyInvalid,
compressed: compressed,
readOnly: readOnly,
prepend: prepend,
append: append,
inputId: id
}, control);
};