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

Commit 71e0b8a

Browse files
committed
feat(datepicker): disable today button if invalid
- Disable today button if current date is before `min-date` or after `max-date` Closes #4199 Resolves #3988
1 parent 8e84567 commit 71e0b8a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/datepicker/datepicker.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,28 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
513513
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody,
514514
onOpenFocus = angular.isDefined(attrs.onOpenFocus) ? scope.$parent.$eval(attrs.onOpenFocus) : datepickerPopupConfig.onOpenFocus,
515515
datepickerPopupTemplateUrl = angular.isDefined(attrs.datepickerPopupTemplateUrl) ? attrs.datepickerPopupTemplateUrl : datepickerPopupConfig.datepickerPopupTemplateUrl,
516-
datepickerTemplateUrl = angular.isDefined(attrs.datepickerTemplateUrl) ? attrs.datepickerTemplateUrl : datepickerPopupConfig.datepickerTemplateUrl;
516+
datepickerTemplateUrl = angular.isDefined(attrs.datepickerTemplateUrl) ? attrs.datepickerTemplateUrl : datepickerPopupConfig.datepickerTemplateUrl,
517+
cache = {};
517518

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

520521
scope.getText = function(key) {
521522
return scope[key + 'Text'] || datepickerPopupConfig[key + 'Text'];
522523
};
523524

525+
scope.isDisabled = function(date) {
526+
if (date === 'today') {
527+
date = new Date();
528+
}
529+
530+
return ((scope.watchData.minDate && scope.compare(date, cache.minDate) < 0) ||
531+
(scope.watchData.maxDate && scope.compare(date, cache.maxDate) > 0));
532+
};
533+
534+
scope.compare = function(date1, date2) {
535+
return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()));
536+
};
537+
524538
var isHtml5DateInput = false;
525539
if (datepickerPopupConfig.html5Types[attrs.type]) {
526540
dateFormat = datepickerPopupConfig.html5Types[attrs.type];
@@ -591,6 +605,9 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
591605
var getAttribute = $parse(attrs[key]);
592606
scope.$parent.$watch(getAttribute, function(value) {
593607
scope.watchData[key] = value;
608+
if (key === 'minDate' || key === 'maxDate') {
609+
cache[key] = new Date(value);
610+
}
594611
});
595612
datepickerEl.attr(cameltoDash(key), 'watchData.' + key);
596613

src/datepicker/test/datepicker.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,26 @@ describe('datepicker directive', function() {
19061906
expect(buttons.eq(2).text()).toBe('CloseME');
19071907
});
19081908

1909+
it('should disable today button if before min date', function() {
1910+
$rootScope.minDate = new Date().setDate(new Date().getDate() + 1);
1911+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup min-date="minDate" is-open="true"><div>')($rootScope);
1912+
$rootScope.$digest();
1913+
assignElements(wrapElement);
1914+
assignButtonBar();
1915+
1916+
expect(buttons.eq(0).prop('disabled')).toBe(true);
1917+
});
1918+
1919+
it('should disable today button if after max date', function() {
1920+
$rootScope.maxDate = new Date().setDate(new Date().getDate() - 2);
1921+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup max-date="maxDate" is-open="true"><div>')($rootScope);
1922+
$rootScope.$digest();
1923+
assignElements(wrapElement);
1924+
assignButtonBar();
1925+
1926+
expect(buttons.eq(0).prop('disabled')).toBe(true);
1927+
});
1928+
19091929
it('should remove bar', function() {
19101930
$rootScope.showBar = false;
19111931
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup show-button-bar="showBar" is-open="true"><div>')($rootScope);

template/datepicker/popup.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<li ng-transclude></li>
33
<li ng-if="showButtonBar" style="padding:10px 9px 2px">
44
<span class="btn-group pull-left">
5-
<button type="button" class="btn btn-sm btn-info" ng-click="select('today')">{{ getText('current') }}</button>
5+
<button type="button" class="btn btn-sm btn-info" ng-click="select('today')" ng-disabled="isDisabled('today')">{{ getText('current') }}</button>
66
<button type="button" class="btn btn-sm btn-danger" ng-click="select(null)">{{ getText('clear') }}</button>
77
</span>
88
<button type="button" class="btn btn-sm btn-success pull-right" ng-click="close()">{{ getText('close') }}</button>

0 commit comments

Comments
 (0)