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

Commit 68afc4c

Browse files
committed
feat(datepicker): add onOpenFocus support
- Add `onOpenFocus` attribute for optional disabling of focus of popup after datepicker popup is opened Closes #2303 Closes #2546 Closes #4146
1 parent 991c91d commit 68afc4c

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/datepicker/datepicker.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
487487
closeText: 'Done',
488488
closeOnDateSelection: true,
489489
appendToBody: false,
490-
showButtonBar: true
490+
showButtonBar: true,
491+
onOpenFocus: true
491492
})
492493

493494
.directive('datepickerPopup', ['$compile', '$parse', '$document', '$rootScope', '$position', 'dateFilter', 'dateParser', 'datepickerPopupConfig', '$timeout',
@@ -506,7 +507,8 @@ function ($compile, $parse, $document, $rootScope, $position, dateFilter, datePa
506507
link: function(scope, element, attrs, ngModel) {
507508
var dateFormat,
508509
closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? scope.$parent.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection,
509-
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody;
510+
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody,
511+
onOpenFocus = angular.isDefined(attrs.onOpenFocus) ? scope.$parent.$eval(attrs.onOpenFocus) : datepickerPopupConfig.onOpenFocus;
510512

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

@@ -727,7 +729,9 @@ function ($compile, $parse, $document, $rootScope, $position, dateFilter, datePa
727729
scope.position.top = scope.position.top + element.prop('offsetHeight');
728730

729731
$timeout(function() {
730-
scope.$broadcast('datepicker.focus');
732+
if (onOpenFocus) {
733+
scope.$broadcast('datepicker.focus');
734+
}
731735
$document.bind('click', documentClickBind);
732736
}, 0, false);
733737
} else {

src/datepicker/docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ Specific settings for the `datepicker-popup`, that can globally configured throu
118118
* `datepicker-append-to-body`
119119
_(Default: false)_:
120120
Append the datepicker popup element to `body`, rather than inserting after `datepicker-popup`. For global configuration, use `datepickerPopupConfig.appendToBody`.
121+
121122
* `is-open` <i class="glyphicon glyphicon-eye-open"></i>
122123
_(Default: false)_:
123124
Whether to show the datepicker.
124125

126+
* `on-open-focus`
127+
_(Default: true)_:
128+
Whether to focus the datepicker popup upon opening.
129+
125130
### Keyboard Support ###
126131

127132
Depending on datepicker's current mode, the date may reffer either to day, month or year. Accordingly, the term view reffers either to a month, year or year range.

src/datepicker/test/datepicker.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,31 @@ describe('datepicker directive', function () {
21042104
expect(getTitle()).toBe('November 1980');
21052105
});
21062106
});
2107+
2108+
describe('attribute `onOpenFocus`', function() {
2109+
beforeEach(function() {
2110+
$rootScope.date = null;
2111+
$rootScope.isopen = false;
2112+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup on-open-focus="false" is-open="isopen"></div>')($rootScope);
2113+
$rootScope.$digest();
2114+
assignElements(wrapElement);
2115+
});
2116+
2117+
it('should remain focused on the input', function() {
2118+
var focused = true;
2119+
expect(dropdownEl.length).toBe(0);
2120+
2121+
inputEl[0].focus();
2122+
inputEl.on('blur', function() {
2123+
focused = false;
2124+
});
2125+
$rootScope.isopen = true;
2126+
$rootScope.$digest();
2127+
2128+
expect(inputEl.parent().find('.dropdown-menu').length).toBe(1);
2129+
expect(focused).toBe(true);
2130+
});
2131+
});
21072132
});
21082133

21092134
describe('with empty initial state', function () {

0 commit comments

Comments
 (0)