UNPKG

@carbon/react

Version:

React components for the Carbon Design System

143 lines (141 loc) 5.59 kB
/** * Copyright IBM Corp. 2016, 2026 * * 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 { usePrefix } from "../../internal/usePrefix.js"; import { useId } from "../../internal/useId.js"; import { noopFn } from "../../internal/noopFn.js"; import { deprecate } from "../../prop-types/deprecate.js"; import { getTextInputProps } from "./util.js"; import classNames from "classnames"; import { forwardRef } from "react"; import PropTypes from "prop-types"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { View, ViewOff, WarningFilled } from "@carbon/icons-react"; //#region src/components/TextInput/ControlledPasswordInput.tsx /** * Copyright IBM Corp. 2023, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const ControlledPasswordInput = forwardRef(({ labelText, className, id, placeholder, onChange = noopFn, onClick = noopFn, disabled = false, hideLabel, invalid = false, invalidText = "", helperText, light, type = "password", togglePasswordVisibility, tooltipPosition = "bottom", tooltipAlignment = "center", hidePasswordLabel = "Hide password", showPasswordLabel = "Show password", size = void 0, ...other }, ref) => { const prefix = usePrefix(); const controlledPasswordInstanceId = useId(); const errorId = id + "-error-msg"; const sharedTextInputProps = { id, onChange: (evt) => { if (!disabled) onChange?.(evt); }, onClick: (evt) => { if (!disabled) onClick?.(evt); }, placeholder, type, ref, className: classNames(`${prefix}--text-input`, `${prefix}--password-input`, className, { [`${prefix}--text-input--light`]: light, [`${prefix}--text-input--invalid`]: invalid, [`${prefix}--text-input--${size}`]: size }), ...other }; const labelClasses = classNames(`${prefix}--label`, { [`${prefix}--visually-hidden`]: hideLabel, [`${prefix}--label--disabled`]: disabled }); const helperTextClasses = classNames(`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: disabled }); const label = typeof labelText !== "undefined" && labelText !== null && /* @__PURE__ */ jsx("label", { htmlFor: id, className: labelClasses, children: labelText }); const error = invalid ? /* @__PURE__ */ jsx("div", { className: `${prefix}--form-requirement`, id: errorId, children: invalidText }) : null; const passwordIsVisible = type === "text"; const passwordVisibilityIcon = passwordIsVisible ? /* @__PURE__ */ jsx(ViewOff, { className: `${prefix}--icon-visibility-off` }) : /* @__PURE__ */ jsx(View, { className: `${prefix}--icon-visibility-on` }); const passwordVisibilityToggleClasses = classNames(`${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 ? void 0 : `controlled-password-helper-text-${controlledPasswordInstanceId}`; const input = /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("input", { ...getTextInputProps({ invalid, sharedTextInputProps, invalidId: errorId, hasHelper: !error && hasHelper, helperId }), "data-toggle-password-visibility": type === "password" }), /* @__PURE__ */ jsxs("button", { type: "button", className: passwordVisibilityToggleClasses, onClick: togglePasswordVisibility, children: [/* @__PURE__ */ jsx("span", { className: `${prefix}--assistive-text`, children: passwordIsVisible ? hidePasswordLabel : showPasswordLabel }), passwordVisibilityIcon] })] }); const helper = hasHelper && /* @__PURE__ */ jsx("div", { id: helperId, className: helperTextClasses, children: helperText }); return /* @__PURE__ */ jsxs("div", { className: `${prefix}--form-item ${prefix}--text-input-wrapper ${prefix}--password-input-wrapper`, children: [ label, /* @__PURE__ */ jsxs("div", { className: `${prefix}--text-input__field-wrapper`, "data-invalid": invalid || null, children: [invalid && /* @__PURE__ */ jsx(WarningFilled, { className: `${prefix}--text-input__invalid-icon` }), input] }), error ? error : helper ] }); }); ControlledPasswordInput.displayName = "ControlledPasswordInput"; ControlledPasswordInput.propTypes = { className: PropTypes.string, defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), disabled: PropTypes.bool, helperText: PropTypes.node, hideLabel: PropTypes.bool, hidePasswordLabel: PropTypes.string, id: PropTypes.string.isRequired, invalid: PropTypes.bool, invalidText: PropTypes.node, labelText: PropTypes.node.isRequired, 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."), onChange: PropTypes.func, onClick: PropTypes.func, placeholder: PropTypes.string, showPasswordLabel: PropTypes.string, size: PropTypes.oneOf([ "sm", "md", "lg" ]), tooltipAlignment: PropTypes.oneOf([ "start", "center", "end" ]), tooltipPosition: PropTypes.oneOf([ "top", "right", "bottom", "left" ]), value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }; //#endregion export { ControlledPasswordInput as default };