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

Dropdowns: Modified Dropdown's _parent to use a new method called 'cl… #41037

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/src/dom/selector-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const SelectorEngine = {
return parents
},

closest(element, selector) {
return Element.prototype.closest.call(element, selector)
},

prev(element, selector) {
let previous = element.previousElementSibling

Expand Down
3 changes: 2 additions & 1 deletion js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class Dropdown extends BaseComponent {
super(element, config)

this._popper = null
this._parent = this._element.parentNode // dropdown wrapper
const wrapperSelector = `:has(${SELECTOR_MENU})`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for information, :has cannot be used right now in Bootstrap v5 because of our browser support (see .browserslistrc. It'll be upgraded in v6.

this._parent = SelectorEngine.closest(element, wrapperSelector) // dropdown wrapper
// TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
this._menu = SelectorEngine.next(this._element, SELECTOR_MENU)[0] ||
SelectorEngine.prev(this._element, SELECTOR_MENU)[0] ||
Expand Down
22 changes: 22 additions & 0 deletions js/tests/unit/dom/selector-engine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ describe('SelectorEngine', () => {
})
})

describe('closest', () => {
it('should return one element when element with selector is the direct parent', () => {
const testId = 'test'
fixtureEl.innerHTML = `<div id="${testId}"><div id="element"></div></div>`

const element = fixtureEl.querySelector('#element')
const parent = fixtureEl.querySelector(`#${testId}`)

expect(SelectorEngine.closest(element, `#${testId}`)).toEqual(parent)
})

it('should return one element when element with selector is an ancestor', () => {
const testId = 'test'
fixtureEl.innerHTML = `<div id="${testId}"><div><div id="element"></div></div></div>`

const element = fixtureEl.querySelector('#element')
const ancestor = fixtureEl.querySelector(`#${testId}`)

expect(SelectorEngine.closest(element, `#${testId}`)).toEqual(ancestor)
})
})

describe('prev', () => {
it('should return previous element', () => {
fixtureEl.innerHTML = '<div class="test"></div><button class="btn"></button>'
Expand Down