Dropdown: Deduplicate complex check
This commit is contained in:
parent
bff95d55af
commit
a14a552d83
|
@ -39,8 +39,6 @@ const ARROW_UP_KEY = 'ArrowUp'
|
||||||
const ARROW_DOWN_KEY = 'ArrowDown'
|
const ARROW_DOWN_KEY = 'ArrowDown'
|
||||||
const RIGHT_MOUSE_BUTTON = 2 // MouseEvent.button value for the secondary button, usually the right button
|
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_HIDE = `hide${EVENT_KEY}`
|
||||||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
||||||
const EVENT_SHOW = `show${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 other than escape
|
||||||
// - If key is not up or down => not a dropdown command
|
// - If key is not up or down => not a dropdown command
|
||||||
// - If trigger inside the menu => 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 &&
|
const isInput = /input|textarea/i.test(event.target.tagName)
|
||||||
((event.key !== ARROW_DOWN_KEY && event.key !== ARROW_UP_KEY) ||
|
const eventKey = event.key
|
||||||
event.target.closest(SELECTOR_MENU))) :
|
if (!isInput && ![ARROW_UP_KEY, ARROW_DOWN_KEY, ESCAPE_KEY].includes(eventKey)) {
|
||||||
!REGEXP_KEYDOWN.test(event.key)) {
|
|
||||||
return
|
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)
|
const isActive = this.classList.contains(CLASS_NAME_SHOW)
|
||||||
|
|
||||||
if (!isActive && event.key === ESCAPE_KEY) {
|
if (!isActive && event.key === ESCAPE_KEY) {
|
||||||
|
|
Loading…
Reference in New Issue