Skip to content

Commit 47113c1

Browse files
committed
more progress, this time only doing keystroke validations on error
1 parent f795c58 commit 47113c1

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# TODO: Should we switch back out of always-on validation once the input passes validation, or stay in always-on?
2-
31
# <auto-check> element
42

53
An input element that validates its value against a server endpoint.
@@ -161,7 +159,6 @@ npm test
161159
```
162160
163161
TODO: Add note about uncommenting line at bottom of examples for local development
164-
Input something other than 422 for error response?
165162
166163
## License
167164

src/auto-check-element.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,13 @@ function handleChange(checker: () => void, event: Event) {
210210
const autoCheckElement = input.closest('auto-check')
211211
if (!(autoCheckElement instanceof AutoCheckElement)) return
212212

213-
if (event.type === 'blur') {
214-
if (autoCheckElement.validateAfterFirstBlur && !autoCheckElement.shouldValidate) {
215-
autoCheckElement.setAttribute('should-validate', '')
213+
if (input.value.length === 0) return
216214

217-
checker()
218-
setLoadingState(event)
219-
}
220-
} else if (autoCheckElement.shouldValidate) {
215+
if (
216+
(event.type !== 'blur' && !autoCheckElement.validateAfterFirstBlur) || // Existing default behavior
217+
(event.type === 'blur' && autoCheckElement.validateAfterFirstBlur) || // Only validate on blur if validate-after-first-blur is set
218+
(autoCheckElement.validateAfterFirstBlur && autoCheckElement.shouldValidate) // Only validate on key inputs in validate-after-first-blur mode if should-validate is set (when input is invalid)
219+
) {
221220
checker()
222221
setLoadingState(event)
223222
}
@@ -334,8 +333,12 @@ async function check(autoCheckElement: AutoCheckElement) {
334333
if (autoCheckElement.required) {
335334
input.setCustomValidity('')
336335
}
336+
if (autoCheckElement.validateAfterFirstBlur) {
337+
autoCheckElement.removeAttribute('should-validate')
338+
}
337339
input.dispatchEvent(new AutoCheckSuccessEvent(response.clone()))
338340
} else {
341+
autoCheckElement.setAttribute('should-validate', '')
339342
const event = new AutoCheckErrorEvent(response.clone())
340343
input.dispatchEvent(event)
341344
if (autoCheckElement.required) {

0 commit comments

Comments
 (0)