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

Commit c0f1027

Browse files
wawyedwesleycho
authored andcommitted
fix(modal): trap tabbing regardless of config
- Ensure tabbing is trapped regardless of whether `keyboard` is `false` Closes #5010 Fixes #4990
1 parent 384fdfc commit c0f1027

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/modal/modal.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,15 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
351351
}
352352

353353
var modal = openedWindows.top();
354-
if (modal && modal.value.keyboard) {
354+
if (modal) {
355355
switch (evt.which) {
356356
case 27: {
357-
evt.preventDefault();
358-
$rootScope.$apply(function() {
359-
$modalStack.dismiss(modal.key, 'escape key press');
360-
});
357+
if (modal.value.keyboard) {
358+
evt.preventDefault();
359+
$rootScope.$apply(function() {
360+
$modalStack.dismiss(modal.key, 'escape key press');
361+
});
362+
}
361363
break;
362364
}
363365
case 9: {

src/modal/test/modal.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,46 @@ describe('$uibModal', function () {
527527

528528
initialPage.remove();
529529
});
530+
531+
it('should change focus to first element when tab key is pressed when keyboard is false', function() {
532+
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
533+
angular.element(document.body).append(initialPage);
534+
initialPage.focus();
535+
536+
open({
537+
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
538+
'<button id="tab-focus-button">Open me!</button>',
539+
keyboard: false
540+
});
541+
expect($document).toHaveModalsOpen(1);
542+
543+
var lastElement = angular.element(document.getElementById('tab-focus-button'));
544+
lastElement.focus();
545+
triggerKeyDown(lastElement, 9);
546+
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-link');
547+
548+
initialPage.remove();
549+
});
550+
551+
it('should change focus to last element when shift+tab keys are pressed when keyboard is false', function() {
552+
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
553+
angular.element(document.body).append(initialPage);
554+
initialPage.focus();
555+
556+
open({
557+
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
558+
'<button id="tab-focus-button">Open me!</button>',
559+
keyboard: false
560+
});
561+
expect($document).toHaveModalsOpen(1);
562+
563+
var lastElement = angular.element(document.getElementById('tab-focus-link'));
564+
lastElement.focus();
565+
triggerKeyDown(lastElement, 9, true);
566+
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button');
567+
568+
initialPage.remove();
569+
});
530570
});
531571

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

0 commit comments

Comments
 (0)