@carbon/react
Version:
React components for the Carbon Design System
214 lines (208 loc) • 7.48 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.
*/
;
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 util = require('./util.js');
var deprecate = require('../../prop-types/deprecate.js');
var usePrefix = require('../../internal/usePrefix.js');
var useId = require('../../internal/useId.js');
var noopFn = require('../../internal/noopFn.js');
const ControlledPasswordInput = /*#__PURE__*/React.forwardRef(({
labelText,
className,
id,
placeholder,
onChange = noopFn.noopFn,
onClick = noopFn.noopFn,
disabled = false,
hideLabel,
invalid = false,
invalidText = '',
helperText = '',
light,
type = 'password',
togglePasswordVisibility,
tooltipPosition = 'bottom',
tooltipAlignment = 'center',
hidePasswordLabel = 'Hide password',
showPasswordLabel = 'Show password',
size = undefined,
...other
}, ref) => {
const prefix = usePrefix.usePrefix();
const controlledPasswordInstanceId = useId.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 = typeof labelText !== 'undefined' && labelText !== null && /*#__PURE__*/React.createElement("label", {
htmlFor: id,
className: labelClasses
}, labelText);
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(iconsReact.ViewOff, {
className: `${prefix}--icon-visibility-off`
}) : /*#__PURE__*/React.createElement(iconsReact.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 hasHelper = typeof helperText !== 'undefined' && helperText !== null;
const helperId = !hasHelper ? undefined : `controlled-password-helper-text-${controlledPasswordInstanceId}`;
const input = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("input", _rollupPluginBabelHelpers.extends({}, util.getTextInputProps({
invalid,
sharedTextInputProps,
invalidId: errorId,
hasHelper: !error && hasHelper,
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 = hasHelper && /*#__PURE__*/React.createElement("div", {
id: helperId,
className: helperTextClasses
}, helperText);
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(iconsReact.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.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])
};
exports.default = ControlledPasswordInput;