Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit b406623

Browse files
committed
fix(datepicker): time is modified before even selecting a date
- `updateErrorState()` was mutating the model incorrectly and clearing the time to midnight when the datepicker is first interacted with, without selecting a date Fixes #12028. Closes #12026.
1 parent 7856883 commit b406623

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/components/datepicker/js/calendar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
/**
175175
* The date that is currently focused or showing in the calendar. This will initially be set
176176
* to the ng-model value if set, otherwise to today. It will be updated as the user navigates
177-
* to other months. The cell corresponding to the displayDate does not necesarily always have
177+
* to other months. The cell corresponding to the displayDate does not necessarily always have
178178
* focus in the document (such as for cases when the user is scrolling the calendar).
179179
* @type {Date}
180180
*/

src/components/datepicker/js/datepickerDirective.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,10 @@
339339
*/
340340
this.$scope = $scope;
341341

342-
/** @type {Date} */
342+
/**
343+
* This holds the model that will be used by the calendar.
344+
* @type {Date|null|undefined}
345+
*/
343346
this.date = null;
344347

345348
/** @type {boolean} */
@@ -631,7 +634,12 @@
631634
* @param {Date=} opt_date Date to check. If not given, defaults to the datepicker's model value.
632635
*/
633636
DatePickerCtrl.prototype.updateErrorState = function(opt_date) {
634-
var date = opt_date || this.date;
637+
var date;
638+
if (opt_date) {
639+
date = new Date(opt_date.valueOf());
640+
} else {
641+
date = angular.copy(this.ngModelCtrl.$modelValue);
642+
}
635643

636644
// Clear any existing errors to get rid of anything that's no longer relevant.
637645
this.clearErrorState();
@@ -840,7 +848,7 @@
840848

841849
/**
842850
* Open the floating calendar pane.
843-
* @param {Event} event
851+
* @param {MouseEvent|KeyboardEvent|{target: HTMLInputElement}} event
844852
*/
845853
DatePickerCtrl.prototype.openCalendarPane = function(event) {
846854
if (!this.isCalendarOpen && !this.isDisabled && !this.inputFocusedOnWindowBlur) {
@@ -901,7 +909,7 @@
901909
}
902910
}
903911

904-
function reset(){
912+
function reset() {
905913
self.isCalendarOpen = self.isOpen = false;
906914
}
907915
};
@@ -916,7 +924,7 @@
916924
// Use a timeout in order to allow the calendar to be rendered, as it is gated behind an ng-if.
917925
var self = this;
918926
this.$mdUtil.nextTick(function() {
919-
self.getCalendarCtrl().focusDate();
927+
self.getCalendarCtrl().focusDate(self.date);
920928
}, false);
921929
};
922930

@@ -1007,6 +1015,7 @@
10071015
var self = this;
10081016
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
10091017

1018+
// Update the model used by the calendar.
10101019
if (this.dateUtil.isValidDate(value) && timezone != null && value.getTimezoneOffset() >= 0) {
10111020
this.date = this.dateUtil.removeLocalTzAndReparseDate(value);
10121021
} else {

src/components/datepicker/js/datepickerDirective.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ describe('md-datepicker', function() {
153153
createDatepickerInstance(
154154
'<md-datepicker ng-model="myDate" ng-model-options="modelOptions"></md-datepicker>');
155155

156-
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'UTC');
156+
// If running in a GMT+X timezone, formatDate will not be called with a timezone argument.
157+
if (pageScope.myDate.getTimezoneOffset() < 0) {
158+
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate);
159+
} else {
160+
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'UTC');
161+
}
157162
});
158163

159164
it('should allow for the locale to be overwritten on a specific element', function() {

0 commit comments

Comments
 (0)