import React, { type ChangeEventHandler, type FocusEventHandler, type KeyboardEventHandler, type Ref, useState, } from 'react'; import { classNames } from '../../utils'; import { Input } from '../Input'; import styles from './PasswordInput.module.css'; import { IconButton } from '../IconButton'; import { useTranslate } from '../../hooks/useTranslate'; import type { PasswordRule } from './passwordRule'; import { PasswordValidator } from './PasswordValidator/PasswordValidator'; import { colors } from '../../colors'; export type PasswordInputProps = { ref?: Ref; /** * className for the element. */ className?: string; /** * The rules to apply. */ rules?: PasswordRule[]; /** * Whether the PasswordInput should fit its parent or content. * @default content */ fit?: 'content' | 'parent'; /** * The id of the PasswordInput. */ id?: string; /** * Whether the Input should be disabled. * @default false */ isDisabled?: boolean; /** * The name of the PasswordInput, used when submitting an HTML form. */ name?: string; /** * Temporary text that occupies the PasswordInput when it is empty. */ placeholder?: string; /** * The current value */ value: string | null; /** * Handler that is called when the value changes. */ onChange?: ChangeEventHandler; /** * Handler that is called when the input receives focus. */ onFocus?: FocusEventHandler; /** * Handler that is called when the input loses focus. */ onBlur?: FocusEventHandler; /** * Handler that is called when a key is pressed. */ onKeyDown?: KeyboardEventHandler; }; export const PasswordInput = ({ className, rules = [], fit = 'content', id, isDisabled, name, placeholder, value, onChange, onFocus, onBlur, onKeyDown, ref, ...rest }: PasswordInputProps) => { const [isPasswordVisible, setIsPasswordVisible] = useState(false); const t = useTranslate(); const areAllPasswordRulesValid = ( password: string, rules: PasswordRule[], ) => { return rules.every((rule) => !!rule.validate(password)); }; return (
setIsPasswordVisible(!isPasswordVisible)} isDisabled={isDisabled} /> } /> {rules?.length > 0 && ( )}
); };