0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00

Don't open dropdown on ESC on trigger element (#28912)

* Don't open dropdown on ESC on trigger element

Closes #28751
This commit is contained in:
Patrick H. Lauke 2019-06-17 14:50:43 +01:00 committed by XhmikosR
parent 6587e5cf80
commit 1da3aa3103
2 changed files with 29 additions and 0 deletions

View File

@ -475,6 +475,10 @@ class Dropdown {
const parent = Dropdown._getParentFromElement(this) const parent = Dropdown._getParentFromElement(this)
const isActive = $(parent).hasClass(ClassName.SHOW) const isActive = $(parent).hasClass(ClassName.SHOW)
if (!isActive && event.which === ESCAPE_KEYCODE) {
return
}
if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) { if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
if (event.which === ESCAPE_KEYCODE) { if (event.which === ESCAPE_KEYCODE) {
const toggle = parent.querySelector(Selector.DATA_TOGGLE) const toggle = parent.querySelector(Selector.DATA_TOGGLE)

View File

@ -67,6 +67,31 @@ $(function () {
$dropdown.trigger($.Event('click')) $dropdown.trigger($.Event('click'))
}) })
QUnit.test('should not open dropdown if escape key was pressed on the toggle', function (assert) {
assert.expect(1)
var done = assert.async()
var dropdownHTML = '<div class="tabs">' +
'<div class="dropdown">' +
'<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' +
'<div class="dropdown-menu">' +
'<a class="dropdown-item" href="#">Secondary link</a>' +
'<a class="dropdown-item" href="#">Something else here</a>' +
'<div class="divider"/>' +
'<a class="dropdown-item" href="#">Another link</a>' +
'</div>' +
'</div>' +
'</div>'
$(dropdownHTML).appendTo('#qunit-fixture')
var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown()
var $button = $('button[data-toggle="dropdown"]')
// Key escape
$button.trigger('focus').trigger($.Event('keydown', {
which: 27
}))
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown after escape pressed')
done()
})
QUnit.test('should not add class position-static to dropdown if boundary not set', function (assert) { QUnit.test('should not add class position-static to dropdown if boundary not set', function (assert) {
assert.expect(1) assert.expect(1)
var done = assert.async() var done = assert.async()