Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropdown escape propagation #33479

Merged
merged 5 commits into from Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 35 additions & 28 deletions js/src/dropdown.js
Expand Up @@ -443,6 +443,31 @@ class Dropdown extends BaseComponent {
}
}

static selectMenuItem(parent, event) {
const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible)

if (!items.length) {
return
}

let index = items.indexOf(event.target)

// Up
if (event.key === ARROW_UP_KEY && index > 0) {
index--
}

// Down
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
index++
}

// index is -1 if the first keydown is an ArrowUp
index = index === -1 ? 0 : index

items[index].focus()
}

static getParentFromElement(element) {
return getElementFromSelector(element) || element.parentNode
}
Expand All @@ -463,26 +488,29 @@ class Dropdown extends BaseComponent {
return
}

const isActive = this.classList.contains(CLASS_NAME_SHOW)

if (!isActive && event.key === ESCAPE_KEY) {
return
}

event.preventDefault()
event.stopPropagation()

if (isDisabled(this)) {
return
}

const parent = Dropdown.getParentFromElement(this)
const isActive = this.classList.contains(CLASS_NAME_SHOW)
const getToggleButton = () => this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]

if (event.key === ESCAPE_KEY) {
const button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
button.focus()
getToggleButton().focus()
Dropdown.clearMenus()
return
}

if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
const button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
button.click()
getToggleButton().click()
return
}

Expand All @@ -491,28 +519,7 @@ class Dropdown extends BaseComponent {
return
}

const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible)

if (!items.length) {
return
}

let index = items.indexOf(event.target)

// Up
if (event.key === ARROW_UP_KEY && index > 0) {
index--
}

// Down
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
index++
}

// index is -1 if the first keydown is an ArrowUp
index = index === -1 ? 0 : index

items[index].focus()
Dropdown.selectMenuItem(Dropdown.getParentFromElement(this), event)
}
}

Expand Down
33 changes: 33 additions & 0 deletions js/tests/unit/dropdown.spec.js
Expand Up @@ -1671,6 +1671,39 @@ describe('Dropdown', () => {
done()
}, 20)
})

it('should propagate escape key events if dropdown is closed', done => {
fixtureEl.innerHTML = [
'<div class="parent">',
' <div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Some Item</a>',
' </div>',
' </div>',
'</div>'
]

const parent = fixtureEl.querySelector('.parent')
const toggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')

const parentKeyHandler = jasmine.createSpy('parentKeyHandler')

parent.addEventListener('keydown', parentKeyHandler)
parent.addEventListener('keyup', () => {
expect(parentKeyHandler).toHaveBeenCalled()
done()
})

const keydownEscape = createEvent('keydown', { bubbles: true })
keydownEscape.key = 'Escape'
const keyupEscape = createEvent('keyup', { bubbles: true })
keyupEscape.key = 'Escape'

toggle.focus()
toggle.dispatchEvent(keydownEscape)
toggle.dispatchEvent(keyupEscape)
})
})

describe('jQueryInterface', () => {
Expand Down