Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make static selectMenuItem method private #33589

Merged
merged 3 commits into from Apr 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
alpadev marked this conversation as resolved.
Show resolved Hide resolved
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