Skip to content

Commit

Permalink
fix: exclude hidden type inputs from focusable selector (#467)
Browse files Browse the repository at this point in the history
* fix(tab): exclude hidden type inputs from focusable selector

* make test leaner

Co-authored-by: Michael Mitchell <michael.mitchell@mongodb.com>
  • Loading branch information
lazytype and Michael Mitchell committed Oct 14, 2020
1 parent ce2dbf6 commit de5db36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/focus.js
Expand Up @@ -43,6 +43,15 @@ test('no events fired on a disabled focusable input', () => {
expect(element).not.toHaveFocus()
})

test('no events fired on a hidden input', () => {
const {element, getEventSnapshot} = setup(`<input type="hidden" />`)
focus(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: input[value=""]`,
)
expect(element).not.toHaveFocus()
})

test('no events fired if the element is already focused', () => {
const {element, getEventSnapshot, clearEventCalls} = setup(`<button />`)
focus(element)
Expand Down
20 changes: 19 additions & 1 deletion src/__tests__/tab.js
Expand Up @@ -221,7 +221,7 @@ test('should respect tab index order, then DOM order', () => {
expect(checkbox).toHaveFocus()
})

test('should suport a mix of elements with/without tab index', () => {
test('should support a mix of elements with/without tab index', () => {
setup(`
<div>
<input data-testid="element" tabIndex="0" type="checkbox" />
Expand Down Expand Up @@ -287,6 +287,24 @@ test('should not tab to <a> with no href', () => {
expect(link).toHaveFocus()
})

test('should not tab to <input> with type="hidden"', () => {
const {
elements: [checkbox, , text],
} = setup(`
<input tabIndex="0" type="checkbox" />
<input type="hidden" />
<input type="text" />
`)

userEvent.tab()

expect(checkbox).toHaveFocus()

userEvent.tab()

expect(text).toHaveFocus()
})

test('should stay within a focus trap', () => {
setup(`
<>
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Expand Up @@ -219,7 +219,7 @@ function setSelectionRangeIfNecessary(
}

const FOCUSABLE_SELECTOR = [
'input:not([disabled])',
'input:not([type=hidden]):not([disabled])',
'button:not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
Expand Down

0 comments on commit de5db36

Please sign in to comment.