-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(button-toggle): allow selection of null toggle button #14061
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
Conversation
@@ -333,7 +333,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After | |||
/** Selects a value if there's a toggle that corresponds to it. */ | |||
private _selectValue(value: any) { | |||
const correspondingOption = this._buttonToggles.find(toggle => { | |||
return toggle.value != null && toggle.value === value; |
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.
If we’re following how mat-select works, these changes should apply only in multiple mode. In single-selection mode null options act as a reset.
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.
Why only in multiple mode? Can we handle both? Like maybe setting the value to null is a "reset" in both select and button toggle unless there's an option inside that binds to null. Then select that.
My use case is that I have a record that's either deemed a Failure, Not a Failure, Or not yet reviewed. I'm storing this in the database just using a nullable boolean. The following code doesn't select "Pending" when you load it up for the first time because of this "null means reset" philosophy
<mat-button-toggle-group color="primary" [(ngModel)]="row.failure">
<mat-button-toggle [value]="null">Pending Review</mat-button-toggle>
<mat-button-toggle [value]="false">Incident</mat-button-toggle>
<mat-button-toggle [value]="true">Failure</mat-button-toggle>
</mat-button-toggle-group>
Sounds like the same code wouldn't work if I changed it to a mat-select instead of using button-toggle. I can work around this easily but it's kind of odd that you can't treat null
as a real value, especially when we have undefined
as an option in JS which seems more like a "reset" style of value.
Hello, do you have some news about this fix, please? It seems to have been forgotten. :-p |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Fixes #14059