Skip to content

Commit 9ea468a

Browse files
authored
fix(material/button-toggle): make null value selected on init (#25553)
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
1 parent bf4c633 commit 9ea468a

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Diff for: src/material/button-toggle/button-toggle.spec.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,22 @@ describe('MatButtonToggle without forms', () => {
10041004
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(true);
10051005
});
10061006

1007+
it('should not throw on init when toggles are repeated and there is an initial value null', () => {
1008+
const fixture = TestBed.createComponent(RepeatedButtonTogglesWithPreselectedValue);
1009+
fixture.detectChanges();
1010+
1011+
expect(fixture.componentInstance.toggleGroup.value).toBe('Two');
1012+
expect(fixture.componentInstance.toggles.toArray()[1].checked).toBe(true);
1013+
1014+
fixture.componentInstance.possibleValues = [null, 'Five', 'Six'];
1015+
fixture.componentInstance.value = null;
1016+
fixture.changeDetectorRef.markForCheck();
1017+
fixture.detectChanges();
1018+
1019+
expect(fixture.componentInstance.toggleGroup.value).toBe(null);
1020+
expect(fixture.componentInstance.toggles.toArray()[0].checked).toBe(true);
1021+
});
1022+
10071023
it('should not throw on init when toggles are repeated and there is an initial value', () => {
10081024
const fixture = TestBed.createComponent(ButtonToggleWithStaticName);
10091025
fixture.detectChanges();
@@ -1245,8 +1261,8 @@ class RepeatedButtonTogglesWithPreselectedValue {
12451261
@ViewChild(MatButtonToggleGroup) toggleGroup: MatButtonToggleGroup;
12461262
@ViewChildren(MatButtonToggle) toggles: QueryList<MatButtonToggle>;
12471263

1248-
possibleValues = ['One', 'Two', 'Three'];
1249-
value = 'Two';
1264+
possibleValues: (string | null)[] = ['One', 'Two', 'Three'];
1265+
value: string | null = 'Two';
12501266
}
12511267

12521268
@Component({

Diff for: src/material/button-toggle/button-toggle.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
*/
88

99
import {_IdGenerator, FocusMonitor} from '@angular/cdk/a11y';
10+
import {Direction, Directionality} from '@angular/cdk/bidi';
1011
import {SelectionModel} from '@angular/cdk/collections';
11-
import {DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, UP_ARROW, SPACE, ENTER} from '@angular/cdk/keycodes';
12+
import {DOWN_ARROW, ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, UP_ARROW} from '@angular/cdk/keycodes';
13+
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
1214
import {
1315
AfterContentInit,
16+
AfterViewInit,
17+
ANIMATION_MODULE_TYPE,
18+
booleanAttribute,
1419
ChangeDetectionStrategy,
1520
ChangeDetectorRef,
1621
Component,
@@ -19,24 +24,19 @@ import {
1924
ElementRef,
2025
EventEmitter,
2126
forwardRef,
27+
HostAttributeToken,
28+
inject,
29+
InjectionToken,
2230
Input,
2331
OnDestroy,
2432
OnInit,
2533
Output,
2634
QueryList,
2735
ViewChild,
2836
ViewEncapsulation,
29-
InjectionToken,
30-
AfterViewInit,
31-
booleanAttribute,
32-
inject,
33-
HostAttributeToken,
34-
ANIMATION_MODULE_TYPE,
3537
} from '@angular/core';
36-
import {Direction, Directionality} from '@angular/cdk/bidi';
3738
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
38-
import {MatRipple, MatPseudoCheckbox, _StructuralStylesLoader} from '@angular/material/core';
39-
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
39+
import {_StructuralStylesLoader, MatPseudoCheckbox, MatRipple} from '@angular/material/core';
4040

4141
/**
4242
* @deprecated No longer used.
@@ -513,7 +513,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
513513
/** Selects a value if there's a toggle that corresponds to it. */
514514
private _selectValue(value: any, toggles: MatButtonToggle[]) {
515515
for (const toggle of toggles) {
516-
if (toggle.value != null && toggle.value === value) {
516+
if (toggle.value === value) {
517517
toggle.checked = true;
518518
this._selectionModel.select(toggle);
519519
if (!this.multiple) {

0 commit comments

Comments
 (0)