-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding external validators to TextInput #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ export default class TextInput extends HTMLElement { | |
}; | ||
/** @type {string} */ | ||
#lastChangedValue; | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why ignore? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because in another case, we need to make an unused import of this component. And if we don’t do this, we get an error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. What kind of error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TS2304: Cannot find name 'InputValidator' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, you need to put this explanation whenever you add some "ignore". So the problem is that JS Doc doesn't know what type we're referencing without an import - it doesn't know where this type is coming from. But there seems to be a solution to this problem: https://stackoverflow.com/questions/51982814/reference-definition-from-ts-check-js-file |
||
/** @type {InputValidator} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be an array |
||
#externalValidators = []; | ||
|
||
connectedCallback() { | ||
this.innerHTML = this.#htmlTemplate(); | ||
|
@@ -42,6 +45,12 @@ export default class TextInput extends HTMLElement { | |
window.removeEventListener("visibilitychange", this.#onVisibilityChange.bind(this), { capture: true }); | ||
} | ||
|
||
// @ts-ignore | ||
/** @param {InputValidator} validator */ | ||
addExternalValidator(validator) { | ||
this.#externalValidators.push(validator); | ||
} | ||
|
||
/** | ||
* Returns trimmed value of the input. | ||
* @returns {string} | ||
|
@@ -66,9 +75,20 @@ export default class TextInput extends HTMLElement { | |
} | ||
|
||
checkValidity() { | ||
const isValid = this.#validityState.checkValidity(); | ||
if (!this.#validityState.checkValidity()){ | ||
this.errorMessage = this.#validityState.errorMessage; | ||
return false; | ||
} | ||
this.errorMessage = this.#validityState.errorMessage; | ||
return isValid; | ||
for (const /**@type {InputValidator} */ validator of this.#externalValidators) { | ||
const result = validator.validate(this, this.value); | ||
if (!result.isValid) { | ||
this.errorMessage = result.errorMessage; | ||
this.focus() | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
get errorMessage() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just "validator", it's not "external"