@carbon/react
Version:
React components for the Carbon Design System
310 lines (306 loc) • 10.8 kB
JavaScript
/**
* 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React, { useState, useContext, useEffect } from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { ViewOff, View } from '@carbon/icons-react';
import { useNormalizedInputProps } from '../../internal/useNormalizedInputProps.js';
import { textInputProps } from './util.js';
import '../FluidForm/FluidForm.js';
import { FormContext } from '../FluidForm/FormContext.js';
import '../Tooltip/DefinitionTooltip.js';
import { Tooltip } from '../Tooltip/Tooltip.js';
import { deprecate } from '../../prop-types/deprecate.js';
import { usePrefix } from '../../internal/usePrefix.js';
const PasswordInput = /*#__PURE__*/React.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] = useState(type);
const prefix = usePrefix();
const normalizedProps = useNormalizedInputProps({
id,
invalid,
invalidText,
warn,
warnText,
readOnly,
disabled
});
const {
isFluid
} = useContext(FormContext);
const handleTogglePasswordVisibility = event => {
setInputType(inputType === 'password' ? 'text' : 'password');
onTogglePasswordVisibility && onTogglePasswordVisibility(event);
};
const textInputClasses = cx(`${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(`${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(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
[`${prefix}--label--disabled`]: disabled,
[`${prefix}--label--inline`]: inline,
[`${prefix}--label--inline--${size}`]: inline && !!size
});
const helperTextClasses = cx(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: disabled,
[`${prefix}--form__helper-text--inline`]: inline
});
const fieldOuterWrapperClasses = cx(`${prefix}--text-input__field-outer-wrapper`, {
[`${prefix}--text-input__field-outer-wrapper--inline`]: inline
});
const fieldWrapperClasses = cx(`${prefix}--text-input__field-wrapper`, {
[`${prefix}--text-input__field-wrapper--warning`]: normalizedProps.warn
});
const iconClasses = cx({
[`${prefix}--text-input__invalid-icon`]: normalizedProps.invalid || normalizedProps.warn,
[`${prefix}--text-input__invalid-icon--warning`]: normalizedProps.warn
});
const label = labelText ? /*#__PURE__*/React.createElement("label", {
htmlFor: id,
className: labelClasses
}, labelText) : null;
const helper = helperText ? /*#__PURE__*/React.createElement("div", {
id: normalizedProps.helperId,
className: helperTextClasses
}, helperText) : null;
const passwordIsVisible = inputType === 'text';
const passwordVisibilityIcon = passwordIsVisible ? /*#__PURE__*/React.createElement(ViewOff, {
className: `${prefix}--icon-visibility-off`
}) : /*#__PURE__*/React.createElement(View, {
className: `${prefix}--icon-visibility-on`
});
const passwordVisibilityToggleClasses = cx(`${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.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("input", _extends({}, 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.createElement("hr", {
className: `${prefix}--text-input__divider`
}), /*#__PURE__*/React.createElement(Tooltip, {
align: align,
className: `${prefix}--toggle-password-tooltip`,
label: passwordIsVisible ? hidePasswordLabel : showPasswordLabel
}, /*#__PURE__*/React.createElement("button", {
type: "button",
className: passwordVisibilityToggleClasses,
disabled: disabled || readOnly,
onClick: handleTogglePasswordVisibility
}, passwordVisibilityIcon)));
useEffect(() => {
setInputType(type);
}, [type]);
const Icon = normalizedProps.icon;
return /*#__PURE__*/React.createElement("div", {
className: inputWrapperClasses
}, !inline ? label : /*#__PURE__*/React.createElement("div", {
className: `${prefix}--text-input__label-helper-wrapper`
}, label, !isFluid && helper), /*#__PURE__*/React.createElement("div", {
className: fieldOuterWrapperClasses
}, /*#__PURE__*/React.createElement("div", {
className: fieldWrapperClasses,
"data-invalid": normalizedProps.invalid || null
}, Icon && /*#__PURE__*/React.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.string,
/**
* Optionally provide the default value of the `<input>`
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the control is disabled
*/
disabled: PropTypes.bool,
/**
* Provide text that is used alongside the control label for additional help
*/
helperText: PropTypes.node,
/**
* Specify whether or not the underlying label is visually hidden
*/
hideLabel: PropTypes.bool,
/**
* "Hide password" tooltip text on password visibility toggle
*/
hidePasswordLabel: PropTypes.string,
/**
* Provide a unique identifier for the input field
*/
id: PropTypes.string.isRequired,
/**
* `true` to use the inline version.
*/
inline: PropTypes.bool,
/**
* Specify whether the control is currently invalid
*/
invalid: PropTypes.bool,
/**
* Whether the PasswordInput should be read-only
*/
readOnly: PropTypes.bool,
/**
* Provide the text that is displayed when the control is in an invalid state
*/
invalidText: PropTypes.node,
/**
* Provide the text that will be read by a screen reader when visiting this
* control
*/
labelText: PropTypes.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(PropTypes.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.func,
/**
* Optionally provide an `onClick` handler that is called whenever the
* `<input>` is clicked
*/
onClick: PropTypes.func,
/**
* Callback function that is called whenever the toggle password visibility
* button is clicked
*/
onTogglePasswordVisibility: PropTypes.func,
/**
* Specify the placeholder attribute for the `<input>`
*/
placeholder: PropTypes.string,
/**
* "Show password" tooltip text on password visibility toggle
*/
showPasswordLabel: PropTypes.string,
/**
* Specify the size of the Text Input. Supports `sm`, `md`, or `lg`.
*/
size: PropTypes.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.oneOf(['start', 'center', 'end']),
/**
* Specify the direction of the tooltip for icon-only buttons.
* Can be either top, right, bottom, or left.
*/
tooltipPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
/**
* The input type, either password or text
*/
type: PropTypes.oneOf(['password', 'text']),
/**
* Provide the current value of the `<input>`
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the control is currently in warning state
*/
warn: PropTypes.bool,
/**
* Provide the text that is displayed when the control is in warning state
*/
warnText: PropTypes.node
};
export { PasswordInput as default };