Skip to content

Commit

Permalink
backport #29523: skip hidden dropdowns while focusing
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S authored and XhmikosR committed Nov 2, 2019
1 parent f55566e commit 29f5853
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ class Dropdown {
}

const items = [].slice.call(parent.querySelectorAll(Selector.VISIBLE_ITEMS))
.filter((item) => $(item).is(':visible'))

if (items.length === 0) {
return
Expand Down
41 changes: 41 additions & 0 deletions js/tests/unit/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,4 +1585,45 @@ $(function () {

dropdown.show(true)
})

QUnit.test('it should skip hidden element when using keyboard navigation', function (assert) {
assert.expect(3)
var done = assert.async()

var fixtureHtml = [
'<style>',
' .d-none {',
' display: none;',
' }',
'</style>',
'<div class="dropdown">',
' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <button class="dropdown-item d-none" type="button">Hidden button by class</button>',
' <a class="dropdown-item" href="#sub1" style="display: none">Hidden link</a>',
' <a class="dropdown-item" href="#sub1" style="visibility: hidden">Hidden link</a>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')

$(fixtureHtml).appendTo('#qunit-fixture')

var $dropdownEl = $('.dropdown')
var $dropdown = $('[data-toggle="dropdown"]')
.bootstrapDropdown()

$dropdownEl.one('shown.bs.dropdown', function () {
$dropdown.trigger($.Event('keydown', {
which: 40
}))

assert.strictEqual($(document.activeElement).hasClass('d-none'), false, '.d-none not focused')
assert.strictEqual($(document.activeElement).css('display') === 'none', false, '"display: none" not focused')
assert.strictEqual(document.activeElement.style.visibility === 'hidden', false, '"visibility: hidden" not focused')
done()
})

$dropdown.trigger('click')
})
})

0 comments on commit 29f5853

Please sign in to comment.