import { BaseController } from "../../utilities/base_controller" ; import { useCollectionEventListener } from "../../mixins/use_event_listener" ; import { installClassMethods } from "../../mixins/install_class_methods" ; export class PasswordConfirmController extends BaseController { static targets = [ "password" ] ; static classes = [ "error" ] ; declare readonly passwordTargets : HTMLInputElement [ ] ; declare addErrorClasses : ( el ? : HTMLElement ) => void ; declare removeErrorClasses : ( el ? : HTMLElement ) => void ; connect ( ) { installClassMethods ( this ) ; useCollectionEventListener ( this , this . passwordTargets , "change" , this . _checkPasswordsMatch ) ; } private _allPasswordsMatch ( ) { let values = new Set ( this . passwordTargets . map ( el => el . value ) ) ; return values . has ( "" ) || values . size == 1 ; } private _checkPasswordsMatch ( ) { let element = this . el ; if ( this . _allPasswordsMatch ( ) ) { this . dispatchEvent ( element , "password-confirm:match" ) ; this . passwordTargets . forEach ( el => this . removeErrorClasses ( el ) ) ; } else { this . dispatchEvent ( element , "password-confirm:no-match" ) ; this . passwordTargets . forEach ( el => this . addErrorClasses ( el ) ) ; } } }