Skip to content

Commit

Permalink
Move external validation from EditText to TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetDealer committed Apr 26, 2024
1 parent 5ca9dd1 commit 1893ae6
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/EditText/EditText.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export default class EditText extends HTMLElement {
onChangeValue: []
}

/** @type {InputValidator[]} */
#externalValidators = [];

connectedCallback() {
this.innerHTML = this.#htmlTemplate();
this.#children = {
Expand Down Expand Up @@ -49,7 +46,7 @@ export default class EditText extends HTMLElement {

/** @param {InputValidator} validator */
addExternalValidator(validator) {
this.#externalValidators.push(validator);
this.#children.input.addExternalValidator(validator);
}

get value() {
Expand Down Expand Up @@ -102,7 +99,7 @@ export default class EditText extends HTMLElement {
}

#onEnter() {
if (this.#validateWithExternalValidators()) {
if (this.checkValidity()) {
this.#children.popup.close();
this.#updateDisplayTextAndNotifyIfChanged();
}
Expand All @@ -113,23 +110,12 @@ export default class EditText extends HTMLElement {
event.stopPropagation();
if (event.target !== this.#children.popup)
return;
if (this.#validateWithExternalValidators()) {
if (this.checkValidity()) {
this.#children.popup.close();
this.#updateDisplayTextAndNotifyIfChanged();
}
}

#validateWithExternalValidators() {
for (const validator of this.#externalValidators) {
const result = validator.validate(this, this.value);
if (!result.isValid) {
this.#children.input.errorMessage = result.errorMessage;
return false;
}
}
return true;
}

#updateDisplayTextAndNotifyIfChanged() {
if (!this.#isValid){
this.#children.input.value = this.#getValueAttr();
Expand Down

0 comments on commit 1893ae6

Please sign in to comment.