This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(input): re-validate when partially editing date-family inputs #13886
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,12 @@ var WEEK_REGEXP = /^(\d{4})-W(\d\d)$/; | |
var MONTH_REGEXP = /^(\d{4})-(\d\d)$/; | ||
var TIME_REGEXP = /^(\d\d):(\d\d)(?::(\d\d)(\.\d{1,3})?)?$/; | ||
|
||
var PARTIAL_VALIDATION_EVENTS = 'keydown wheel mousedown'; | ||
var PARTIAL_VALIDATION_TYPES = createMap(); | ||
forEach('date,datetime-local,month,time,week'.split(','), function(type) { | ||
PARTIAL_VALIDATION_TYPES[type] = true; | ||
}); | ||
|
||
var inputType = { | ||
|
||
/** | ||
|
@@ -1118,6 +1124,8 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) { | |
}); | ||
} | ||
|
||
var timeout; | ||
|
||
var listener = function(ev) { | ||
if (timeout) { | ||
$browser.defer.cancel(timeout); | ||
|
@@ -1147,8 +1155,6 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) { | |
if ($sniffer.hasEvent('input')) { | ||
element.on('input', listener); | ||
} else { | ||
var timeout; | ||
|
||
var deferListener = function(ev, input, origValue) { | ||
if (!timeout) { | ||
timeout = $browser.defer(function() { | ||
|
@@ -1180,6 +1186,26 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) { | |
// or form autocomplete on newer browser, we need "change" event to catch it | ||
element.on('change', listener); | ||
|
||
// Some native input types (date-family) have the ability to change validity without | ||
// firing any input/change events. | ||
// For these event types, when native validators are present and the browser supports the type, | ||
// check for validity changes on various DOM events. | ||
if (PARTIAL_VALIDATION_TYPES[type] && ctrl.$$hasNativeValidators && type === attr.type) { | ||
element.on(PARTIAL_VALIDATION_EVENTS, function(ev) { | ||
if (!timeout) { | ||
var validity = this[VALIDITY_STATE_PROPERTY]; | ||
var origBadInput = validity.badInput; | ||
var origTypeMismatch = validity.typeMismatch; | ||
timeout = $browser.defer(function() { | ||
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. What's the timeout for? 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. When a |
||
timeout = null; | ||
if (validity.badInput !== origBadInput || validity.typeMismatch !== origTypeMismatch) { | ||
listener(ev); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
ctrl.$render = function() { | ||
// Workaround for Firefox validation #12102. | ||
var value = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The fact that we are reusing the same timeout with the
else
brach above, is not good I think.Since the callbacks are conditionally calling
listener
, there might be a timeout registered, so we don't register another one, but that other timeout's callback may end up not callinglistener
(while our callback would).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.
I've had that concern as well for a while now with
keydown
vscut
/paste
events in the IE case (noinput
event).But in this case, with current browser date-like input support, I don't think there will be any issues. I don't think there is any browser that will not have the
input
event but will have native date-like inputs.I do think this should change at some point though, even if just for the IE case...
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.
True. Having separate timeouts would still be "cleaner", but even the shared timeout isn't indeed expected to create any issues.