diff options
author | GeoSot <geo.sotis@gmail.com> | 2021-10-10 16:21:34 +0300 |
---|---|---|
committer | XhmikosR <xhmikosr@gmail.com> | 2021-12-01 18:10:39 +0300 |
commit | a14a552d83a8f5452f2ef53a6a85a91c8aafb5f7 (patch) | |
tree | 982ae08749cd7179d70026f8f217a29a4d09689e /js/src/dropdown.js | |
parent | bff95d55af1074d67738c5f83d69f7b8cff5a22a (diff) |
Dropdown: Deduplicate complex check
Diffstat (limited to 'js/src/dropdown.js')
-rw-r--r-- | js/src/dropdown.js | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 6c613efc6b..90bc582b96 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -39,8 +39,6 @@ const ARROW_UP_KEY = 'ArrowUp' const ARROW_DOWN_KEY = 'ArrowDown' const RIGHT_MOUSE_BUTTON = 2 // MouseEvent.button value for the secondary button, usually the right button -const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEY}|${ARROW_DOWN_KEY}|${ESCAPE_KEY}`) - const EVENT_HIDE = `hide${EVENT_KEY}` const EVENT_HIDDEN = `hidden${EVENT_KEY}` const EVENT_SHOW = `show${EVENT_KEY}` @@ -407,14 +405,23 @@ class Dropdown extends BaseComponent { // - If key is other than escape // - If key is not up or down => not a dropdown command // - If trigger inside the menu => not a dropdown command - if (/input|textarea/i.test(event.target.tagName) ? - event.key === SPACE_KEY || (event.key !== ESCAPE_KEY && - ((event.key !== ARROW_DOWN_KEY && event.key !== ARROW_UP_KEY) || - event.target.closest(SELECTOR_MENU))) : - !REGEXP_KEYDOWN.test(event.key)) { + + const isInput = /input|textarea/i.test(event.target.tagName) + const eventKey = event.key + if (!isInput && ![ARROW_UP_KEY, ARROW_DOWN_KEY, ESCAPE_KEY].includes(eventKey)) { return } + if (isInput) { + if (eventKey === SPACE_KEY) { + return + } + + if (eventKey !== ESCAPE_KEY && (![ARROW_UP_KEY, ARROW_DOWN_KEY].includes(eventKey) || event.target.closest(SELECTOR_MENU))) { + return + } + } + const isActive = this.classList.contains(CLASS_NAME_SHOW) if (!isActive && event.key === ESCAPE_KEY) { |