-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(selection-list): fix option value coercion and selection events #6901
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
Changes from 1 commit
eb3f6da
390cb1d
801c92c
bc14f66
efab749
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 |
---|---|---|
|
@@ -52,8 +52,9 @@ export class MatListOptionBase {} | |
export const _MatListOptionMixinBase = mixinDisableRipple(MatListOptionBase); | ||
|
||
/** Event emitted by a selection-list whenever the state of an option is changed. */ | ||
export interface MatSelectionListOptionEvent { | ||
export class MatSelectionListOptionEvent { | ||
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.
|
||
option: MatListOption; | ||
selected: boolean; | ||
} | ||
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. I'd keep the consistency between this and other components:
See checkbox for example. 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. So checked instead of selected? Gotcha. 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. In fact, all changes above (to keep consistency). Also with comments so it can be shown in docs. 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. By others you mean other checkboxes? Add comments? Changing it from selected to checked seems like a step in the wrong direction, since it is MatListOption.selected, not MatListOption.checked. 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. You're right (bad copy). Comments === docs description. What about I updated the code above. 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. Sounds good, I'll change that. |
||
|
||
/** | ||
|
@@ -86,7 +87,6 @@ export interface MatSelectionListOptionEvent { | |
export class MatListOption extends _MatListOptionMixinBase | ||
implements AfterContentInit, OnInit, OnDestroy, FocusableOption, CanDisableRipple { | ||
private _lineSetter: MatLineSetter; | ||
private _selected: boolean = false; | ||
private _disabled: boolean = false; | ||
|
||
/** Whether the option has focus. */ | ||
|
@@ -97,34 +97,29 @@ export class MatListOption extends _MatListOptionMixinBase | |
/** Whether the label should appear before or after the checkbox. Defaults to 'after' */ | ||
@Input() checkboxPosition: 'before' | 'after' = 'after'; | ||
|
||
/** Value of the option */ | ||
@Input() value: any; | ||
|
||
/** Whether the option is disabled. */ | ||
@Input() | ||
get disabled() { return (this.selectionList && this.selectionList.disabled) || this._disabled; } | ||
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.
|
||
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); } | ||
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.
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.
Edit: Misread 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. Well... for 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. Gotcha, fixed. 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. Ah, ok. The highlighted code I see is both on this section:
So I was kind of guessing what you meant. Making those changes now. |
||
|
||
/** Value of the option */ | ||
@Input() value: any; | ||
|
||
/** Whether the option is selected. */ | ||
@Input() | ||
get selected() { return this._selected; } | ||
get selected() { return this.selectionList.selectedOptions.isSelected(this); } | ||
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. Can you add |
||
set selected(value: boolean) { | ||
const isSelected = coerceBooleanProperty(value); | ||
|
||
if (isSelected !== this._selected) { | ||
const selectionModel = this.selectionList.selectedOptions; | ||
|
||
this._selected = isSelected; | ||
isSelected ? selectionModel.select(this) : selectionModel.deselect(this); | ||
if (isSelected !== this.selected) { | ||
this.selectionList.selectedOptions.toggle(this); | ||
this._changeDetector.markForCheck(); | ||
this.selectionChange.emit(this._createChangeEvent()); | ||
} | ||
} | ||
|
||
/** Emitted when the option is selected. */ | ||
@Output() selectChange = new EventEmitter<MatSelectionListOptionEvent>(); | ||
|
||
/** Emitted when the option is deselected. */ | ||
@Output() deselected = new EventEmitter<MatSelectionListOptionEvent>(); | ||
/** Emitted when the option is selected or deselected. */ | ||
@Output() selectionChange = new EventEmitter<MatSelectionListOptionEvent>(); | ||
|
||
constructor(private _renderer: Renderer2, | ||
private _element: ElementRef, | ||
|
@@ -174,6 +169,16 @@ export class MatListOption extends _MatListOptionMixinBase | |
this.selectionList._setFocusedOption(this); | ||
} | ||
|
||
/** Creates a selection event object from the specified option. */ | ||
private _createChangeEvent(option: MatListOption = this): MatSelectionListOptionEvent { | ||
let event = new MatSelectionListOptionEvent(); | ||
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.
|
||
|
||
event.option = option; | ||
event.selected = option.selected; | ||
|
||
return event; | ||
} | ||
|
||
/** Retrieves the DOM element of the component host. */ | ||
_getHostElement(): HTMLElement { | ||
return this._element.nativeElement; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
remove the spaces around:
id="selection-list-5"
?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.
I don't know if I hit the format button on my side, but there are spaces around most of the unit test component's id's in this file.
I'll fix them.
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're right. There are a lot of strange spaces in this spec file.