@carbon/react
Version:
React components for the Carbon Design System
147 lines (145 loc) • 6.53 kB
JavaScript
/**
* 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
const require_useId = require("../../internal/useId.js");
const require_noopFn = require("../../internal/noopFn.js");
const require_deprecate = require("../../prop-types/deprecate.js");
const require_util = require("./util.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("react/jsx-runtime");
let _carbon_icons_react = require("@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 = (0, react.forwardRef)(({ labelText, className, id, placeholder, onChange = require_noopFn.noopFn, onClick = require_noopFn.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 = require_usePrefix.usePrefix();
const controlledPasswordInstanceId = require_useId.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: (0, classnames.default)(`${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 = (0, classnames.default)(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
[`${prefix}--label--disabled`]: disabled
});
const helperTextClasses = (0, classnames.default)(`${prefix}--form__helper-text`, { [`${prefix}--form__helper-text--disabled`]: disabled });
const label = typeof labelText !== "undefined" && labelText !== null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", {
htmlFor: id,
className: labelClasses,
children: labelText
});
const error = invalid ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--form-requirement`,
id: errorId,
children: invalidText
}) : null;
const passwordIsVisible = type === "text";
const passwordVisibilityIcon = passwordIsVisible ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.ViewOff, { className: `${prefix}--icon-visibility-off` }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.View, { className: `${prefix}--icon-visibility-on` });
const passwordVisibilityToggleClasses = (0, classnames.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 hasHelper = typeof helperText !== "undefined" && helperText !== null;
const helperId = !hasHelper ? void 0 : `controlled-password-helper-text-${controlledPasswordInstanceId}`;
const input = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
...require_util.getTextInputProps({
invalid,
sharedTextInputProps,
invalidId: errorId,
hasHelper: !error && hasHelper,
helperId
}),
"data-toggle-password-visibility": type === "password"
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
type: "button",
className: passwordVisibilityToggleClasses,
onClick: togglePasswordVisibility,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: `${prefix}--assistive-text`,
children: passwordIsVisible ? hidePasswordLabel : showPasswordLabel
}), passwordVisibilityIcon]
})] });
const helper = hasHelper && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
id: helperId,
className: helperTextClasses,
children: helperText
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: `${prefix}--form-item ${prefix}--text-input-wrapper ${prefix}--password-input-wrapper`,
children: [
label,
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: `${prefix}--text-input__field-wrapper`,
"data-invalid": invalid || null,
children: [invalid && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.WarningFilled, { className: `${prefix}--text-input__invalid-icon` }), input]
}),
error ? error : helper
]
});
});
ControlledPasswordInput.displayName = "ControlledPasswordInput";
ControlledPasswordInput.propTypes = {
className: prop_types.default.string,
defaultValue: prop_types.default.oneOfType([prop_types.default.string, prop_types.default.number]),
disabled: prop_types.default.bool,
helperText: prop_types.default.node,
hideLabel: prop_types.default.bool,
hidePasswordLabel: prop_types.default.string,
id: prop_types.default.string.isRequired,
invalid: prop_types.default.bool,
invalidText: prop_types.default.node,
labelText: prop_types.default.node.isRequired,
light: require_deprecate.deprecate(prop_types.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."),
onChange: prop_types.default.func,
onClick: prop_types.default.func,
placeholder: prop_types.default.string,
showPasswordLabel: prop_types.default.string,
size: prop_types.default.oneOf([
"sm",
"md",
"lg"
]),
tooltipAlignment: prop_types.default.oneOf([
"start",
"center",
"end"
]),
tooltipPosition: prop_types.default.oneOf([
"top",
"right",
"bottom",
"left"
]),
value: prop_types.default.oneOfType([prop_types.default.string, prop_types.default.number])
};
//#endregion
exports.default = ControlledPasswordInput;