Skip to content

Commit

Permalink
refactor: make static selectMenuItem method private
Browse files Browse the repository at this point in the history
requested by rohit2sharma95 - twbs#33479 (comment)
  • Loading branch information
alpadev committed Apr 9, 2021
1 parent 9bca1b5 commit 806f8fd
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions js/src/dropdown.js
Expand Up @@ -357,6 +357,33 @@ 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
if (index < 0) {
index = 0
}

items[index].focus()
}

// Static

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

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

Expand Down

0 comments on commit 806f8fd

Please sign in to comment.