From 806f8fd3588bf9abc917b7a58bee95f5f0f2999a Mon Sep 17 00:00:00 2001 From: alpadev Date: Fri, 9 Apr 2021 02:18:31 +0200 Subject: [PATCH 1/2] refactor: make static selectMenuItem method private requested by rohit2sharma95 - https://github.com/twbs/bootstrap/pull/33479#issuecomment-810172509 --- js/src/dropdown.js | 54 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 6b541ed150b3..a6db4213dcda 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -357,6 +357,33 @@ class Dropdown extends BaseComponent { } } + _selectMenuItem(event) { + const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible) + + if (!items.length) { + return + } + + let index = items.indexOf(event.target) + + // Up + if (event.key === ARROW_UP_KEY && index > 0) { + index-- + } + + // Down + if (event.key === ARROW_DOWN_KEY && index < items.length - 1) { + index++ + } + + // index is -1 if the first keydown is an ArrowUp + if (index < 0) { + index = 0 + } + + items[index].focus() + } + // Static static dropdownInterface(element, config) { @@ -450,31 +477,6 @@ class Dropdown extends BaseComponent { } } - static selectMenuItem(parent, event) { - const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible) - - if (!items.length) { - return - } - - let index = items.indexOf(event.target) - - // Up - if (event.key === ARROW_UP_KEY && index > 0) { - index-- - } - - // Down - if (event.key === ARROW_DOWN_KEY && index < items.length - 1) { - index++ - } - - // index is -1 if the first keydown is an ArrowUp - index = index === -1 ? 0 : index - - items[index].focus() - } - static getParentFromElement(element) { return getElementFromSelector(element) || element.parentNode } @@ -526,7 +528,7 @@ class Dropdown extends BaseComponent { return } - Dropdown.selectMenuItem(Dropdown.getParentFromElement(this), event) + Dropdown.getInstance(getToggleButton())._selectMenuItem(event) } } From 8dded2464fa5b49f09ddc7e2c8bc476d8648ec86 Mon Sep 17 00:00:00 2001 From: alpadev Date: Sat, 10 Apr 2021 22:00:00 +0200 Subject: [PATCH 2/2] fix: revert if condition requested by rohit2sharma95 - https://github.com/twbs/bootstrap/pull/33589/files#r611072197 --- js/src/dropdown.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/js/src/dropdown.js b/js/src/dropdown.js index a6db4213dcda..72a0aa4f1d12 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -377,9 +377,7 @@ class Dropdown extends BaseComponent { } // index is -1 if the first keydown is an ArrowUp - if (index < 0) { - index = 0 - } + index = index === -1 ? 0 : index items[index].focus() }