@carbon/react
Version:
React components for the Carbon Design System
211 lines (207 loc) • 7.35 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 from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { WarningFilled, ViewOff, View } from '@carbon/icons-react';
import { textInputProps } from './util.js';
import { deprecate } from '../../prop-types/deprecate.js';
import { usePrefix } from '../../internal/usePrefix.js';
import { useId } from '../../internal/useId.js';
import { noopFn } from '../../internal/noopFn.js';
const ControlledPasswordInput = /*#__PURE__*/React.forwardRef(function ControlledPasswordInput({
labelText,
className,
id,
placeholder,
onChange = noopFn,
onClick = noopFn,
disabled = false,
hideLabel,
invalid = false,
invalidText = '',
helperText = '',
light,
// eslint-disable-next-line react/prop-types
type = 'password',
// eslint-disable-next-line react/prop-types
togglePasswordVisibility,
tooltipPosition = 'bottom',
tooltipAlignment = 'center',
hidePasswordLabel = 'Hide password',
showPasswordLabel = 'Show password',
size = undefined,
...other
}, ref) {
const prefix = usePrefix();
const controlledPasswordInstanceId = useId();
const errorId = id + '-error-msg';
const textInputClasses = cx(`${prefix}--text-input`, `${prefix}--password-input`, className, {
[`${prefix}--text-input--light`]: light,
[`${prefix}--text-input--invalid`]: invalid,
[`${prefix}--text-input--${size}`]: size
});
const sharedTextInputProps = {
id,
onChange: evt => {
if (!disabled) {
onChange?.(evt);
}
},
onClick: evt => {
if (!disabled) {
onClick?.(evt);
}
},
placeholder,
type,
ref,
className: textInputClasses,
...other
};
const labelClasses = cx(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
[`${prefix}--label--disabled`]: disabled
});
const helperTextClasses = cx(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: disabled
});
const label = labelText ? /*#__PURE__*/React.createElement("label", {
htmlFor: id,
className: labelClasses
}, labelText) : null;
const error = invalid ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--form-requirement`,
id: errorId
}, invalidText) : null;
const passwordIsVisible = type === '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}--btn--icon-only`, `${prefix}--tooltip__trigger`, `${prefix}--tooltip--a11y`, {
[`${prefix}--tooltip--${tooltipPosition}`]: tooltipPosition,
[`${prefix}--tooltip--align-${tooltipAlignment}`]: tooltipAlignment
});
const helperId = !helperText ? undefined : `controlled-password-helper-text-${controlledPasswordInstanceId}`;
const input = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("input", _extends({}, textInputProps({
invalid,
sharedTextInputProps,
invalidId: errorId,
hasHelper: !error && helperText ? true : false,
helperId
}), {
"data-toggle-password-visibility": type === 'password'
})), /*#__PURE__*/React.createElement("button", {
type: "button",
className: passwordVisibilityToggleClasses,
onClick: togglePasswordVisibility
}, /*#__PURE__*/React.createElement("span", {
className: `${prefix}--assistive-text`
}, passwordIsVisible ? hidePasswordLabel : showPasswordLabel), passwordVisibilityIcon));
const helper = helperText ? /*#__PURE__*/React.createElement("div", {
id: helperId,
className: helperTextClasses
}, helperText) : null;
return /*#__PURE__*/React.createElement("div", {
className: `${prefix}--form-item ${prefix}--text-input-wrapper ${prefix}--password-input-wrapper`
}, label, /*#__PURE__*/React.createElement("div", {
className: `${prefix}--text-input__field-wrapper`,
"data-invalid": invalid || null
}, invalid && /*#__PURE__*/React.createElement(WarningFilled, {
className: `${prefix}--text-input__invalid-icon`
}), input), error ? error : helper);
});
ControlledPasswordInput.displayName = 'ControlledPasswordInput';
ControlledPasswordInput.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,
/**
* Specify whether the control is currently invalid
*/
invalid: 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 `ControlledPasswordInput` 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,
/**
* 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.
*/
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']),
/**
* Provide the current value of the `<input>`
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};
export { ControlledPasswordInput as default };