This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(menu): Add --selected class to menu items #2084
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4d5b0a1
feat(menu): Modify menu to add --selected classes to selected list items
williamernest fc285f0
feat(menu): Update tests
williamernest 141ca77
feat(menu): Update tests
williamernest 971b948
feat(menu): Fix typo
williamernest 60ff443
feat(menu): Update tests
williamernest 151c4e6
feat(menu): Update tests
williamernest 7dcc8a4
feat(menu): Update after comments
williamernest d5ff68f
Merge branch 'master' into feat/menu/add-selected-to-list-items
lynnmercier 8cb3c5b
Merge branch 'master' into feat/menu/add-selected-to-list-items
williamernest 82ed956
Merge branch 'master' into feat/menu/add-selected-to-list-items
williamernest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,10 @@ class MDCMenuFoundation extends MDCFoundation { | |
setTransformOrigin: () => {}, | ||
setPosition: () => {}, | ||
setMaxHeight: () => {}, | ||
setAttrForOptionAtIndex: () => {}, | ||
rmAttrForOptionAtIndex: () => {}, | ||
addClassForOptionAtIndex: () => {}, | ||
rmClassForOptionAtIndex: () => {}, | ||
}); | ||
} | ||
|
||
|
@@ -137,6 +141,16 @@ class MDCMenuFoundation extends MDCFoundation { | |
this.anchorMargin_ = {top: 0, right: 0, bottom: 0, left: 0}; | ||
/** @private {?AutoLayoutMeasurements} */ | ||
this.measures_ = null; | ||
/** @private {number} */ | ||
this.selectedIndex_ = -1; | ||
/** @private {boolean} */ | ||
this.rememberSelection_ = false; | ||
|
||
// A keyup event on the menu needs to have a corresponding keydown | ||
// event on the menu. If the user opens the menu with a keydown event on a | ||
// button, the menu will only get the key up event causing buggy behavior with selected elements. | ||
/** @private {boolean} */ | ||
this.keyDownWithinMenu_ = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this keeps track of whether the user currently has a keydown interaction happening on the These sorts of fields can be vague...so add a comment about exactly what it is this field represents |
||
} | ||
|
||
init() { | ||
|
@@ -188,13 +202,25 @@ class MDCMenuFoundation extends MDCFoundation { | |
this.anchorMargin_.left = typeof margin.left === 'number' ? margin.left : 0; | ||
} | ||
|
||
/** @param {boolean} rememberSelection */ | ||
setRememberSelection(rememberSelection) { | ||
this.rememberSelection_ = rememberSelection; | ||
this.setSelectedIndex(-1); | ||
} | ||
|
||
/** | ||
* @param {?number} focusIndex | ||
* @private | ||
*/ | ||
focusOnOpen_(focusIndex) { | ||
if (focusIndex === null) { | ||
// First, try focusing the menu. | ||
// If this instance of MDCMenu remembers selections, and the user has | ||
// made a selection, then focus the last selected item | ||
if (this.rememberSelection_ && this.selectedIndex_ >= 0) { | ||
this.adapter_.focusItemAtIndex(this.selectedIndex_); | ||
return; | ||
} | ||
|
||
this.adapter_.focus(); | ||
// If that doesn't work, focus first item instead. | ||
if (!this.adapter_.isFocused()) { | ||
|
@@ -241,6 +267,9 @@ class MDCMenuFoundation extends MDCFoundation { | |
const isArrowUp = key === 'ArrowUp' || keyCode === 38; | ||
const isArrowDown = key === 'ArrowDown' || keyCode === 40; | ||
const isSpace = key === 'Space' || keyCode === 32; | ||
const isEnter = key === 'Enter' || keyCode === 13; | ||
// The menu needs to know if the keydown event was triggered on the menu | ||
this.keyDownWithinMenu_ = isEnter || isSpace; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explain whats going on here |
||
|
||
const focusedItemIndex = this.adapter_.getFocusedItemIndex(); | ||
const lastItemIndex = this.adapter_.getNumberOfItems() - 1; | ||
|
@@ -297,7 +326,12 @@ class MDCMenuFoundation extends MDCFoundation { | |
const isEscape = key === 'Escape' || keyCode === 27; | ||
|
||
if (isEnter || isSpace) { | ||
this.handlePossibleSelected_(evt); | ||
// If the keydown event didn't occur on the menu, then it should | ||
// disregard the possible selected event. | ||
if (this.keyDownWithinMenu_) { | ||
this.handlePossibleSelected_(evt); | ||
} | ||
this.keyDownWithinMenu_ = false; | ||
} | ||
|
||
if (isEscape) { | ||
|
@@ -327,6 +361,9 @@ class MDCMenuFoundation extends MDCFoundation { | |
this.selectedTriggerTimerId_ = setTimeout(() => { | ||
this.selectedTriggerTimerId_ = 0; | ||
this.close(); | ||
if (this.rememberSelection_) { | ||
this.setSelectedIndex(targetIndex); | ||
} | ||
this.adapter_.notifySelected({index: targetIndex}); | ||
}, numbers.SELECTED_TRIGGER_DELAY); | ||
} | ||
|
@@ -564,6 +601,32 @@ class MDCMenuFoundation extends MDCFoundation { | |
isOpen() { | ||
return this.isOpen_; | ||
} | ||
|
||
/** @return {number} */ | ||
getSelectedIndex() { | ||
return this.selectedIndex_; | ||
} | ||
|
||
/** | ||
* @param {number} index Index of the item to set as selected. | ||
*/ | ||
setSelectedIndex(index) { | ||
if (index === this.selectedIndex_) { | ||
return; | ||
} | ||
|
||
const prevSelectedIndex = this.selectedIndex_; | ||
if (prevSelectedIndex >= 0) { | ||
this.adapter_.rmAttrForOptionAtIndex(prevSelectedIndex, 'aria-selected'); | ||
this.adapter_.rmClassForOptionAtIndex(prevSelectedIndex, cssClasses.SELECTED_LIST_ITEM); | ||
} | ||
|
||
this.selectedIndex_ = index >= 0 && index < this.adapter_.getNumberOfItems() ? index : -1; | ||
if (this.selectedIndex_ >= 0) { | ||
this.adapter_.setAttrForOptionAtIndex(this.selectedIndex_, 'aria-selected', 'true'); | ||
this.adapter_.addClassForOptionAtIndex(this.selectedIndex_, cssClasses.SELECTED_LIST_ITEM); | ||
} | ||
} | ||
} | ||
|
||
export {MDCMenuFoundation, AnchorMargin}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be able to reference the constants on MDCListFoundation.strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MDCListFoundation doesn't exist