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

fix(modal): keyboard=false should not stop trapping tab focus. #5010

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
12 changes: 7 additions & 5 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,15 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}

var modal = openedWindows.top();
if (modal && modal.value.keyboard) {
if (modal) {
switch (evt.which) {
case 27: {
evt.preventDefault();
$rootScope.$apply(function() {
$modalStack.dismiss(modal.key, 'escape key press');
});
if (modal.value.keyboard) {
evt.preventDefault();
$rootScope.$apply(function() {
$modalStack.dismiss(modal.key, 'escape key press');
});
}
break;
}
case 9: {
Expand Down
40 changes: 40 additions & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,46 @@ describe('$uibModal', function () {

initialPage.remove();
});

it('should change focus to first element when tab key was pressed even if keyboard=false options is provided', function() {
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
angular.element(document.body).append(initialPage);
initialPage.focus();

open({
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
'<button id="tab-focus-button">Open me!</button>',
keyboard: false
});
expect($document).toHaveModalsOpen(1);

var lastElement = angular.element(document.getElementById('tab-focus-button'));
lastElement.focus();
triggerKeyDown(lastElement, 9);
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-link');

initialPage.remove();
});

it('should change focus to last element when shift+tab key is pressed even if keyboard=false options is provided', function() {
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
angular.element(document.body).append(initialPage);
initialPage.focus();

open({
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
'<button id="tab-focus-button">Open me!</button>',
keyboard: false
});
expect($document).toHaveModalsOpen(1);

var lastElement = angular.element(document.getElementById('tab-focus-link'));
lastElement.focus();
triggerKeyDown(lastElement, 9, true);
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button');

initialPage.remove();
});
});

describe('default options can be changed in a provider', function() {
Expand Down