UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

225 lines (221 loc) 8.03 kB
/** * MSKCC 2021, 2024 */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import React__default, { useRef } 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 { warning } from '../../internal/warning.js'; import deprecate from '../../prop-types/deprecate.js'; import { usePrefix } from '../../internal/usePrefix.js'; import setupGetInstanceId from '../../tools/setupGetInstanceId.js'; const getInstanceId = setupGetInstanceId(); let didWarnAboutDeprecation = false; const ControlledPasswordInput = /*#__PURE__*/React__default.forwardRef(function ControlledPasswordInput(_ref, ref) { let { labelText, className, id, placeholder, onChange, onClick, hideLabel, invalid, 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, ...other } = _ref; const prefix = usePrefix(); const { current: controlledPasswordInstanceId } = useRef(getInstanceId()); if (process.env.NODE_ENV !== "production") { process.env.NODE_ENV !== "production" ? warning(didWarnAboutDeprecation, '`<TextInput.ControlledPasswordInput>` has been deprecated in favor of `<TextInput.PasswordInput />` and will be removed in the next major release of `carbon-components-react`') : void 0; didWarnAboutDeprecation = true; } 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 (!other.disabled) { onChange?.(evt); } }, onClick: evt => { if (!other.disabled) { onClick?.(evt); } }, placeholder, type, ref, className: textInputClasses, ...other }; const labelClasses = cx(`${prefix}--label`, { [`${prefix}--visually-hidden`]: hideLabel, [`${prefix}--label--disabled`]: other.disabled }); const helperTextClasses = cx(`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: other.disabled }); const label = labelText ? /*#__PURE__*/React__default.createElement("label", { htmlFor: id, className: labelClasses }, labelText) : null; const error = invalid ? /*#__PURE__*/React__default.createElement("div", { className: `${prefix}--form-requirement`, id: errorId }, invalidText) : null; const passwordIsVisible = type === 'text'; const passwordVisibilityIcon = passwordIsVisible ? /*#__PURE__*/React__default.createElement(ViewOff, { className: `${prefix}--icon-visibility-off` }) : /*#__PURE__*/React__default.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__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("input", _extends({}, textInputProps({ invalid, sharedTextInputProps, invalidId: errorId, hasHelper: !error && helperText ? true : false, helperId }), { "data-toggle-password-visibility": type === 'password' })), /*#__PURE__*/React__default.createElement("button", { type: "button", className: passwordVisibilityToggleClasses, onClick: togglePasswordVisibility }, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--assistive-text` }, passwordIsVisible ? hidePasswordLabel : showPasswordLabel), passwordVisibilityIcon)); const helper = helperText ? /*#__PURE__*/React__default.createElement("div", { id: helperId, className: helperTextClasses }, helperText) : null; return /*#__PURE__*/React__default.createElement("div", { className: `${prefix}--form-item ${prefix}--text-input-wrapper ${prefix}--password-input-wrapper` }, label, /*#__PURE__*/React__default.createElement("div", { className: `${prefix}--text-input__field-wrapper`, "data-invalid": invalid || null }, invalid && /*#__PURE__*/React__default.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]) }; ControlledPasswordInput.defaultProps = { disabled: false, onChange: () => {}, onClick: () => {}, invalid: false, invalidText: '', helperText: '', size: undefined }; export { ControlledPasswordInput as default };