"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 { passwordValidators } from "@scandic-hotels/common/utils/zod/passwordValidator" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { Input } from "@scandic-hotels/design-system/Input" import Button from "@/components/TempDesignSystem/Button" import Caption from "@/components/TempDesignSystem/Text/Caption" import { getErrorMessage } from "../Input/errors" import styles from "./passwordInput.module.css" import type { PasswordValidatorKey } from "@/types/components/form/newPassword" import type { IconProps, PasswordInputProps } from "./passwordInput" export default function PasswordInput({ name = "password", label, "aria-label": ariaLabel, disabled = false, placeholder, registerOptions = {}, visibilityToggleable = true, isNewPassword = false, className = "", }: PasswordInputProps) { const { control } = useFormContext() const intl = useIntl() const [isPasswordVisible, setIsPasswordVisible] = useState(false) return ( { const errors = isNewPassword ? Object.values(formState.errors[name]?.types ?? []).flat() : [] return (
{visibilityToggleable ? ( ) : null}
{isNewPassword ? ( ) : null} {isNewPassword ? ( !field.value && fieldState.error ? ( {getErrorMessage(intl, fieldState.error.message)} ) : null ) : fieldState.error ? ( {getErrorMessage(intl, fieldState.error.message)} ) : null}
) }} /> ) } function Icon({ errorMessage, errors }: IconProps) { return errors.includes(errorMessage) ? ( ) : ( ) } function NewPasswordValidation({ value, errors, }: { value: string errors: string[] }) { const intl = useIntl() if (!value) return null function getErrorMessage(key: PasswordValidatorKey) { switch (key) { case "length": return intl.formatMessage( { defaultMessage: "{min} to {max} characters", }, { min: 10, max: 40, } ) case "hasUppercase": return intl.formatMessage( { defaultMessage: "{count} uppercase letter", }, { count: 1 } ) case "hasLowercase": return intl.formatMessage( { defaultMessage: "{count} lowercase letter", }, { count: 1 } ) case "hasNumber": return intl.formatMessage( { defaultMessage: "{count} number", }, { count: 1 } ) case "hasSpecialChar": return intl.formatMessage( { defaultMessage: "{count} special character", }, { count: 1 } ) } } return (
{Object.entries(passwordValidators).map(([key, { message }]) => ( {getErrorMessage(key as PasswordValidatorKey)} ))}
) }