mirror of
https://github.com/twbs/bootstrap.git
synced 2025-01-30 22:52:24 +01:00
handle click event in one place, remove undocumented click listener on element in case of not having the proper markup
This commit is contained in:
parent
d01a08547d
commit
b1dad0943f
@ -48,7 +48,6 @@ const EVENT_HIDE = `hide${EVENT_KEY}`
|
||||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
||||
const EVENT_SHOW = `show${EVENT_KEY}`
|
||||
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
||||
const EVENT_CLICK = `click${EVENT_KEY}`
|
||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`
|
||||
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
|
||||
@ -103,8 +102,6 @@ class Dropdown extends BaseComponent {
|
||||
this._config = this._getConfig(config)
|
||||
this._menu = this._getMenuElement()
|
||||
this._inNavbar = this._detectNavbar()
|
||||
|
||||
this._addEventListeners()
|
||||
}
|
||||
|
||||
// Getters
|
||||
@ -218,13 +215,6 @@ class Dropdown extends BaseComponent {
|
||||
|
||||
// Private
|
||||
|
||||
_addEventListeners() {
|
||||
EventHandler.on(this._element, EVENT_CLICK, event => {
|
||||
event.preventDefault()
|
||||
this.toggle()
|
||||
})
|
||||
}
|
||||
|
||||
_completeHide(relatedTarget) {
|
||||
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)
|
||||
if (hideEvent.defaultPrevented) {
|
||||
@ -490,7 +480,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus)
|
||||
EventHandler.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus)
|
||||
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
||||
event.preventDefault()
|
||||
Dropdown.getOrCreateInstance(this)
|
||||
Dropdown.getOrCreateInstance(this).toggle()
|
||||
})
|
||||
|
||||
/**
|
||||
|
@ -59,26 +59,6 @@ describe('Dropdown', () => {
|
||||
expect(dropdownByElement._element).toEqual(btnDropdown)
|
||||
})
|
||||
|
||||
it('should add a listener on trigger which do not have data-bs-toggle="dropdown"', () => {
|
||||
fixtureEl.innerHTML = [
|
||||
'<div class="dropdown">',
|
||||
' <button class="btn">Dropdown</button>',
|
||||
' <div class="dropdown-menu">',
|
||||
' <a class="dropdown-item" href="#">Secondary link</a>',
|
||||
' </div>',
|
||||
'</div>'
|
||||
].join('')
|
||||
|
||||
const btnDropdown = fixtureEl.querySelector('.btn')
|
||||
const dropdown = new Dropdown(btnDropdown)
|
||||
|
||||
spyOn(dropdown, 'toggle')
|
||||
|
||||
btnDropdown.click()
|
||||
|
||||
expect(dropdown.toggle).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should create offset modifier correctly when offset option is a function', done => {
|
||||
fixtureEl.innerHTML = [
|
||||
'<div class="dropdown">',
|
||||
@ -943,21 +923,19 @@ describe('Dropdown', () => {
|
||||
].join('')
|
||||
|
||||
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
|
||||
spyOn(btnDropdown, 'addEventListener').and.callThrough()
|
||||
spyOn(btnDropdown, 'removeEventListener').and.callThrough()
|
||||
|
||||
const dropdown = new Dropdown(btnDropdown)
|
||||
|
||||
expect(dropdown._popper).toBeNull()
|
||||
expect(dropdown._menu).not.toBeNull()
|
||||
expect(dropdown._element).not.toBeNull()
|
||||
expect(btnDropdown.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), jasmine.any(Boolean))
|
||||
spyOn(EventHandler, 'off')
|
||||
|
||||
dropdown.dispose()
|
||||
|
||||
expect(dropdown._menu).toBeNull()
|
||||
expect(dropdown._element).toBeNull()
|
||||
expect(btnDropdown.removeEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), jasmine.any(Boolean))
|
||||
expect(EventHandler.off).toHaveBeenCalledWith(btnDropdown, Dropdown.EVENT_KEY)
|
||||
})
|
||||
|
||||
it('should dispose dropdown with Popper', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user