Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/event/behavior/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const keydownBehavior: {
Tab: (event, target, instance) => {
return () => {
const dest = getTabDestination(
target,
document.activeElement ?? target,
instance.system.keyboard.modifiers.Shift,
)
focusElement(dest)
Expand Down
45 changes: 45 additions & 0 deletions tests/event/behavior/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,51 @@ cases(
},
)

cases(
'tab from a target moved during the keyboard event',
({focus, shiftKey = false, expectedFocus, expectedSelection}) => {
const {xpathNode} = render(
`<input value="abc"/><button>1</button><input type="number" value="1e23"/><button>2</button>`,
{
focus,
},
)

const instance = setupInstance()
instance.system.keyboard.modifiers.Shift = shiftKey

document.activeElement?.addEventListener('keydown', (e) => {
xpathNode('button[1]').focus()
})
instance.dispatchUIEvent(document.activeElement as Element, 'keydown', {
key: 'Tab',
})
expect(xpathNode(expectedFocus)).toHaveFocus()
if (expectedSelection) {
expect(getUISelection(xpathNode(expectedFocus))).toEqual(
expect.objectContaining(expectedSelection),
)
}
},
{
'tab to input2': {
focus: '//body',
expectedFocus: 'input[2]',
expectedSelection: {startOffset: 0, endOffset: 4},
},
'tab to number input': {
focus: 'input[1]',
expectedFocus: 'input[2]',
expectedSelection: {startOffset: 0, endOffset: 4},
},
'tab backward to input1': {
focus: 'input[2]',
shiftKey: true,
expectedFocus: 'input[1]',
},
},
)

cases(
'walk through radio group per arrow keys',
({focus, key, expectedTarget}) => {
Expand Down