|
| 1 | +import React, { |
| 2 | + type DetailedHTMLProps, |
| 3 | + forwardRef, |
| 4 | + type InputHTMLAttributes, |
| 5 | + memo, |
| 6 | + type ReactNode, |
| 7 | + useId |
| 8 | +} from "react"; |
| 9 | +import { assert, type Equals } from "tsafe/assert"; |
| 10 | +import { symToStr } from "tsafe/symToStr"; |
| 11 | +import { fr } from "../fr"; |
| 12 | +import { createComponentI18nApi } from "../i18n"; |
| 13 | +import type { InputProps } from "../Input"; |
| 14 | +import { cx } from "../tools/cx"; |
| 15 | +import type { FrClassName } from "../fr/generatedFromCss/classNames"; |
| 16 | + |
| 17 | +export type PasswordInputProps = Omit< |
| 18 | + InputProps.Common, |
| 19 | + "state" | "stateRelatedMessage" | "iconId" | "classes" |
| 20 | +> & { |
| 21 | + classes?: Partial<Record<"root" | "input" | "label" | "checkbox", string>>; |
| 22 | + messages?: { |
| 23 | + severity: PasswordInputProps.Severity; |
| 24 | + message: ReactNode; |
| 25 | + }[]; |
| 26 | + nativeInputProps?: Omit< |
| 27 | + DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, |
| 28 | + "type" |
| 29 | + >; |
| 30 | +}; |
| 31 | + |
| 32 | +export namespace PasswordInputProps { |
| 33 | + type ExtractSeverity<ClassName extends string> = |
| 34 | + ClassName extends `fr-message--${infer Severity}` ? Severity : never; |
| 35 | + |
| 36 | + export type Severity = ExtractSeverity<FrClassName>; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @see <https://react-dsfr-components.etalab.studio/?path=/docs/blocks-passwordinput |
| 41 | + * */ |
| 42 | +export const PasswordInput = memo( |
| 43 | + forwardRef<HTMLDivElement, PasswordInputProps>((props, ref) => { |
| 44 | + const { |
| 45 | + className, |
| 46 | + label, |
| 47 | + hintText, |
| 48 | + hideLabel, |
| 49 | + disabled = false, |
| 50 | + classes = {}, |
| 51 | + style, |
| 52 | + messages = [], |
| 53 | + nativeInputProps, |
| 54 | + ...rest |
| 55 | + } = props; |
| 56 | + |
| 57 | + assert<Equals<keyof typeof rest, never>>(); |
| 58 | + |
| 59 | + const { t } = useTranslation(); |
| 60 | + |
| 61 | + const inputId = (function useClosure() { |
| 62 | + const id = useId(); |
| 63 | + |
| 64 | + return nativeInputProps?.id ?? `password-${id}`; |
| 65 | + })(); |
| 66 | + const containerId = `${inputId}-container`; |
| 67 | + const togglePasswordShowId = `${inputId}-toggle-show`; |
| 68 | + const messagesGroupId = `${inputId}-messages-group`; |
| 69 | + const messageGroupId = `${inputId}-message-group`; |
| 70 | + |
| 71 | + const hasError = messages.find(({ severity }) => severity === "error") !== undefined; |
| 72 | + const isSuccess = |
| 73 | + messages.length !== 0 && |
| 74 | + messages.find(({ severity }) => severity !== "valid") === undefined; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div |
| 78 | + className={cx( |
| 79 | + fr.cx( |
| 80 | + "fr-password", |
| 81 | + disabled && "fr-input-group--disabled", |
| 82 | + hasError && "fr-input-group--error", |
| 83 | + isSuccess && "fr-input-group--valid" |
| 84 | + ), |
| 85 | + classes.root, |
| 86 | + className |
| 87 | + )} |
| 88 | + id={containerId} |
| 89 | + style={style} |
| 90 | + ref={ref} |
| 91 | + {...rest} |
| 92 | + > |
| 93 | + <label |
| 94 | + className={cx(fr.cx("fr-label", hideLabel && "fr-sr-only"), classes.label)} |
| 95 | + htmlFor={inputId} |
| 96 | + > |
| 97 | + {label} |
| 98 | + {hintText !== undefined && <span className="fr-hint-text">{hintText}</span>} |
| 99 | + </label> |
| 100 | + <div className={fr.cx("fr-input-wrap")}> |
| 101 | + <input |
| 102 | + {...nativeInputProps} |
| 103 | + className={cx(fr.cx("fr-password__input", "fr-input"), classes.input)} |
| 104 | + id={inputId} |
| 105 | + type="password" |
| 106 | + disabled={disabled} |
| 107 | + {...(messages.length !== 0 && { "aria-describedby": messagesGroupId })} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + {messages.length !== 0 && ( |
| 111 | + <div |
| 112 | + className={fr.cx("fr-messages-group")} |
| 113 | + id={messagesGroupId} |
| 114 | + aria-live="assertive" |
| 115 | + > |
| 116 | + <p className={fr.cx("fr-message")} id={messageGroupId}> |
| 117 | + {t("your password must contain")} |
| 118 | + </p> |
| 119 | + {messages.map(({ severity, message }, index) => ( |
| 120 | + <p |
| 121 | + key={index} |
| 122 | + className={fr.cx("fr-message", `fr-message--${severity}`)} |
| 123 | + id={`${messageGroupId}-${index}`} |
| 124 | + > |
| 125 | + {message} |
| 126 | + </p> |
| 127 | + ))} |
| 128 | + </div> |
| 129 | + )} |
| 130 | + <div |
| 131 | + className={cx( |
| 132 | + fr.cx( |
| 133 | + "fr-password__checkbox", |
| 134 | + "fr-checkbox-group", |
| 135 | + "fr-checkbox-group--sm" |
| 136 | + ), |
| 137 | + classes.checkbox |
| 138 | + )} |
| 139 | + > |
| 140 | + <input |
| 141 | + aria-label={t("show password")} |
| 142 | + id={togglePasswordShowId} |
| 143 | + type="checkbox" |
| 144 | + disabled={disabled || undefined} |
| 145 | + /> |
| 146 | + <label |
| 147 | + className={cx(fr.cx("fr-password__checkbox", "fr-label"), classes.checkbox)} |
| 148 | + htmlFor={togglePasswordShowId} |
| 149 | + > |
| 150 | + {t("show")} |
| 151 | + </label> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + ); |
| 155 | + }) |
| 156 | +); |
| 157 | + |
| 158 | +const { useTranslation, addPasswordInputTranslations } = createComponentI18nApi({ |
| 159 | + "componentName": symToStr({ PasswordInput }), |
| 160 | + "frMessages": { |
| 161 | + /* spell-checker: disable */ |
| 162 | + "show": "Afficher", |
| 163 | + "show password": "Afficher le mot de passe", |
| 164 | + "your password must contain": "Votre mot de passe doit contenir :" |
| 165 | + /* spell-checker: enable */ |
| 166 | + } |
| 167 | +}); |
| 168 | + |
| 169 | +addPasswordInputTranslations({ |
| 170 | + "lang": "en", |
| 171 | + "messages": { |
| 172 | + "show": "Show", |
| 173 | + "show password": "Show password", |
| 174 | + "your password must contain": "Your password must contain:" |
| 175 | + } |
| 176 | +}); |
| 177 | + |
| 178 | +addPasswordInputTranslations({ |
| 179 | + "lang": "es", |
| 180 | + "messages": { |
| 181 | + /* spell-checker: disable */ |
| 182 | + "show": "Mostrar", |
| 183 | + "show password": "Mostrar contraseña", |
| 184 | + "your password must contain": "Su contraseña debe contener:" |
| 185 | + /* spell-checker: enable */ |
| 186 | + } |
| 187 | +}); |
| 188 | + |
| 189 | +PasswordInput.displayName = symToStr({ PasswordInput }); |
| 190 | + |
| 191 | +export default PasswordInput; |
0 commit comments