Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit fd88dcb

Browse files
daviouswesleycho
authored andcommitted
fix(datepicker): update with alternative format
Closes #5014
1 parent 62e0761 commit fd88dcb

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/datepicker/datepicker.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
679679

680680
// Detect changes in the view from the text box
681681
ngModel.$viewChangeListeners.push(function() {
682-
scope.date = dateParser.parse(ngModel.$viewValue, dateFormat, scope.date);
682+
scope.date = parseDateString(ngModel.$viewValue);
683683
});
684684

685685
element.bind('keydown', inputKeydownBind);
@@ -797,6 +797,19 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
797797
return string.replace(/([A-Z])/g, function($1) { return '-' + $1.toLowerCase(); });
798798
}
799799

800+
function parseDateString(viewValue) {
801+
var date = dateParser.parse(viewValue, dateFormat, scope.date);
802+
if (isNaN(date)) {
803+
for (var i = 0; i < altInputFormats.length; i++) {
804+
date = dateParser.parse(viewValue, altInputFormats[i], scope.date);
805+
if (!isNaN(date)) {
806+
return date;
807+
}
808+
}
809+
}
810+
return date;
811+
}
812+
800813
function parseDate(viewValue) {
801814
if (angular.isNumber(viewValue)) {
802815
// presumably timestamp to date object
@@ -812,15 +825,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
812825
}
813826

814827
if (angular.isString(viewValue)) {
815-
var date = dateParser.parse(viewValue, dateFormat, scope.date);
816-
if (isNaN(date)) {
817-
for (var i = 0; i < altInputFormats.length; i++) {
818-
date = dateParser.parse(viewValue, altInputFormats[i], scope.date);
819-
if (!isNaN(date)) {
820-
break;
821-
}
822-
}
823-
}
828+
var date = parseDateString(viewValue);
824829
if (isNaN(date)) {
825830
return undefined;
826831
}
@@ -851,16 +856,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
851856
}
852857

853858
if (angular.isString(value)) {
854-
var date = dateParser.parse(value, dateFormat);
855-
if (isNaN(date)) {
856-
for (var i = 0; i < altInputFormats.length; i++) {
857-
date = dateParser.parse(value, altInputFormats[i]);
858-
if (!isNaN(date)) {
859-
break;
860-
}
861-
}
862-
}
863-
return !isNaN(date);
859+
return !isNaN(parseDateString(viewValue));
864860
}
865861

866862
return false;

src/datepicker/test/datepicker.spec.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2343,10 +2343,10 @@ describe('datepicker', function() {
23432343
describe('datepickerPopupConfig.altInputFormats', function() {
23442344
var originalConfig = {};
23452345
beforeEach(inject(function(uibDatepickerPopupConfig) {
2346+
$rootScope.date = new Date('November 9, 1980');
23462347
angular.extend(originalConfig, uibDatepickerPopupConfig);
23472348
uibDatepickerPopupConfig.datepickerPopup = 'MM-dd-yyyy';
23482349
uibDatepickerPopupConfig.altInputFormats = ['M!/d!/yyyy'];
2349-
23502350
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup is-open="true"></div>')($rootScope);
23512351
$rootScope.$digest();
23522352
assignElements(wrapElement);
@@ -2364,6 +2364,12 @@ describe('datepicker', function() {
23642364
expect($rootScope.date.getMonth()).toEqual(10);
23652365
expect($rootScope.date.getDate()).toEqual(8);
23662366
});
2367+
2368+
it('changes the datepicker', function() {
2369+
expect(selectedElementIndex()).toEqual(14);
2370+
changeInputValueTo(inputEl, '11/8/1980');
2371+
expect(selectedElementIndex()).toEqual(13);
2372+
});
23672373
});
23682374

23692375
describe('attribute `alt-input-formats`', function() {
@@ -2381,6 +2387,12 @@ describe('datepicker', function() {
23812387
expect($rootScope.date.getMonth()).toEqual(10);
23822388
expect($rootScope.date.getDate()).toEqual(8);
23832389
});
2390+
2391+
it('changes the datepicker', function() {
2392+
expect(selectedElementIndex()).toEqual(14);
2393+
changeInputValueTo(inputEl, '11/8/1980');
2394+
expect(selectedElementIndex()).toEqual(13);
2395+
});
23842396
});
23852397
});
23862398

0 commit comments

Comments
 (0)