diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 97bba1c763..29c4efe047 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -35,6 +35,7 @@ const Dropdown = (($) => { SHOWN : `shown${EVENT_KEY}`, CLICK : `click${EVENT_KEY}`, CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`, + FOCUSIN_DATA_API : `focusin${EVENT_KEY}${DATA_API_KEY}`, KEYDOWN_DATA_API : `keydown${EVENT_KEY}${DATA_API_KEY}` } @@ -180,9 +181,9 @@ const Dropdown = (($) => { continue } - if (event && event.type === 'click' && - /input|textarea/i.test(event.target.tagName) && - $.contains(parent, event.target)) { + if (event && (event.type === 'click' && + /input|textarea/i.test(event.target.tagName) || event.type === 'focusin') + && $.contains(parent, event.target)) { continue } @@ -275,7 +276,7 @@ const Dropdown = (($) => { .on(Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler) .on(Event.KEYDOWN_DATA_API, Selector.ROLE_MENU, Dropdown._dataApiKeydownHandler) .on(Event.KEYDOWN_DATA_API, Selector.ROLE_LISTBOX, Dropdown._dataApiKeydownHandler) - .on(Event.CLICK_DATA_API, Dropdown._clearMenus) + .on(`${Event.CLICK_DATA_API} ${Event.FOCUSIN_DATA_API}`, Dropdown._clearMenus) .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, Dropdown.prototype.toggle) .on(Event.CLICK_DATA_API, Selector.FORM_CHILD, (e) => { e.stopPropagation() diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js index 53455c2a62..9a34fc0c93 100644 --- a/js/tests/unit/dropdown.js +++ b/js/tests/unit/dropdown.js @@ -192,6 +192,30 @@ $(function () { assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed') }) + QUnit.test('should remove "show" class if body is focused', function (assert) { + assert.expect(2) + var dropdownHTML = '
' + + '' + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + .trigger('click') + + assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') + $(document.body).trigger('focusin') + assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed') + }) + QUnit.test('should remove "show" class if body is clicked, with multiple dropdowns', function (assert) { assert.expect(7) var dropdownHTML = '