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

Commit

Permalink
fix(datepicker): update with alternative format
Browse files Browse the repository at this point in the history
Closes #5014
  • Loading branch information
davious authored and wesleycho committed Dec 5, 2015
1 parent 62e0761 commit fd88dcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
36 changes: 16 additions & 20 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi

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

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

function parseDateString(viewValue) {
var date = dateParser.parse(viewValue, dateFormat, scope.date);
if (isNaN(date)) {
for (var i = 0; i < altInputFormats.length; i++) {
date = dateParser.parse(viewValue, altInputFormats[i], scope.date);
if (!isNaN(date)) {
return date;
}
}
}
return date;
}

function parseDate(viewValue) {
if (angular.isNumber(viewValue)) {
// presumably timestamp to date object
Expand All @@ -812,15 +825,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
}

if (angular.isString(viewValue)) {
var date = dateParser.parse(viewValue, dateFormat, scope.date);
if (isNaN(date)) {
for (var i = 0; i < altInputFormats.length; i++) {
date = dateParser.parse(viewValue, altInputFormats[i], scope.date);
if (!isNaN(date)) {
break;
}
}
}
var date = parseDateString(viewValue);
if (isNaN(date)) {
return undefined;
}
Expand Down Expand Up @@ -851,16 +856,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
}

if (angular.isString(value)) {
var date = dateParser.parse(value, dateFormat);
if (isNaN(date)) {
for (var i = 0; i < altInputFormats.length; i++) {
date = dateParser.parse(value, altInputFormats[i]);
if (!isNaN(date)) {
break;
}
}
}
return !isNaN(date);
return !isNaN(parseDateString(viewValue));
}

return false;
Expand Down
14 changes: 13 additions & 1 deletion src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2343,10 +2343,10 @@ describe('datepicker', function() {
describe('datepickerPopupConfig.altInputFormats', function() {
var originalConfig = {};
beforeEach(inject(function(uibDatepickerPopupConfig) {
$rootScope.date = new Date('November 9, 1980');
angular.extend(originalConfig, uibDatepickerPopupConfig);
uibDatepickerPopupConfig.datepickerPopup = 'MM-dd-yyyy';
uibDatepickerPopupConfig.altInputFormats = ['M!/d!/yyyy'];

var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup is-open="true"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
Expand All @@ -2364,6 +2364,12 @@ describe('datepicker', function() {
expect($rootScope.date.getMonth()).toEqual(10);
expect($rootScope.date.getDate()).toEqual(8);
});

it('changes the datepicker', function() {
expect(selectedElementIndex()).toEqual(14);
changeInputValueTo(inputEl, '11/8/1980');
expect(selectedElementIndex()).toEqual(13);
});
});

describe('attribute `alt-input-formats`', function() {
Expand All @@ -2381,6 +2387,12 @@ describe('datepicker', function() {
expect($rootScope.date.getMonth()).toEqual(10);
expect($rootScope.date.getDate()).toEqual(8);
});

it('changes the datepicker', function() {
expect(selectedElementIndex()).toEqual(14);
changeInputValueTo(inputEl, '11/8/1980');
expect(selectedElementIndex()).toEqual(13);
});
});
});

Expand Down

0 comments on commit fd88dcb

Please sign in to comment.