1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00

Dropdown: merge instance identification in dataApiKeydownHandler

As we use the `dataApiKeydownHandler` only for events that are triggered on `[data-bs-toggle="dropdown"]` or on `.dropdown-menu`, we can ensure that their `parentNode` will ALWAYS be the `.dropdown` wrapper
This commit is contained in:
GeoSot 2022-01-30 23:50:22 +02:00 committed by XhmikosR
parent 22bbff0b03
commit d105439235
2 changed files with 21 additions and 1 deletions

View file

@ -414,7 +414,7 @@ class Dropdown extends BaseComponent {
return
}
const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
const getToggleButton = SelectorEngine.findOne(SELECTOR_DATA_TOGGLE, event.delegateTarget.parentNode)
const instance = Dropdown.getOrCreateInstance(getToggleButton)
if (isEscapeEvent) {

View file

@ -2023,6 +2023,26 @@ describe('Dropdown', () => {
dropdownToggle.click()
})
})
it('should be able to identify clicked dropdown, no matter the markup order', () => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdown item</a>',
' </div>',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown toggle</button>',
'</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const spy = spyOn(Dropdown, 'getOrCreateInstance').and.callThrough()
dropdownToggle.click()
expect(spy).toHaveBeenCalledWith(dropdownToggle)
dropdownMenu.click()
expect(spy).toHaveBeenCalledWith(dropdownToggle)
})
})
describe('jQueryInterface', () => {