Skip to content

Commit

Permalink
Close #33625: support .dropdown-item wrapped in <li> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Apr 15, 2021
1 parent 69f5c01 commit d976313
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion js/src/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ class Tab extends BaseComponent {
element.classList.add(CLASS_NAME_SHOW)
}

if (element.parentNode && element.parentNode.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
let parent = element.parentNode
if (parent && parent.nodeName === 'LI') {
parent = parent.parentNode
}

if (parent && parent.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
const dropdownElement = element.closest(SELECTOR_DROPDOWN)

if (dropdownElement) {
Expand Down
22 changes: 22 additions & 0 deletions js/tests/unit/tab.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,28 @@ describe('Tab', () => {
expect(fixtureEl.querySelector('#nav2 .dropdown-item').classList.contains('active')).toEqual(false)
})

it('should support li > .dropdown-item', () => {
fixtureEl.innerHTML = [
'<ul class="nav nav-tabs">',
' <li class="nav-item"><a class="nav-link active" href="#home" data-bs-toggle="tab">Home</a></li>',
' <li class="nav-item"><a class="nav-link" href="#profile" data-bs-toggle="tab">Profile</a></li>',
' <li class="nav-item dropdown">',
' <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#">Dropdown</a>',
' <div class="dropdown-menu">',
' <li><a class="dropdown-item" href="#dropdown1" id="dropdown1-tab" data-bs-toggle="tab">@fat</a></li>',
' <li><a class="dropdown-item" href="#dropdown2" id="dropdown2-tab" data-bs-toggle="tab">@mdo</a></li>',
' </div>',
' </li>',
'</ul>'
].join('')

const firstDropItem = fixtureEl.querySelector('.dropdown-item')

firstDropItem.click()
expect(firstDropItem.classList.contains('active')).toEqual(true)
expect(fixtureEl.querySelector('.nav-link').classList.contains('active')).toEqual(false)
})

it('should handle nested tabs', done => {
fixtureEl.innerHTML = [
'<nav class="nav nav-tabs" role="tablist">',
Expand Down

0 comments on commit d976313

Please sign in to comment.