aboutsummaryrefslogtreecommitdiff
path: root/js/src/dropdown.js
diff options
context:
space:
mode:
authoralpadev <[email protected]>2021-05-22 09:58:52 +0200
committerGitHub <[email protected]>2021-05-22 10:58:52 +0300
commitb39b665072a2d36914e741b2c11620323924be89 (patch)
treecaaf6f2f815901efff78bdca9796fb7a46d5f785 /js/src/dropdown.js
parent803397554836dcba736eb50020ed3cea07b3a3ea (diff)
downloadbootstrap-b39b665072a2d36914e741b2c11620323924be89.tar.xz
bootstrap-b39b665072a2d36914e741b2c11620323924be89.zip
Automatically select an item in the dropdown when using arrow keys (#34052)
Diffstat (limited to 'js/src/dropdown.js')
-rw-r--r--js/src/dropdown.js21
1 files changed, 10 insertions, 11 deletions
diff --git a/js/src/dropdown.js b/js/src/dropdown.js
index cab2d018b..34beb6512 100644
--- a/js/src/dropdown.js
+++ b/js/src/dropdown.js
@@ -354,18 +354,16 @@ class Dropdown extends BaseComponent {
}
}
- _selectMenuItem(event) {
- if (![ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key)) {
- return
- }
-
+ _selectMenuItem({ key, target }) {
const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible)
if (!items.length) {
return
}
- getNextActiveElement(items, event.target, event.key === ARROW_DOWN_KEY, false).focus()
+ // if target isn't included in items (e.g. when expanding the dropdown)
+ // allow cycling to get the last item in case key equals ARROW_UP_KEY
+ getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus()
}
// Static
@@ -480,17 +478,18 @@ class Dropdown extends BaseComponent {
return
}
- if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
- getToggleButton().click()
+ if (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY) {
+ if (!isActive) {
+ getToggleButton().click()
+ }
+
+ Dropdown.getInstance(getToggleButton())._selectMenuItem(event)
return
}
if (!isActive || event.key === SPACE_KEY) {
Dropdown.clearMenus()
- return
}
-
- Dropdown.getInstance(getToggleButton())._selectMenuItem(event)
}
}