UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

235 lines (227 loc) 9.19 kB
/** * MSKCC 2021, 2024 */ '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 util = require('./util.js'); var warning = require('../../internal/warning.js'); var deprecate = require('../../prop-types/deprecate.js'); var usePrefix = require('../../internal/usePrefix.js'); var setupGetInstanceId = require('../../tools/setupGetInstanceId.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 getInstanceId = setupGetInstanceId["default"](); let didWarnAboutDeprecation = false; const ControlledPasswordInput = /*#__PURE__*/React__default["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.usePrefix(); const { current: controlledPasswordInstanceId } = React.useRef(getInstanceId()); if (process.env.NODE_ENV !== "production") { process.env.NODE_ENV !== "production" ? warning.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__default["default"](`${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__default["default"](`${prefix}--label`, { [`${prefix}--visually-hidden`]: hideLabel, [`${prefix}--label--disabled`]: other.disabled }); const helperTextClasses = cx__default["default"](`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: other.disabled }); const label = labelText ? /*#__PURE__*/React__default["default"].createElement("label", { htmlFor: id, className: labelClasses }, labelText) : null; const error = invalid ? /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-requirement`, id: errorId }, invalidText) : null; const passwordIsVisible = type === '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}--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["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("input", _rollupPluginBabelHelpers["extends"]({}, util.textInputProps({ invalid, sharedTextInputProps, invalidId: errorId, hasHelper: !error && helperText ? true : false, helperId }), { "data-toggle-password-visibility": type === 'password' })), /*#__PURE__*/React__default["default"].createElement("button", { type: "button", className: passwordVisibilityToggleClasses, onClick: togglePasswordVisibility }, /*#__PURE__*/React__default["default"].createElement("span", { className: `${prefix}--assistive-text` }, passwordIsVisible ? hidePasswordLabel : showPasswordLabel), passwordVisibilityIcon)); const helper = helperText ? /*#__PURE__*/React__default["default"].createElement("div", { id: helperId, className: helperTextClasses }, helperText) : null; return /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-item ${prefix}--text-input-wrapper ${prefix}--password-input-wrapper` }, label, /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--text-input__field-wrapper`, "data-invalid": invalid || null }, invalid && /*#__PURE__*/React__default["default"].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__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, /** * Specify whether the control is currently invalid */ invalid: 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 `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__default["default"].func, /** * Optionally provide an `onClick` handler that is called whenever the * `<input>` is clicked */ onClick: 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. */ 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']), /** * Provide the current value of the `<input>` */ value: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].number]) }; ControlledPasswordInput.defaultProps = { disabled: false, onChange: () => {}, onClick: () => {}, invalid: false, invalidText: '', helperText: '', size: undefined }; exports["default"] = ControlledPasswordInput;