"use client" import { useState } from "react" import { Text, TextField } from "react-aria-components" import { Controller, useFormContext } from "react-hook-form" import { useIntl } from "react-intl" import { CheckIcon, CloseIcon, EyeHideIcon, EyeShowIcon, InfoCircleIcon, } from "@/components/Icons" import AriaInputWithLabel from "@/components/TempDesignSystem/Form/Input/AriaInputWithLabel" import Caption from "@/components/TempDesignSystem/Text/Caption" import { passwordValidators } from "@/utils/passwordValidator" import Button from "../../Button" import { IconProps, type NewPasswordProps } from "./newPassword" import styles from "./newPassword.module.css" import { PasswordValidatorKey } from "@/types/components/form/newPassword" export default function NewPassword({ name = "newPassword", "aria-label": ariaLabel, disabled = false, placeholder = "", registerOptions = {}, label, visibilityToggleable = true, }: NewPasswordProps) { const { control } = useFormContext() const intl = useIntl() const [isPasswordVisible, setIsPasswordVisible] = useState(false) return ( { const errors = Object.values(formState.errors[name]?.types ?? []).flat() return (
{visibilityToggleable ? ( ) : null}
{!field.value && fieldState.error ? ( {fieldState.error.message} ) : null}
) }} /> ) } function Icon({ errorMessage, errors }: IconProps) { return errors.includes(errorMessage) ? ( ) : ( ) } function PasswordValidation({ value, errors, }: { value: string errors: string[] }) { const intl = useIntl() if (!value) return null function getErrorMessage(key: PasswordValidatorKey) { switch (key) { case "length": return `10 ${intl.formatMessage({ id: "to" })} 40 ${intl.formatMessage({ id: "characters" })}` case "hasUppercase": return `1 ${intl.formatMessage({ id: "uppercase letter" })}` case "hasLowercase": return `1 ${intl.formatMessage({ id: "lowercase letter" })}` case "hasNumber": return `1 ${intl.formatMessage({ id: "number" })}` case "hasSpecialChar": return `1 ${intl.formatMessage({ id: "special character" })}` } } return (
{Object.entries(passwordValidators).map(([key, { message }]) => ( {getErrorMessage(key as PasswordValidatorKey)} ))}
) }