import PasswordElement, {PasswordElementProps} from './PasswordElement' import {FieldPath, FieldValues, useWatch} from 'react-hook-form' import {forwardRef, type ReactElement, Ref, RefAttributes} from 'react' export type PasswordRepeatElementProps< TFieldValues extends FieldValues = FieldValues, TConfirmPasswordName extends FieldPath = FieldPath, TPasswordName extends FieldPath = FieldPath, TConfirmPasswordValue = unknown, > = PasswordElementProps< TFieldValues, TConfirmPasswordName, TConfirmPasswordValue > & { passwordFieldName: TPasswordName customInvalidFieldMessage?: string } type PasswordRepeatElementComponent = < TFieldValues extends FieldValues = FieldValues, TConfirmPasswordName extends FieldPath = FieldPath, TPasswordName extends FieldPath = FieldPath, >( props: PasswordRepeatElementProps< TFieldValues, TConfirmPasswordName, TPasswordName > & RefAttributes ) => ReactElement const PasswordRepeatElement = forwardRef(function PasswordRepeatElement< TFieldValues extends FieldValues = FieldValues, TConfirmPasswordName extends FieldPath = FieldPath, TPasswordName extends FieldPath = FieldPath, TConfirmPasswordValue = unknown, >( props: PasswordRepeatElementProps< TFieldValues, TConfirmPasswordName, TPasswordName, TConfirmPasswordValue >, ref: Ref ) { const {passwordFieldName, customInvalidFieldMessage, control, ...rest} = props const pwValue = useWatch({ name: passwordFieldName, control, }) return ( { return ( value === pwValue || (customInvalidFieldMessage ?? 'Password should match') ) }, }} /> ) }) PasswordRepeatElement.displayName = 'PasswordRepeatElement' export default PasswordRepeatElement as PasswordRepeatElementComponent