"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/zod/passwordValidator" import Button from "../../Button" import { type IconProps, type NewPasswordProps } from "./newPassword" import styles from "./newPassword.module.css" import type { PasswordValidatorKey } from "@/types/components/form/newPassword" export default function NewPassword({ name = "newPassword", "aria-label": ariaLabel, disabled = false, placeholder = "", registerOptions = {}, 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 intl.formatMessage( { id: "{min} to {max} characters", }, { min: 10, max: 40, } ) case "hasUppercase": return intl.formatMessage( { id: "{count} uppercase letter" }, { count: 1 } ) case "hasLowercase": return intl.formatMessage( { id: "{count} lowercase letter" }, { count: 1 } ) case "hasNumber": return intl.formatMessage({ id: "{count} number" }, { count: 1 }) case "hasSpecialChar": return intl.formatMessage( { id: "{count} special character" }, { count: 1 } ) } } return (
{Object.entries(passwordValidators).map(([key, { message }]) => ( {getErrorMessage(key as PasswordValidatorKey)} ))}
) }