# All files / src/form Password.tsx

0% Statements 0/212
0% Branches 0/1
0% Functions 0/1
0% Lines 0/212

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213

     "use client";
    import { forwardRef, type ReactNode } from "react";
    import { Button, type ButtonProps } from "../button/Button.js";
    import { getIcon } from "../icon/iconConfig.js";
    import { type PropsWithRef } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { useToggle } from "../useToggle.js";
    import {
      password,
      passwordInput,
      passwordInputToggle,
    } from "./passwordStyles.js";
    import { TextField, type TextFieldProps } from "./TextField.js";

    /**
     * @example
     * Simple Example
     * ```tsx
     * const getVisibilityIcon: GetPasswordVisibilityIcon = (isPasswordVisible) => {
     *   if (isPasswordVisible) {
     *     return <SecurityIcon />;
     *   }
     *
     *   return <RemoveRedEyeIcon />;
     * };
     * ```
     *
     * @param isPasswordVisible - `true` when the password is visible and in plain
     * text
     * @returns a custom icon to use for the password visibility toggle.
     */
    export type GetPasswordVisibilityIcon = (
      isPasswordVisible: boolean
    ) => ReactNode;

    /**
     * @example
     * Simple Example
     * ```tsx
     * const visibilityIcon: ConfigurableVisibilityIcon = {
     *   visible: <SecurityIcon />,
     *   invisible: <RemoveRedEyeIcon />,
     * };
     * ```
     */
    export interface ConfigurableVisibilityIcon {
      /**
       * The icon to display while the password is currently visible as plain text.
       */
      visible: ReactNode;

      /**
       * The icon to display while the password is currently invisible as the
       * password input.
       */
      invisible: ReactNode;
    }

    export interface PasswordProps
      extends Omit<TextFieldProps, "type" | "rightAddon"> {
      /**
       * @defaultValue `"password"`
       * @remarks \@since 6.0.0 Defaults to `"password"`
       */
      name?: string;

      /**
       * @example
       * Configurable Visibility Icon Object
       * ```tsx
       * <Password
       *   {...props}
       *   visibilityIcon={{
       *     visible: <SecurityIcon />,
       *     invisible: <RemoveRedEyeIcon />,
       *   }}
       * />
       * ```
       *
       * @example
       * Get Password Visibility Icon Function
       * ```tsx
       * <Password
       *   {...props}
       *   visibilityIcon={(isPasswordVisible) => {
       *     if (isPasswordVisible) {
       *       return <SecurityIcon />;
       *     }
       *
       *     return <RemoveRedEyeIcon />;
       *   }}
       * />
       * ```
       *
       * @example
       * Custom Icon
       * ```tsx
       * <Password
       *   {...props}
       *   visibilityIcon={<SomeCustomComponent />}
       * />
       * ```
       */
      visibilityIcon?:
        | ConfigurableVisibilityIcon
        | GetPasswordVisibilityIcon
        | ReactNode;

      /**
       * The `aria-label` to use for the password visibility icon button.
       *
       * @defaultValue `"Show password"`
       */
      visibilityLabel?: string;

      /**
       * Any props that should be passed to the password visibility icon button. If
       * `id`, `buttonType`, `aria-label`, `aria-pressed`, or `children` are
       * provided here, they will override the default implementation.
       */
      visibilityProps?: PropsWithRef<ButtonProps, HTMLButtonElement>;
    }

    /**
     * **Client Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import { Password } from "@react-md/core";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Password
     *       label="Password"
     *       name="password"
     *       required
     *     />
     *   );
     * }
     * ```
     */
    export const Password = forwardRef<HTMLInputElement, PasswordProps>(
      function Password(props, ref) {
        const {
          id: propId,
          name = "password",
          className,
          inputClassName,
          visibilityIcon: propVisibilityIcon,
          visibilityLabel = "Show password",
          visibilityProps,
          ...remaining
        } = props;
        const { toggled: isPasswordVisible, toggle } = useToggle(false);

        let currentVisibilityIcon: ReactNode;
        if (
          propVisibilityIcon &&
          typeof propVisibilityIcon === "object" &&
          "visible" in propVisibilityIcon
        ) {
          currentVisibilityIcon = isPasswordVisible
            ? propVisibilityIcon.visible
            : propVisibilityIcon.invisible;
        } else if (typeof propVisibilityIcon === "function") {
          currentVisibilityIcon = propVisibilityIcon(isPasswordVisible);
        } else {
          currentVisibilityIcon = propVisibilityIcon;
        }

        const id = useEnsuredId(propId, "password");
        const visibilityIcon = getIcon("password", currentVisibilityIcon);

        return (
          <TextField
            {...remaining}
            ref={ref}
            name={name}
            type={isPasswordVisible ? "text" : "password"}
            className={password({ className })}
            inputClassName={passwordInput({ className: inputClassName })}
            rightAddon={
              <Button
                id={`${id}-toggle`}
                buttonType="icon"
                aria-label={visibilityLabel}
                aria-pressed={isPasswordVisible}
                // allow all props except the onClick, className, and aria-pressed to
                // be overridden. onClick can only stop default behavior with
                // `event.stopPropagation()`
                {...visibilityProps}
                className={passwordInputToggle({
                  className: visibilityProps?.className,
                })}
                onClick={(event) => {
                  visibilityProps?.onClick?.(event);
                  if (event.isPropagationStopped()) {
                    return;
                  }

                  toggle();
                }}
              >
                {visibilityProps?.children ?? visibilityIcon}
              </Button>
            }
            disableRightAddonStyles
          />
        );
      }
    );