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

Commit 8bfeda0

Browse files
daviouswesleycho
authored andcommitted
feat(datepicker): implements alternative format support
- Adds support for multiple input formats Closes #4951 Closes #4952
1 parent f3d4dc2 commit 8bfeda0

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

src/datepicker/datepicker.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
539539
closeOnDateSelection: true,
540540
appendToBody: false,
541541
showButtonBar: true,
542-
onOpenFocus: true
542+
onOpenFocus: true,
543+
altInputFormats: []
543544
})
544545

545546
.controller('UibDatepickerPopupController', ['$scope', '$element', '$attrs', '$compile', '$parse', '$document', '$rootScope', '$uibPosition', 'dateFilter', 'uibDateParser', 'uibDatepickerPopupConfig', '$timeout',
@@ -549,7 +550,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
549550
isHtml5DateInput = false;
550551
var dateFormat, closeOnDateSelection, appendToBody, onOpenFocus,
551552
datepickerPopupTemplateUrl, datepickerTemplateUrl, popupEl, datepickerEl,
552-
ngModel, $popup;
553+
ngModel, $popup, altInputFormats;
553554

554555
scope.watchData = {};
555556

@@ -560,6 +561,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
560561
onOpenFocus = angular.isDefined(attrs.onOpenFocus) ? scope.$parent.$eval(attrs.onOpenFocus) : datepickerPopupConfig.onOpenFocus;
561562
datepickerPopupTemplateUrl = angular.isDefined(attrs.datepickerPopupTemplateUrl) ? attrs.datepickerPopupTemplateUrl : datepickerPopupConfig.datepickerPopupTemplateUrl;
562563
datepickerTemplateUrl = angular.isDefined(attrs.datepickerTemplateUrl) ? attrs.datepickerTemplateUrl : datepickerPopupConfig.datepickerTemplateUrl;
564+
altInputFormats = angular.isDefined(attrs.altInputFormats) ? scope.$parent.$eval(attrs.altInputFormats) : datepickerPopupConfig.altInputFormats;
563565

564566
scope.showButtonBar = angular.isDefined(attrs.showButtonBar) ? scope.$parent.$eval(attrs.showButtonBar) : datepickerPopupConfig.showButtonBar;
565567

@@ -811,6 +813,14 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
811813

812814
if (angular.isString(viewValue)) {
813815
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+
}
814824
if (isNaN(date)) {
815825
return undefined;
816826
}
@@ -842,6 +852,14 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
842852

843853
if (angular.isString(value)) {
844854
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+
}
845863
return !isNaN(date);
846864
}
847865

src/datepicker/docs/demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h4>Popup</h4>
2222
<div class="row">
2323
<div class="col-md-6">
2424
<p class="input-group">
25-
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
25+
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" />
2626
<span class="input-group-btn">
2727
<button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
2828
</span>
@@ -40,7 +40,7 @@ <h4>Popup</h4>
4040
</div>
4141
<div class="row">
4242
<div class="col-md-6">
43-
<label>Format:</label> <select class="form-control" ng-model="format" ng-options="f for f in formats"><option></option></select>
43+
<label>Format: <span class="muted-text">(manual alternate <em>{{altInputFormats[0]}}</em>)</span></label> <select class="form-control" ng-model="format" ng-options="f for f in formats"><option></option></select>
4444
</div>
4545
</div>
4646

src/datepicker/docs/demo.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($
3939

4040
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
4141
$scope.format = $scope.formats[0];
42+
$scope.altInputFormats = ['M!/d!/yyyy'];
4243

4344
$scope.popup1 = {
4445
opened: false

src/datepicker/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Options for the uib-datepicker must be passed as JSON using the `datepicker-opti
144144
_(Default: `yyyy-MM-dd`)_ -
145145
The format for displayed dates. This string can take string literals by surrounding the value with single quotes, i.e. `yyyy-MM-dd h 'o\'clock'`.
146146

147+
* `alt-input-formats`
148+
_(Default: `[]`)_:
149+
A list of alternate formats acceptable for manual entry.
150+
147151
### Keyboard support ###
148152

149153
Depending on datepicker's current mode, the date may refer either to day, month or year. Accordingly, the term view refers either to a month, year or year range.

src/datepicker/test/datepicker.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,51 @@ describe('datepicker', function() {
23392339
});
23402340
});
23412341

2342+
describe('altInputFormats', function() {
2343+
describe('datepickerPopupConfig.altInputFormats', function() {
2344+
var originalConfig = {};
2345+
beforeEach(inject(function(uibDatepickerPopupConfig) {
2346+
angular.extend(originalConfig, uibDatepickerPopupConfig);
2347+
uibDatepickerPopupConfig.datepickerPopup = 'MM-dd-yyyy';
2348+
uibDatepickerPopupConfig.altInputFormats = ['M!/d!/yyyy'];
2349+
2350+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup is-open="true"></div>')($rootScope);
2351+
$rootScope.$digest();
2352+
assignElements(wrapElement);
2353+
}));
2354+
2355+
afterEach(inject(function(uibDatepickerPopupConfig) {
2356+
// return it to the original state
2357+
angular.extend(uibDatepickerPopupConfig, originalConfig);
2358+
}));
2359+
2360+
it('changes date format', function() {
2361+
changeInputValueTo(inputEl, '11/8/1980');
2362+
2363+
expect($rootScope.date.getFullYear()).toEqual(1980);
2364+
expect($rootScope.date.getMonth()).toEqual(10);
2365+
expect($rootScope.date.getDate()).toEqual(8);
2366+
});
2367+
});
2368+
2369+
describe('attribute `alt-input-formats`', function() {
2370+
beforeEach(function() {
2371+
$rootScope.date = new Date('November 9, 1980');
2372+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup="MMMM d yyyy" alt-input-formats="[\'M!/d!/yyyy\']" is-open="true"></div>')($rootScope);
2373+
$rootScope.$digest();
2374+
assignElements(wrapElement);
2375+
});
2376+
2377+
it('should accept alternate input formats', function() {
2378+
changeInputValueTo(inputEl, '11/8/1980');
2379+
2380+
expect($rootScope.date.getFullYear()).toEqual(1980);
2381+
expect($rootScope.date.getMonth()).toEqual(10);
2382+
expect($rootScope.date.getDate()).toEqual(8);
2383+
});
2384+
});
2385+
});
2386+
23422387
describe('pass through attributes', function() {
23432388
var wrapElement;
23442389
describe('formatting', function() {

0 commit comments

Comments
 (0)