Skip to content

Commit 37732cb

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(button): not updating DOM node name if group name changes (#14963)
Fixes the underlying DOM node's name not being in sync with the group, if the group's name is set through the input.
1 parent a8a6617 commit 37732cb

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed
 

‎src/lib/button-toggle/button-toggle.spec.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('MatButtonToggle with forms', () => {
7979
let buttonToggleInstances: MatButtonToggle[];
8080
let testComponent: ButtonToggleGroupWithNgModel;
8181
let groupNgModel: NgModel;
82-
let buttonToggleLabels: HTMLElement[];
82+
let innerButtons: HTMLElement[];
8383

8484
beforeEach(fakeAsync(() => {
8585
fixture = TestBed.createComponent(ButtonToggleGroupWithNgModel);
@@ -92,7 +92,7 @@ describe('MatButtonToggle with forms', () => {
9292

9393
buttonToggleDebugElements = fixture.debugElement.queryAll(By.directive(MatButtonToggle));
9494
buttonToggleInstances = buttonToggleDebugElements.map(debugEl => debugEl.componentInstance);
95-
buttonToggleLabels = buttonToggleDebugElements.map(
95+
innerButtons = buttonToggleDebugElements.map(
9696
debugEl => debugEl.query(By.css('button')).nativeElement);
9797

9898
fixture.detectChanges();
@@ -102,7 +102,7 @@ describe('MatButtonToggle with forms', () => {
102102
expect(testComponent.modelValue).toBeUndefined();
103103
expect(testComponent.lastEvent).toBeUndefined();
104104

105-
buttonToggleLabels[0].click();
105+
innerButtons[0].click();
106106
fixture.detectChanges();
107107

108108
tick();
@@ -122,6 +122,18 @@ describe('MatButtonToggle with forms', () => {
122122
}
123123
});
124124

125+
it('should update the name of radio DOM elements if the name of the group changes', () => {
126+
expect(innerButtons.every(button => button.getAttribute('name') === groupInstance.name))
127+
.toBe(true, 'Expected all buttons to have the initial name.');
128+
129+
fixture.componentInstance.groupName = 'changed-name';
130+
fixture.detectChanges();
131+
132+
expect(groupInstance.name).toBe('changed-name');
133+
expect(innerButtons.every(button => button.getAttribute('name') === groupInstance.name))
134+
.toBe(true, 'Expected all buttons to have the new name.');
135+
});
136+
125137
it('should check the corresponding button toggle on a group value change', () => {
126138
expect(groupInstance.value).toBeFalsy();
127139
for (let buttonToggle of buttonToggleInstances) {
@@ -152,7 +164,7 @@ describe('MatButtonToggle with forms', () => {
152164
expect(groupNgModel.pristine).toBe(true);
153165
expect(groupNgModel.touched).toBe(false);
154166

155-
buttonToggleLabels[2].click();
167+
innerButtons[2].click();
156168
fixture.detectChanges();
157169
tick();
158170

@@ -162,7 +174,7 @@ describe('MatButtonToggle with forms', () => {
162174
}));
163175

164176
it('should update the ngModel value when selecting a button toggle', fakeAsync(() => {
165-
buttonToggleLabels[1].click();
177+
innerButtons[1].click();
166178
fixture.detectChanges();
167179

168180
tick();
@@ -175,8 +187,8 @@ describe('MatButtonToggle with forms', () => {
175187

176188
expect(groupElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
177189

178-
dispatchMouseEvent(buttonToggleLabels[0], 'mousedown');
179-
dispatchMouseEvent(buttonToggleLabels[0], 'mouseup');
190+
dispatchMouseEvent(innerButtons[0], 'mousedown');
191+
dispatchMouseEvent(innerButtons[0], 'mouseup');
180192

181193
expect(groupElement.querySelectorAll('.mat-ripple-element').length).toBe(1);
182194
});
@@ -189,8 +201,8 @@ describe('MatButtonToggle with forms', () => {
189201

190202
expect(groupElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
191203

192-
dispatchMouseEvent(buttonToggleLabels[0], 'mousedown');
193-
dispatchMouseEvent(buttonToggleLabels[0], 'mouseup');
204+
dispatchMouseEvent(innerButtons[0], 'mousedown');
205+
dispatchMouseEvent(innerButtons[0], 'mouseup');
194206

195207
expect(groupElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
196208
});
@@ -819,7 +831,10 @@ class ButtonTogglesInsideButtonToggleGroup {
819831

820832
@Component({
821833
template: `
822-
<mat-button-toggle-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
834+
<mat-button-toggle-group
835+
[name]="groupName"
836+
[(ngModel)]="modelValue"
837+
(change)="lastEvent = $event">
823838
<mat-button-toggle *ngFor="let option of options" [value]="option.value"
824839
[disableRipple]="disableRipple">
825840
{{option.label}}
@@ -828,6 +843,7 @@ class ButtonTogglesInsideButtonToggleGroup {
828843
`
829844
})
830845
class ButtonToggleGroupWithNgModel {
846+
groupName = 'group-name';
831847
modelValue: string;
832848
options = [
833849
{label: 'Red', value: 'red'},

‎src/lib/button-toggle/button-toggle.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
143143
this._name = value;
144144

145145
if (this._buttonToggles) {
146-
this._buttonToggles.forEach(toggle => toggle.name = this._name);
146+
this._buttonToggles.forEach(toggle => {
147+
toggle.name = this._name;
148+
toggle._markForCheck();
149+
});
147150
}
148151
}
149152
private _name = `mat-button-toggle-group-${_uniqueIdCounter++}`;

0 commit comments

Comments
 (0)
Please sign in to comment.