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

Commit

Permalink
feat(datepicker): watch for changes when falsy
Browse files Browse the repository at this point in the history
- When initial value is falsy, continue to watch for changes to update

Closes #5672
Closes #5677
  • Loading branch information
wesleycho committed Mar 22, 2016
1 parent 8590ea7 commit 45165ba
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,21 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
break;
case 'maxDate':
case 'minDate':
if ($scope.datepickerOptions[key]) {
$scope.$watch('datepickerOptions.' + key, function(value) {
if (value) {
if (angular.isDate(value)) {
self[key] = dateParser.fromTimezone(new Date(value), ngModelOptions.timezone);
} else {
self[key] = new Date(dateFilter(value, 'medium'));
}
$scope.$watch('datepickerOptions.' + key, function(value) {
if (value) {
if (angular.isDate(value)) {
self[key] = dateParser.fromTimezone(new Date(value), ngModelOptions.timezone);
} else {
self[key] = null;
self[key] = new Date(dateFilter(value, 'medium'));
}
} else {
self[key] = datepickerConfig[key] ?
dateParser.fromTimezone(new Date(datepickerConfig[key]), ngModelOptions.timezone) :
null;
}

self.refreshView();
});
} else {
self[key] = datepickerConfig[key] ? dateParser.fromTimezone(new Date(datepickerConfig[key]), ngModelOptions.timezone) : null;
}
self.refreshView();
});

break;
case 'maxMode':
Expand Down
48 changes: 48 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,30 @@ describe('datepicker', function() {
});
});

describe('minDate with no initial value', function() {
beforeEach(function() {
$rootScope.options = {};
$rootScope.date = new Date('September 10, 2010');
element = $compile('<uib-datepicker ng-model="date" datepicker-options="options"></uib-datepicker>')($rootScope);
$rootScope.$digest();
});

it('should toggle appropriately', function() {
var buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(false);
});

$rootScope.options.minDate = new Date('September 12, 2010');
$rootScope.$digest();

refreshedButtons = getAllOptionsEl();
angular.forEach(refreshedButtons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index < 14);
});
});
});

describe('minDate', function() {
beforeEach(function() {
$rootScope.options = {
Expand Down Expand Up @@ -1019,6 +1043,30 @@ describe('datepicker', function() {
});
});

describe('maxDate with no initial value', function() {
beforeEach(function() {
$rootScope.options = {};
$rootScope.date = new Date('September 10, 2010');
element = $compile('<uib-datepicker ng-model="date" datepicker-options="options"></uib-datepicker>')($rootScope);
$rootScope.$digest();
});

it('should toggle appropriately', function() {
var buttons = getAllOptionsEl();
angular.forEach(buttons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(false);
});

$rootScope.options.maxDate = new Date('September 25, 2010');
$rootScope.$digest();

refreshedButtons = getAllOptionsEl();
angular.forEach(refreshedButtons, function(button, index) {
expect(angular.element(button).prop('disabled')).toBe(index > 27);
});
});
});

describe('maxDate', function() {
beforeEach(function() {
$rootScope.options = {
Expand Down

0 comments on commit 45165ba

Please sign in to comment.