Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const handleKeyDown = ev => {
ev.persist()
const isEnabledKey = ENABLED_KEYS.includes(ev.key)
const options = refsMoleculeSelectOptions.current.map(getTarget)
const domSourceEvent = ev.target
const domMoleculeSelect = refMoleculeSelect.current
if (!isOpen && isEnabledKey) {
domSourceEvent === domMoleculeSelect && handleToggle(ev)
} else {
const currentElementFocused = getCurrentElementFocused()
const isSomeOptionFocused = [...options].includes(currentElementFocused)
if (ev.key === 'Escape') closeList(ev)
if (ev.key === 'ArrowDown' && !isSomeOptionFocused)
focusFirstOption(ev, {options})
}
}
setTimeout(() => {
const currentElementFocused = getCurrentElementFocused()
const focusOutFromOutside = ![domInnerInput, ...options].includes(
currentElementFocused
)
if (focusOutFromOutside) {
isOpen ? closeList(ev) : setFocus(false)
}
}, 1)
setFocus(true)
setTimeout(() => {
const currentElementFocused = getCurrentElementFocused()
const isSomeOptionFocused = [...options].includes(currentElementFocused)
const isOptionListFocused = firstOption
? currentElementFocused.isSameNode(firstOption.parentNode)
: false
if (!isSomeOptionFocused && !isOptionListFocused && isOpen) {
closeList(ev)
}
}, 1)
setFocus(false)