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

Commit e7de21d

Browse files
crisbetommalerba
authored andcommitted
feat(datepicker): allow date strings as the source for ng-model (#9554)
This change adds the ability to have a date string (e.g. "2016-01-02") as the source for the datepicker. Until now only Date objects were allowed. Fixes #6253. Fixes #9535.
1 parent 0276d83 commit e7de21d

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/components/datepicker/js/datepickerDirective.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @name mdDatepicker
1919
* @module material.components.datepicker
2020
*
21-
* @param {Date} ng-model The component's model. Expects a JavaScript Date object.
21+
* @param {Date} ng-model The component's model. Expects either a JavaScript Date object or a value that can be parsed into one (e.g. a ISO 8601 string).
2222
* @param {Object=} ng-model-options Allows tuning of the way in which `ng-model` is being updated. Also allows
2323
* for a timezone to be specified. <a href="https://docs.angularjs.org/api/ng/directive/ngModelOptions#usage">Read more at the ngModelOptions docs.</a>
2424
* @param {expression=} ng-change Expression evaluated when the model value changes.
@@ -444,9 +444,18 @@
444444

445445
// Responds to external changes to the model value.
446446
self.ngModelCtrl.$formatters.push(function(value) {
447+
var parsedValue = angular.isDefined(value) ? Date.parse(value) : null;
448+
449+
// `parsedValue` is the time since epoch if valid or `NaN` if invalid.
450+
if (!isNaN(parsedValue) && angular.isNumber(parsedValue)) {
451+
value = new Date(parsedValue);
452+
}
453+
447454
if (value && !(value instanceof Date)) {
448-
throw Error('The ng-model for md-datepicker must be a Date instance. ' +
449-
'Currently the model is a: ' + (typeof value));
455+
throw Error(
456+
'The ng-model for md-datepicker must be a Date instance or a value ' +
457+
'that can be parsed into a date. Currently the model is of type: ' + typeof value
458+
);
450459
}
451460

452461
self.onExternalChange(value);

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,29 @@ describe('md-datepicker', function() {
105105
expect(controller.inputElement.placeholder).toBe('Fancy new placeholder');
106106
});
107107

108-
it('should throw an error when the model is not a date', function() {
108+
it('should forward the aria-label to the generated input', function() {
109+
createDatepickerInstance('<md-datepicker ng-model="myDate" aria-label="Enter a date"></md-datepicker>');
110+
expect(controller.ngInputElement.attr('aria-label')).toBe('Enter a date');
111+
});
112+
113+
it('should throw an error when the model cannot be parsed into a date', function() {
109114
expect(function() {
110-
pageScope.myDate = '2015-01-01';
115+
pageScope.myDate = 'Frodo Baggins';
111116
pageScope.$apply();
112-
}).toThrowError('The ng-model for md-datepicker must be a Date instance. ' +
113-
'Currently the model is a: string');
117+
}).toThrowError('The ng-model for md-datepicker must be a Date instance or a value ' +
118+
'that can be parsed into a date. Currently the model is of type: string');
114119
});
115120

116-
it('should support null and undefined values', function() {
121+
it('should support null, undefined and values that can be parsed into a date', function() {
117122
expect(function() {
118123
pageScope.myDate = null;
119124
pageScope.$apply();
120125

121126
pageScope.myDate = undefined;
122127
pageScope.$apply();
123128

129+
pageScope.myDate = '2016-09-08';
130+
pageScope.$apply();
124131
}).not.toThrow();
125132
});
126133

0 commit comments

Comments
 (0)