Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue with tabbing when a radio is the last element of the modal #1045

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
20 changes: 20 additions & 0 deletions specs/Modal.events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ export default () => {
});
});

it("traps tab in the modal on tab with radio button as last element", () => {
const topButton = <button>top</button>;
const radio1 = <input id="radio-a" name="radios" type="radio" />;
const radio2 = <input id="radio-b" name="radios" type="radio" />;
const modalContent = (
<div>
{topButton}
{radio1}
{radio2}
</div>
);
const props = { isOpen: true };
withModal(props, modalContent, modal => {
const content = mcontent(modal);
tabKeyDown(content);
tabKeyDown(content);
document.activeElement.textContent.should.be.eql("top");
});
});

describe("shouldCloseOnEsc", () => {
context("when true", () => {
it("should close on Esc key event", () => {
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/scopeTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function getActiveElement(el = document) {
: el.activeElement;
}

function areFromSameRadioGroup(a, b) {
return a.name === b.name && a.type === "radio" && b.type === "radio";
}

export default function scopeTab(node, event) {
const tabbable = findTabbable(node);

Expand All @@ -29,7 +33,10 @@ export default function scopeTab(node, event) {
target = tail;
}

if (tail === activeElement && !shiftKey) {
if (
(tail === activeElement || areFromSameRadioGroup(activeElement, tail)) &&
!shiftKey
) {
target = head;
}

Expand Down