-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
fix(selection-list): do not allow toggling disabled options #12617
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,12 +410,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu | |
switch (keyCode) { | ||
case SPACE: | ||
case ENTER: | ||
if (!this.disabled) { | ||
this._toggleSelectOnFocusedOption(); | ||
|
||
// Always prevent space from scrolling the page since the list has focus | ||
event.preventDefault(); | ||
} | ||
this._toggleFocusedOptionIfEnabled(); | ||
// Always prevent space from scrolling the page since the list has focus | ||
event.preventDefault(); | ||
break; | ||
case HOME: | ||
case END: | ||
|
@@ -434,7 +431,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu | |
|
||
if ((keyCode === UP_ARROW || keyCode === DOWN_ARROW) && event.shiftKey && | ||
manager.activeItemIndex !== previousFocusIndex) { | ||
this._toggleSelectOnFocusedOption(); | ||
this._toggleFocusedOptionIfEnabled(); | ||
} | ||
} | ||
|
||
|
@@ -492,14 +489,14 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu | |
return this.options.filter(option => option.selected).map(option => option.value); | ||
} | ||
|
||
/** Toggles the selected state of the currently focused option. */ | ||
private _toggleSelectOnFocusedOption(): void { | ||
/** Toggles the state of the currently focused option if enabled. */ | ||
private _toggleFocusedOptionIfEnabled(): void { | ||
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. Even though this method is private, the naming sounds weird. If we end up doing this, instead of what I mentioned above, I think it should be turned into 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. As discussed on Slack. In my opinion, this would result in code duplication that can be avoided by just having such a method. I'm not sure about the method name, though. I agree that |
||
let focusedIndex = this._keyManager.activeItemIndex; | ||
|
||
if (focusedIndex != null && this._isValidIndex(focusedIndex)) { | ||
let focusedOption: MatListOption = this.options.toArray()[focusedIndex]; | ||
|
||
if (focusedOption) { | ||
if (focusedOption && !focusedOption.disabled) { | ||
focusedOption.toggle(); | ||
|
||
// Emit a change event because the focused option changed its state through user | ||
|
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.
Wouldn't it be simpler to fix the issue by having a
return
here instead of thebreak
?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.
How does the issue relate to the
break
here? The issue is that we just check the state ofSelectionList#disabled
instead of looking at the disabled state of the individual option that should be toggled.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.
Ah, sorry I misread it.
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.
No problem, I've updated the PR description to explain what's causing the issue.