Skip to content

Commit

Permalink
fix: tab focus on disabled elements false positive (#202)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Monro <ben.monro@gmail.com>
  • Loading branch information
2 people authored and Kent C. Dodds committed Jan 28, 2020
1 parent 4a01b32 commit 375102e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions __tests__/react/tab.js
Expand Up @@ -245,3 +245,23 @@ describe("userEvent.tab", () => {
});
});
});

it("should not focus disabled elements", () => {
const { getByTestId } = render(
<div>
<input data-testid="one" />
<input tabIndex={-1} />
<button disabled>click</button>
<input disabled />
<input data-testid="five" />
</div>
);

const [one, five] = [getByTestId("one"), getByTestId("five")];

userEvent.tab();
expect(one).toHaveFocus();

userEvent.tab();
expect(five).toHaveFocus();
});
3 changes: 2 additions & 1 deletion src/index.js
Expand Up @@ -241,8 +241,9 @@ const userEvent = {
const focusableElements = focusTrap.querySelectorAll(
"input, button, select, textarea, a[href], [tabindex]"
);

let list = Array.prototype.filter.call(focusableElements, function (item) {
return item.getAttribute("tabindex") !== "-1";
return item.getAttribute("tabindex") !== "-1"&& !item.disabled;
}).map((el, idx) => ({ el, idx }))
.sort((a, b) => {
const tabIndexA = a.el.getAttribute("tabindex");
Expand Down

0 comments on commit 375102e

Please sign in to comment.