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

feat(datepicker): add onOpenFocus support #4146

Closed
wants to merge 1 commit into from
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
10 changes: 7 additions & 3 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
closeText: 'Done',
closeOnDateSelection: true,
appendToBody: false,
showButtonBar: true
showButtonBar: true,
onOpenFocus: true
})

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

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

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

$timeout(function() {
scope.$broadcast('datepicker.focus');
if (onOpenFocus) {
scope.$broadcast('datepicker.focus');
}
$document.bind('click', documentClickBind);
}, 0, false);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/datepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ Specific settings for the `datepicker-popup`, that can globally configured throu
* `datepicker-append-to-body`
_(Default: false)_:
Append the datepicker popup element to `body`, rather than inserting after `datepicker-popup`. For global configuration, use `datepickerPopupConfig.appendToBody`.

* `is-open` <i class="glyphicon glyphicon-eye-open"></i>
_(Default: false)_:
Whether to show the datepicker.

* `on-open-focus`
_(Default: true)_:
Whether to focus the datepicker popup upon opening.

### Keyboard Support ###

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.
Expand Down
25 changes: 25 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,31 @@ describe('datepicker directive', function () {
expect(getTitle()).toBe('November 1980');
});
});

describe('attribute `onOpenFocus`', function() {
beforeEach(function() {
$rootScope.date = null;
$rootScope.isopen = false;
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup on-open-focus="false" is-open="isopen"></div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
});

it('should remain focused on the input', function() {
var focused = true;
expect(dropdownEl.length).toBe(0);

inputEl[0].focus();
inputEl.on('blur', function() {
focused = false;
});
$rootScope.isopen = true;
$rootScope.$digest();

expect(inputEl.parent().find('.dropdown-menu').length).toBe(1);
expect(focused).toBe(true);
});
});
});

describe('with empty initial state', function () {
Expand Down