@stimulus-library/controllers
Version:
A library of useful controllers for Stimulus
26 lines (25 loc) • 1.06 kB
JavaScript
import { BaseController } from "@stimulus-library/utilities";
import { installClassMethods, useCollectionEventListener } from "@stimulus-library/mixins";
export class PasswordConfirmController extends BaseController {
connect() {
installClassMethods(this);
useCollectionEventListener(this, this.passwordTargets, "change", this._checkPasswordsMatch);
}
_allPasswordsMatch() {
const values = new Set(this.passwordTargets.map(el => el.value));
return values.has("") || values.size == 1;
}
_checkPasswordsMatch() {
const element = this.el;
if (this._allPasswordsMatch()) {
this.dispatchEvent(element, this.eventName("match"));
this.passwordTargets.forEach(el => this.removeErrorClasses(el));
}
else {
this.dispatchEvent(element, this.eventName("no-match"));
this.passwordTargets.forEach(el => this.addErrorClasses(el));
}
}
}
PasswordConfirmController.targets = ["password"];
PasswordConfirmController.classes = ["error"];