@elastic/eui
Version:
Elastic UI Component Library
177 lines (172 loc) • 10.2 kB
JavaScript
var _excluded = ["className", "id", "name", "placeholder", "value", "isInvalid", "fullWidth", "isLoading", "compressed", "inputRef", "prepend", "append", "type", "dualToggleProps"];
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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, { useState } from 'react';
import PropTypes from "prop-types";
import classNames from 'classnames';
import { EuiFormControlLayout } from '../form_control_layout';
import { EuiValidatableControl } from '../validatable_control';
import { EuiButtonIcon } from '../../button';
import { useEuiI18n } from '../../i18n';
import { useCombinedRefs } from '../../../services';
import { getFormControlClassNameForIconCount } from '../form_control_layout/_num_icons';
import { useFormContext } from '../eui_form_context';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiFieldPassword = function EuiFieldPassword(props) {
var _useFormContext = useFormContext(),
defaultFullWidth = _useFormContext.defaultFullWidth;
var className = props.className,
id = props.id,
name = props.name,
placeholder = props.placeholder,
value = props.value,
isInvalid = props.isInvalid,
_props$fullWidth = props.fullWidth,
fullWidth = _props$fullWidth === void 0 ? defaultFullWidth : _props$fullWidth,
isLoading = props.isLoading,
compressed = props.compressed,
_inputRef = props.inputRef,
prepend = props.prepend,
append = props.append,
_props$type = props.type,
type = _props$type === void 0 ? 'password' : _props$type,
dualToggleProps = props.dualToggleProps,
rest = _objectWithoutProperties(props, _excluded);
// Set the initial input type to `password` if they want dual
var _useState = useState(type === 'dual' ? 'password' : type),
_useState2 = _slicedToArray(_useState, 2),
inputType = _useState2[0],
setInputType = _useState2[1];
// Setup toggle aria-label
var _useEuiI18n = useEuiI18n(['euiFieldPassword.showPassword', 'euiFieldPassword.maskPassword'], ['Show password as plain text. Note: this will visually expose your password on the screen.', 'Mask password']),
_useEuiI18n2 = _slicedToArray(_useEuiI18n, 2),
showPasswordLabel = _useEuiI18n2[0],
maskPasswordLabel = _useEuiI18n2[1];
// Setup the inputRef to auto-focus when toggling visibility
var _useState3 = useState(null),
_useState4 = _slicedToArray(_useState3, 2),
inputRef = _useState4[0],
_setInputRef = _useState4[1];
var setInputRef = useCombinedRefs([_setInputRef, _inputRef]);
var handleToggle = function handleToggle(event, isVisible) {
setInputType(isVisible ? 'password' : 'text');
if (inputRef) {
inputRef.focus();
}
if (dualToggleProps && dualToggleProps.onClick) {
dualToggleProps.onClick(event);
}
};
// Convert any `append` elements to an array so the visibility
// toggle can be added to it
var appends = Array.isArray(append) ? append : [];
if (append && !Array.isArray(append)) appends.push(append);
// Add a toggling button to switch between `password` and `input` if consumer wants `dual`
// https://www.w3schools.com/howto/howto_js_toggle_password.asp
if (type === 'dual') {
var isVisible = inputType === 'text';
var visibilityToggle = ___EmotionJSX(EuiButtonIcon, _extends({
iconType: isVisible ? 'eyeClosed' : 'eye',
"aria-label": isVisible ? maskPasswordLabel : showPasswordLabel,
title: isVisible ? maskPasswordLabel : showPasswordLabel,
disabled: rest.disabled
}, dualToggleProps, {
onClick: function onClick(e) {
return handleToggle(e, isVisible);
}
}));
appends = [].concat(_toConsumableArray(appends), [visibilityToggle]);
}
var finalAppend = appends.length ? appends : undefined;
var numIconsClass = getFormControlClassNameForIconCount({
isInvalid: isInvalid,
isLoading: isLoading
});
var classes = classNames('euiFieldPassword', numIconsClass, {
'euiFieldPassword--fullWidth': fullWidth,
'euiFieldPassword--compressed': compressed,
'euiFieldPassword--inGroup': prepend || finalAppend,
'euiFieldPassword--withToggle': type === 'dual',
'euiFieldPassword-isLoading': isLoading
}, className);
return ___EmotionJSX(EuiFormControlLayout, {
icon: "lock",
fullWidth: fullWidth,
isLoading: isLoading,
isInvalid: isInvalid,
compressed: compressed,
prepend: prepend,
append: finalAppend
}, ___EmotionJSX(EuiValidatableControl, {
isInvalid: isInvalid
}, ___EmotionJSX("input", _extends({
type: inputType,
id: id,
name: name,
placeholder: placeholder,
className: classes,
value: value,
ref: setInputRef
}, rest))));
};
EuiFieldPassword.propTypes = {
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
isInvalid: PropTypes.bool,
/**
* Expand to fill 100% of the parent.
* Defaults to `fullWidth` prop of `<EuiForm>`.
* @default false
*/
fullWidth: PropTypes.bool,
isLoading: PropTypes.bool,
compressed: PropTypes.bool,
inputRef: PropTypes.any,
/**
* Creates an input group with element(s) coming before input.
* `string` | `ReactElement` or an array of these
*/
prepend: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired).isRequired]),
/**
* Creates an input group with element(s) coming after input.
* `string` | `ReactElement` or an array of these
*/
append: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.element.isRequired]).isRequired).isRequired]),
value: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]),
/**
* Change the `type` of input for manually handling obfuscation.
* The `dual` option adds the ability to toggle the obfuscation of the input by
* adding an icon button as the first `append` element
* @default password
*/
type: PropTypes.oneOf(["password", "text", "dual"]),
/**
* Additional props to apply to the dual toggle. Extends EuiButtonIcon
*/
dualToggleProps: PropTypes.any
};
EuiFieldPassword.defaultProps = {
value: undefined,
isLoading: false,
compressed: false
};