-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tab)!: remove
focusTrap
option (#772)
BREAKING CHANGE: The `focusTrap` option has been removed from `userEvent.tab()`.
- Loading branch information
1 parent
87470ff
commit a0412c0
Showing
8 changed files
with
89 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,15 @@ | ||
import {fireEvent} from '@testing-library/dom' | ||
import {blur, focus, getActiveElement, getTabDestination} from './utils' | ||
import type {UserEvent} from './setup' | ||
|
||
export interface tabOptions { | ||
shift?: boolean | ||
focusTrap?: Document | Element | ||
} | ||
|
||
export function tab( | ||
this: UserEvent, | ||
{shift = false, focusTrap}: tabOptions = {}, | ||
) { | ||
const doc = focusTrap?.ownerDocument ?? document | ||
const previousElement = getActiveElement(doc) | ||
|
||
// Never the case in our test environment | ||
// Only happens if the activeElement is inside a shadowRoot that is not part of `doc`. | ||
/* istanbul ignore next */ | ||
if (!previousElement) { | ||
return | ||
} | ||
|
||
if (!focusTrap) { | ||
focusTrap = doc | ||
} | ||
|
||
const nextElement = getTabDestination(previousElement, shift, focusTrap) | ||
|
||
const shiftKeyInit = { | ||
key: 'Shift', | ||
keyCode: 16, | ||
shiftKey: true, | ||
} | ||
const tabKeyInit = { | ||
key: 'Tab', | ||
keyCode: 9, | ||
shiftKey: shift, | ||
} | ||
|
||
let continueToTab = true | ||
|
||
if (shift) fireEvent.keyDown(previousElement, {...shiftKeyInit}) | ||
continueToTab = fireEvent.keyDown(previousElement, {...tabKeyInit}) | ||
|
||
const keyUpTarget = continueToTab ? nextElement : previousElement | ||
|
||
if (continueToTab) { | ||
if (nextElement === doc.body) { | ||
blur(previousElement) | ||
} else { | ||
focus(nextElement) | ||
} | ||
} | ||
|
||
fireEvent.keyUp(keyUpTarget, {...tabKeyInit}) | ||
|
||
if (shift) { | ||
fireEvent.keyUp(keyUpTarget, {...shiftKeyInit, shiftKey: false}) | ||
} | ||
export function tab(this: UserEvent, {shift}: tabOptions = {}) { | ||
this.keyboard( | ||
shift === true | ||
? '{Shift>}{Tab}{/Shift}' | ||
: shift === false | ||
? '[/ShiftLeft][/ShiftRight]{Tab}' | ||
: '{Tab}', | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters