Skip to content

bug(MatButtonToggle): Button with undefined or null value not selected by default #25472

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
1 task
mcalmus opened this issue Aug 16, 2022 · 9 comments · Fixed by #25553
Closed
1 task

bug(MatButtonToggle): Button with undefined or null value not selected by default #25472

mcalmus opened this issue Aug 16, 2022 · 9 comments · Fixed by #25553
Labels
area: material/button-toggle P4 A relatively minor issue that is not relevant to core functions

Comments

@mcalmus
Copy link
Contributor

mcalmus commented Aug 16, 2022

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

If one of the buttons in a button toggle group has an unknown or null value in a template-driven page, that button is not selected at page load. Any other default value works fine.

Reproduction

https://stackblitz.com/edit/angular-1hthwq-eq6mud?file=src%2Fapp%2Fbutton-toggle-forms-example.ts,src%2Fapp%2Fbutton-toggle-forms-example.html

This stackblitz example is a fork of the MatButtonToggle example. It adds a "null" value to the set of options and sets that as the default for the model, but the corresponding button is selected only if explicitly chosen.

Expected Behavior

Button corresponding to null value should be selected on page load.

Actual Behavior

No button is chosen on page load.

Environment

  • Angular:
  • CDK/Material:
  • Browser(s):
  • Operating System (e.g. Windows, macOS, Ubuntu):
@mcalmus mcalmus added the needs triage This issue needs to be triaged by the team label Aug 16, 2022
@kush-savani
Copy link
Contributor

@crisbeto hello, is this actual issue? if yes, can you please assign this to me if possible!

@mcalmus
Copy link
Contributor Author

mcalmus commented Aug 23, 2022

It seems like this line could be (at least part of) the problem:

return toggle.value != null && toggle.value === value;

@zakhenry
Copy link

@kush-savani yep this is an actual issue, and note that this behaviour is inconsistent with mat-radio-button - there is no null check on the value of the radio buttons for selection.

kush-savani added a commit to kush-savani/components that referenced this issue Aug 30, 2022
When button-toggle have init value as null, it's not get selected. This fix remove condition which stopping null button to select.

Fixes angular#25472
@andrewseguin andrewseguin added P4 A relatively minor issue that is not relevant to core functions area: material/button-toggle and removed needs triage This issue needs to be triaged by the team labels May 22, 2023
@andrewseguin
Copy link
Contributor

It's not obvious to me why null should be a supported value.

@kush-savani
Copy link
Contributor

Hello @andrewseguin,
FYI: There is no null check on the value of the mat-radio-button for selection. Code-ref
Here is demo of radio-button: https://stackblitz.com/edit/1alcsb-qssszv

@kush-savani
Copy link
Contributor

Hello @andrewseguin, FYI: There is no null check on the value of the mat-radio-button for selection. Code-ref Here is demo of radio-button: https://stackblitz.com/edit/1alcsb-qssszv

Hello @andrewseguin, I need your input on this issue.

@BaerMitUmlaut
Copy link

It's not obvious to me why null should be a supported value.

Why would it not be a supported value? The paragraph style from the original post is a perfect example of why it would be valid. I just stumbled upon this problem, too, in my case with a nullable boolean (yes / no / n.a.). A select did not feel like the right control to use, but now I will have to convert the value to a string and back.

@aukarelin
Copy link

aukarelin commented Nov 26, 2024

It seems the following workaround (set "checked" explicitly) works fine:

<mat-button-toggle-group [formControl]="control">
    <mat-button-toggle [value]="null" [checked]="control.value === null">
        None
    </mat-button-toggle>
</mat-button-toggle-group>

mmalerba pushed a commit that referenced this issue Jan 30, 2025
When button-toggle have init value as null, it's not get selected. This fix remove condition which stopping null button to select.

Fixes #25472
mmalerba pushed a commit that referenced this issue Jan 30, 2025
When button-toggle have init value as null, it's not get selected. This fix remove condition which stopping null button to select.

Fixes #25472

(cherry picked from commit 9ea468a)
@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 Mar 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: material/button-toggle P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants