Skip to content

Commit

Permalink
Merge pull request reactjs#141 from everdimension/fix-no-tabbable-focus
Browse files Browse the repository at this point in the history
Fix no tabbable focus
  • Loading branch information
claydiffrient committed Mar 26, 2016
2 parents e8749dd + c844719 commit b5e38cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/helpers/scopeTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var findTabbable = require('../helpers/tabbable');

module.exports = function(node, event) {
var tabbable = findTabbable(node);
if (!tabbable.length) return;
if (!tabbable.length) {
event.preventDefault();
return;
}
var finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
var leavingFinalTabbable = (
finalTabbable === document.activeElement ||
Expand Down
13 changes: 13 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ describe('Modal', function () {
unmountModal();
});

it('keeps focus inside the modal when child has no tabbable elements', function() {
var tabPrevented = false;
var modal = renderModal({isOpen: true}, 'hello');
strictEqual(document.activeElement, modal.portal.refs.content);
Simulate.keyDown(modal.portal.refs.content, {
key: "Tab",
keyCode: 9,
which: 9,
preventDefault: function() { tabPrevented = true; }
});
equal(tabPrevented, true);
});

it('supports custom className', function() {
var modal = renderModal({isOpen: true, className: 'myClass'});
notEqual(modal.portal.refs.content.className.indexOf('myClass'), -1);
Expand Down

0 comments on commit b5e38cf

Please sign in to comment.