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

Commit e7f709f

Browse files
daviouswesleycho
authored andcommitted
fix(datepicker): fix minDate/maxDate with literals
- Fixes use of min-date and max-date with literal strings Closes #4841 Fixes #3437
1 parent 812d47e commit e7f709f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/datepicker/datepicker.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
4242
angular.forEach(['minDate', 'maxDate'], function(key) {
4343
if ($attrs[key]) {
4444
$scope.$parent.$watch($attrs[key], function(value) {
45-
self[key] = value ? new Date(value) : null;
45+
self[key] = value ? angular.isDate(value) ? new Date(value) : new Date(dateFilter(value, 'medium')) : null;
4646
self.refreshView();
4747
});
4848
} else {
@@ -631,10 +631,10 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
631631
if (attrs[key]) {
632632
var getAttribute = $parse(attrs[key]);
633633
scope.$parent.$watch(getAttribute, function(value) {
634-
scope.watchData[key] = value;
635634
if (key === 'minDate' || key === 'maxDate') {
636-
cache[key] = new Date(value);
635+
cache[key] = angular.isDate(value) ? new Date(value) : new Date(dateFilter(value, 'medium'));
637636
}
637+
scope.watchData[key] = cache[key] || value;
638638
});
639639
datepickerEl.attr(cameltoDash(key), 'watchData.' + key);
640640

src/datepicker/test/datepicker.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,20 @@ describe('datepicker', function() {
965965

966966
});
967967

968+
describe('`min-date` attribute', function () {
969+
beforeEach(function() {
970+
element = $compile('<uib-datepicker ng-model="date" min-date="\'2010-09-05\'"></uib-datepicker>')($rootScope);
971+
$rootScope.$digest();
972+
});
973+
974+
it('accepts literals, \'yyyy-MM-dd\' case', function() {
975+
var buttons = getAllOptionsEl();
976+
angular.forEach(buttons, function(button, index) {
977+
expect(angular.element(button).prop('disabled')).toBe(index < 7);
978+
});
979+
});
980+
});
981+
968982
describe('`max-date` attribute', function() {
969983
beforeEach(function() {
970984
$rootScope.maxdate = new Date('September 25, 2010');
@@ -2011,6 +2025,17 @@ describe('datepicker', function() {
20112025
expect(buttons.eq(0).prop('disabled')).toBe(true);
20122026
});
20132027

2028+
it('should disable today button if before min date, yyyy-MM-dd case', inject(function(dateFilter) {
2029+
var minDate = new Date(new Date().setDate(new Date().getDate() + 1));
2030+
var literalMinDate = dateFilter(minDate, 'yyyy-MM-dd');
2031+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup="yyyy-MM-dd" min-date="\'' + literalMinDate + '\'" is-open="true"><div>')($rootScope);
2032+
$rootScope.$digest();
2033+
assignElements(wrapElement);
2034+
assignButtonBar();
2035+
2036+
expect(buttons.eq(0).prop('disabled')).toBe(true);
2037+
}));
2038+
20142039
it('should disable today button if after max date', function() {
20152040
$rootScope.maxDate = new Date().setDate(new Date().getDate() - 2);
20162041
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup max-date="maxDate" is-open="true"><div>')($rootScope);

0 commit comments

Comments
 (0)