Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

josephperrott
Copy link
Member

Fixes #14059

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Nov 9, 2018
@josephperrott josephperrott added the target: patch This PR is targeted for the next patch release label Nov 9, 2018
@@ -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;
Copy link
Member

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.

Copy link

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.

@mmalerba mmalerba added aaa and removed aaa labels Apr 25, 2019
@andrewseguin andrewseguin added the P4 A relatively minor issue that is not relevant to core functions label May 30, 2019
@Mereo4
Copy link

Mereo4 commented Oct 15, 2019

Hello, do you have some news about this fix, please? It seems to have been forgotten. :-p

@konsultaner
Copy link

@jelbourn @crisbeto Any news on this?

@josephperrott josephperrott deleted the button-toggle branch March 20, 2020 22:15
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement P4 A relatively minor issue that is not relevant to core functions target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Button toggle group acts wrong if value is null
8 participants