-
Notifications
You must be signed in to change notification settings - Fork 44
Fix shift tab when currentIndex is 0 or -1 #49
Conversation
const currentFocus = elements.filter(el => el.matches(':focus'))[0] | ||
let targetIndex = 0 | ||
const currentFocus = dialog.contains(document.activeElement) ? document.activeElement : null | ||
let targetIndex = movement === -1 ? -1 : 0 | ||
|
||
if (currentFocus) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When currentFocus
does not exist, movement
was not apply at all.
|
||
if (currentFocus) { | ||
const currentIndex = elements.indexOf(currentFocus) | ||
if (currentIndex !== -1) { | ||
const newIndex = currentIndex + movement | ||
if (newIndex >= 0) targetIndex = newIndex % elements.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When newIndex
is -1
((currentIndex)0 + (movement)-1
), targetIndex
stays 0
.
assert(!details.open) | ||
}) | ||
|
||
it('manages focus', async function() { | ||
summary.click() | ||
await waitForToggleEvent(details) | ||
assert.equal(document.activeElement, dialog) | ||
pressTab(details) | ||
triggerKeydownEvent(details, 'Tab', true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New test for shift + tab here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good 👍. New code actually feels simpler because there are less complex conditions & calculations, plus it fixes a bug so 👌
I will comment inline to point out where the bugs come from.
Bonus: instead of
.initEvent
this updates the test to use the proper event class constructors.