UNPKG

@carbon/react

Version:

React components for the Carbon Design System

320 lines (312 loc) 12.4 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var cx = require('classnames'); var PropTypes = require('prop-types'); var iconsReact = require('@carbon/icons-react'); var useNormalizedInputProps = require('../../internal/useNormalizedInputProps.js'); var util = require('./util.js'); require('../FluidForm/FluidForm.js'); var FormContext = require('../FluidForm/FormContext.js'); require('../Tooltip/DefinitionTooltip.js'); var Tooltip = require('../Tooltip/Tooltip.js'); var deprecate = require('../../prop-types/deprecate.js'); var usePrefix = require('../../internal/usePrefix.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); const PasswordInput = /*#__PURE__*/React__default["default"].forwardRef(function PasswordInput({ className, disabled = false, helperText, hideLabel, hidePasswordLabel = 'Hide password', id, inline, invalid = false, invalidText, labelText, light, onChange = () => {}, onClick = () => {}, onTogglePasswordVisibility, placeholder, readOnly, size = 'md', showPasswordLabel = 'Show password', tooltipPosition = 'bottom', tooltipAlignment = 'end', type = 'password', warn = false, warnText, ...rest }, ref) { const [inputType, setInputType] = React.useState(type); const prefix = usePrefix.usePrefix(); const normalizedProps = useNormalizedInputProps.useNormalizedInputProps({ id, invalid, invalidText, warn, warnText, readOnly, disabled }); const { isFluid } = React.useContext(FormContext.FormContext); const handleTogglePasswordVisibility = event => { setInputType(inputType === 'password' ? 'text' : 'password'); onTogglePasswordVisibility && onTogglePasswordVisibility(event); }; const textInputClasses = cx__default["default"](`${prefix}--text-input`, `${prefix}--password-input`, className, { [`${prefix}--text-input--light`]: light, [`${prefix}--text-input--invalid`]: normalizedProps.invalid, [`${prefix}--text-input--warning`]: normalizedProps.warn, [`${prefix}--text-input--${size}`]: size, // TODO: V12 - Remove this class [`${prefix}--layout--size-${size}`]: size }); const sharedTextInputProps = { id, onChange: evt => { if (!disabled) { onChange(evt); } }, onClick: evt => { if (!disabled) { onClick(evt); } }, placeholder, type: inputType, className: textInputClasses, readOnly, ref, ...rest }; const inputWrapperClasses = cx__default["default"](`${prefix}--form-item`, `${prefix}--text-input-wrapper`, `${prefix}--password-input-wrapper`, { [`${prefix}--text-input-wrapper--readonly`]: readOnly, [`${prefix}--text-input-wrapper--light`]: light, [`${prefix}--text-input-wrapper--inline`]: inline, [`${prefix}--text-input--fluid`]: isFluid }); const labelClasses = cx__default["default"](`${prefix}--label`, { [`${prefix}--visually-hidden`]: hideLabel, [`${prefix}--label--disabled`]: disabled, [`${prefix}--label--inline`]: inline, [`${prefix}--label--inline--${size}`]: inline && !!size }); const helperTextClasses = cx__default["default"](`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: disabled, [`${prefix}--form__helper-text--inline`]: inline }); const fieldOuterWrapperClasses = cx__default["default"](`${prefix}--text-input__field-outer-wrapper`, { [`${prefix}--text-input__field-outer-wrapper--inline`]: inline }); const fieldWrapperClasses = cx__default["default"](`${prefix}--text-input__field-wrapper`, { [`${prefix}--text-input__field-wrapper--warning`]: normalizedProps.warn }); const iconClasses = cx__default["default"]({ [`${prefix}--text-input__invalid-icon`]: normalizedProps.invalid || normalizedProps.warn, [`${prefix}--text-input__invalid-icon--warning`]: normalizedProps.warn }); const label = labelText ? /*#__PURE__*/React__default["default"].createElement("label", { htmlFor: id, className: labelClasses }, labelText) : null; const helper = helperText ? /*#__PURE__*/React__default["default"].createElement("div", { id: normalizedProps.helperId, className: helperTextClasses }, helperText) : null; const passwordIsVisible = inputType === 'text'; const passwordVisibilityIcon = passwordIsVisible ? /*#__PURE__*/React__default["default"].createElement(iconsReact.ViewOff, { className: `${prefix}--icon-visibility-off` }) : /*#__PURE__*/React__default["default"].createElement(iconsReact.View, { className: `${prefix}--icon-visibility-on` }); const passwordVisibilityToggleClasses = cx__default["default"](`${prefix}--text-input--password__visibility__toggle`, `${prefix}--btn`, `${prefix}--tooltip__trigger`, `${prefix}--tooltip--a11y`, { [`${prefix}--tooltip--${tooltipPosition}`]: tooltipPosition, [`${prefix}--tooltip--align-${tooltipAlignment}`]: tooltipAlignment }); let align = undefined; if (tooltipPosition === 'top' || tooltipPosition === 'bottom') { if (tooltipAlignment === 'center') { align = tooltipPosition; } if (tooltipAlignment === 'end') { align = `${tooltipPosition}-end`; } if (tooltipAlignment === 'start') { align = `${tooltipPosition}-start`; } } if (tooltipPosition === 'right' || tooltipPosition === 'left') { align = tooltipPosition; } if (!hidePasswordLabel || hidePasswordLabel.trim() === '') { console.warn('Warning: The "hidePasswordLabel" should not be blank.'); } else if (!showPasswordLabel || showPasswordLabel.trim() === '') { console.warn('Warning: The "showPasswordLabel" should not be blank.'); } const input = /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("input", _rollupPluginBabelHelpers["extends"]({}, util.textInputProps({ sharedTextInputProps, invalid: normalizedProps.invalid, invalidId: normalizedProps.invalidId, warn: normalizedProps.warn, warnId: normalizedProps.warnId, hasHelper: Boolean(helperText && !isFluid && (inline || !inline && !normalizedProps.validation)), helperId: normalizedProps.helperId }), { disabled: disabled, "data-toggle-password-visibility": inputType === 'password' })), isFluid && /*#__PURE__*/React__default["default"].createElement("hr", { className: `${prefix}--text-input__divider` }), /*#__PURE__*/React__default["default"].createElement(Tooltip.Tooltip, { align: align, className: `${prefix}--toggle-password-tooltip`, label: passwordIsVisible ? hidePasswordLabel : showPasswordLabel }, /*#__PURE__*/React__default["default"].createElement("button", { type: "button", className: passwordVisibilityToggleClasses, disabled: disabled || readOnly, onClick: handleTogglePasswordVisibility }, passwordVisibilityIcon))); React.useEffect(() => { setInputType(type); }, [type]); const Icon = normalizedProps.icon; return /*#__PURE__*/React__default["default"].createElement("div", { className: inputWrapperClasses }, !inline ? label : /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--text-input__label-helper-wrapper` }, label, !isFluid && helper), /*#__PURE__*/React__default["default"].createElement("div", { className: fieldOuterWrapperClasses }, /*#__PURE__*/React__default["default"].createElement("div", { className: fieldWrapperClasses, "data-invalid": normalizedProps.invalid || null }, Icon && /*#__PURE__*/React__default["default"].createElement(Icon, { className: iconClasses }), input, isFluid && !inline && normalizedProps.validation), !isFluid && !inline && (normalizedProps.validation || helper))); }); PasswordInput.displayName = 'PasswordInput'; PasswordInput.propTypes = { /** * Provide a custom className that is applied directly to the underlying * `<input>` node */ className: PropTypes__default["default"].string, /** * Optionally provide the default value of the `<input>` */ defaultValue: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].number]), /** * Specify whether the control is disabled */ disabled: PropTypes__default["default"].bool, /** * Provide text that is used alongside the control label for additional help */ helperText: PropTypes__default["default"].node, /** * Specify whether or not the underlying label is visually hidden */ hideLabel: PropTypes__default["default"].bool, /** * "Hide password" tooltip text on password visibility toggle */ hidePasswordLabel: PropTypes__default["default"].string, /** * Provide a unique identifier for the input field */ id: PropTypes__default["default"].string.isRequired, /** * `true` to use the inline version. */ inline: PropTypes__default["default"].bool, /** * Specify whether the control is currently invalid */ invalid: PropTypes__default["default"].bool, /** * Whether the PasswordInput should be read-only */ readOnly: PropTypes__default["default"].bool, /** * Provide the text that is displayed when the control is in an invalid state */ invalidText: PropTypes__default["default"].node, /** * Provide the text that will be read by a screen reader when visiting this * control */ labelText: PropTypes__default["default"].node.isRequired, /** * `true` to use the light version. For use on $ui-01 backgrounds only. * Don't use this to make tile background color same as container background color. */ light: deprecate["default"](PropTypes__default["default"].bool, 'The `light` prop for `PasswordInput` has ' + 'been deprecated in favor of the new `Layer` component. It will be removed in the next major release.'), /** * Optionally provide an `onChange` handler that is called whenever `<input>` * is updated */ onChange: PropTypes__default["default"].func, /** * Optionally provide an `onClick` handler that is called whenever the * `<input>` is clicked */ onClick: PropTypes__default["default"].func, /** * Callback function that is called whenever the toggle password visibility * button is clicked */ onTogglePasswordVisibility: PropTypes__default["default"].func, /** * Specify the placeholder attribute for the `<input>` */ placeholder: PropTypes__default["default"].string, /** * "Show password" tooltip text on password visibility toggle */ showPasswordLabel: PropTypes__default["default"].string, /** * Specify the size of the Text Input. Supports `sm`, `md`, or `lg`. */ size: PropTypes__default["default"].oneOf(['sm', 'md', 'lg']), /** * Specify the alignment of the tooltip to the icon-only button. * Can be one of: start, center, or end. */ tooltipAlignment: PropTypes__default["default"].oneOf(['start', 'center', 'end']), /** * Specify the direction of the tooltip for icon-only buttons. * Can be either top, right, bottom, or left. */ tooltipPosition: PropTypes__default["default"].oneOf(['top', 'right', 'bottom', 'left']), /** * The input type, either password or text */ type: PropTypes__default["default"].oneOf(['password', 'text']), /** * Provide the current value of the `<input>` */ value: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].number]), /** * Specify whether the control is currently in warning state */ warn: PropTypes__default["default"].bool, /** * Provide the text that is displayed when the control is in warning state */ warnText: PropTypes__default["default"].node }; exports["default"] = PasswordInput;