0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00

backport #29523: skip hidden dropdowns while focusing

This commit is contained in:
Johann-S 2019-10-28 13:34:11 +01:00 committed by XhmikosR
parent f55566e36b
commit 29f585365f
2 changed files with 42 additions and 0 deletions

View File

@ -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

View File

@ -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')
})
})