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

Simplify input groups by dropping .input-group-prepend and .input-group-append #29885

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions js/src/dom/selector-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ const SelectorEngine = {
previous = previous.previousElementSibling
}

return []
},

next(element, selector) {
let next = element.nextElementSibling

while (next) {
if (this.matches(next, selector)) {
return [next]
}

next = next.nextElementSibling
}

return []
}
}
Expand Down
37 changes: 17 additions & 20 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Dropdown {
return
}

const isActive = this._menu.classList.contains(CLASS_NAME_SHOW)
const isActive = this._element.classList.contains(CLASS_NAME_SHOW)

Dropdown.clearMenus()

Expand All @@ -150,7 +150,7 @@ class Dropdown {
relatedTarget: this._element
}

const showEvent = EventHandler.trigger(parent, EVENT_SHOW, relatedTarget)
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)

if (showEvent.defaultPrevented) {
return
Expand Down Expand Up @@ -199,7 +199,7 @@ class Dropdown {
this._element.setAttribute('aria-expanded', true)

Manipulator.toggleClass(this._menu, CLASS_NAME_SHOW)
Manipulator.toggleClass(parent, CLASS_NAME_SHOW)
Manipulator.toggleClass(this._element, CLASS_NAME_SHOW)
EventHandler.trigger(parent, EVENT_SHOWN, relatedTarget)
}

Expand All @@ -224,7 +224,7 @@ class Dropdown {
}

Manipulator.toggleClass(this._menu, CLASS_NAME_SHOW)
Manipulator.toggleClass(parent, CLASS_NAME_SHOW)
Manipulator.toggleClass(this._element, CLASS_NAME_SHOW)
EventHandler.trigger(parent, EVENT_HIDDEN, relatedTarget)
}

Expand Down Expand Up @@ -273,9 +273,7 @@ class Dropdown {
}

_getMenuElement() {
const parent = Dropdown.getParentFromElement(this._element)

return SelectorEngine.findOne(SELECTOR_MENU, parent)
return SelectorEngine.next(this._element, SELECTOR_MENU)[0]
}

_getPlacement() {
Expand Down Expand Up @@ -397,14 +395,14 @@ class Dropdown {
}

const dropdownMenu = context._menu
if (!parent.classList.contains(CLASS_NAME_SHOW)) {
if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) {
continue
}

if (event && ((event.type === 'click' &&
/input|textarea/i.test(event.target.tagName)) ||
(event.type === 'keyup' && event.which === TAB_KEYCODE)) &&
parent.contains(event.target)) {
dropdownMenu.contains(event.target)) {
continue
}

Expand All @@ -427,7 +425,7 @@ class Dropdown {
}

dropdownMenu.classList.remove(CLASS_NAME_SHOW)
parent.classList.remove(CLASS_NAME_SHOW)
toggles[i].classList.remove(CLASS_NAME_SHOW)
EventHandler.trigger(parent, EVENT_HIDDEN, relatedTarget)
}
}
Expand Down Expand Up @@ -460,13 +458,16 @@ class Dropdown {
}

const parent = Dropdown.getParentFromElement(this)
const isActive = parent.classList.contains(CLASS_NAME_SHOW)
const isActive = this.classList.contains(CLASS_NAME_SHOW)

if (!isActive || (isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE))) {
if (event.which === ESCAPE_KEYCODE) {
SelectorEngine.findOne(SELECTOR_DATA_TOGGLE, parent).focus()
}
if (event.which === ESCAPE_KEYCODE) {
const button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
button.focus()
Dropdown.clearMenus()
return
}

if (!isActive || event.which === SPACE_KEYCODE) {
Dropdown.clearMenus()
return
}
Expand All @@ -478,7 +479,7 @@ class Dropdown {
return
}

let index = items.indexOf(event.target)
let index = items.indexOf(event.target) || 0

if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
index--
Expand All @@ -488,10 +489,6 @@ class Dropdown {
index++
}

if (index < 0) {
index = 0
}

items[index].focus()
}

Expand Down
39 changes: 39 additions & 0 deletions js/tests/unit/dom/selector-engine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,44 @@ describe('SelectorEngine', () => {
expect(SelectorEngine.prev(btn, '.test')).toEqual([divTest])
})
})

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

const btn = fixtureEl.querySelector('.btn')
const divTest = fixtureEl.querySelector('.test')

expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
})

it('should return next element with an extra element between', () => {
fixtureEl.innerHTML = [
'<div class="test"></div>',
'<span></span>',
'<button class="btn"></button>'
].join('')

const btn = fixtureEl.querySelector('.btn')
const divTest = fixtureEl.querySelector('.test')

expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
})

it('should return next element with comments or text nodes between', () => {
fixtureEl.innerHTML = [
'<div class="test"></div>',
'<!-- Comment-->',
'Text',
'<button class="btn"></button>',
'<button class="btn"></button>'
].join('')

const btn = fixtureEl.querySelector('.btn')
const divTest = fixtureEl.querySelector('.test')

expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
})
})
})

Loading