Skip to content

Commit

Permalink
refactor: make static selectMenuItem method private (#33589)
Browse files Browse the repository at this point in the history
  • Loading branch information
alpadev committed Apr 11, 2021
1 parent 5664512 commit ad10f00
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions js/src/dropdown.js
Expand Up @@ -356,6 +356,31 @@ 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
index = index === -1 ? 0 : index

items[index].focus()
}

// Static

static dropdownInterface(element, config) {
Expand Down Expand Up @@ -449,31 +474,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
}
Expand Down Expand Up @@ -525,7 +525,7 @@ class Dropdown extends BaseComponent {
return
}

Dropdown.selectMenuItem(Dropdown.getParentFromElement(this), event)
Dropdown.getInstance(getToggleButton())._selectMenuItem(event)
}
}

Expand Down

0 comments on commit ad10f00

Please sign in to comment.