Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Fix shift tab when currentIndex is 0 or -1 #49

Merged
merged 5 commits into from
Nov 14, 2019
Merged

Fix shift tab when currentIndex is 0 or -1 #49

merged 5 commits into from
Nov 14, 2019

Conversation

muan
Copy link
Contributor

@muan muan commented Nov 6, 2019

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.

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) {
Copy link
Contributor Author

@muan muan Nov 6, 2019

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
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

@muan muan requested a review from a team November 6, 2019 19:26
Copy link
Contributor

@keithamus keithamus left a 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 👌

@muan muan merged commit a2b9ecc into master Nov 14, 2019
@muan muan deleted the shiftTab branch November 14, 2019 19:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants