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

feat(datepicker): watch for changes when falsy #5677

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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