Skip to content

fix(button): not updating DOM node name if group name changes #14963

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

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('MatButtonToggle with forms', () => {
let buttonToggleInstances: MatButtonToggle[];
let testComponent: ButtonToggleGroupWithNgModel;
let groupNgModel: NgModel;
let buttonToggleLabels: HTMLElement[];
let innerButtons: HTMLElement[];

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

buttonToggleDebugElements = fixture.debugElement.queryAll(By.directive(MatButtonToggle));
buttonToggleInstances = buttonToggleDebugElements.map(debugEl => debugEl.componentInstance);
buttonToggleLabels = buttonToggleDebugElements.map(
innerButtons = buttonToggleDebugElements.map(
debugEl => debugEl.query(By.css('button')).nativeElement);

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

buttonToggleLabels[0].click();
innerButtons[0].click();
fixture.detectChanges();

tick();
Expand All @@ -122,6 +122,18 @@ describe('MatButtonToggle with forms', () => {
}
});

it('should update the name of radio DOM elements if the name of the group changes', () => {
expect(innerButtons.every(button => button.getAttribute('name') === groupInstance.name))
.toBe(true, 'Expected all buttons to have the initial name.');

fixture.componentInstance.groupName = 'changed-name';
fixture.detectChanges();

expect(groupInstance.name).toBe('changed-name');
expect(innerButtons.every(button => button.getAttribute('name') === groupInstance.name))
.toBe(true, 'Expected all buttons to have the new name.');
});

it('should check the corresponding button toggle on a group value change', () => {
expect(groupInstance.value).toBeFalsy();
for (let buttonToggle of buttonToggleInstances) {
Expand Down Expand Up @@ -152,7 +164,7 @@ describe('MatButtonToggle with forms', () => {
expect(groupNgModel.pristine).toBe(true);
expect(groupNgModel.touched).toBe(false);

buttonToggleLabels[2].click();
innerButtons[2].click();
fixture.detectChanges();
tick();

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

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

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

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

dispatchMouseEvent(buttonToggleLabels[0], 'mousedown');
dispatchMouseEvent(buttonToggleLabels[0], 'mouseup');
dispatchMouseEvent(innerButtons[0], 'mousedown');
dispatchMouseEvent(innerButtons[0], 'mouseup');

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

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

dispatchMouseEvent(buttonToggleLabels[0], 'mousedown');
dispatchMouseEvent(buttonToggleLabels[0], 'mouseup');
dispatchMouseEvent(innerButtons[0], 'mousedown');
dispatchMouseEvent(innerButtons[0], 'mouseup');

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

@Component({
template: `
<mat-button-toggle-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
<mat-button-toggle-group
[name]="groupName"
[(ngModel)]="modelValue"
(change)="lastEvent = $event">
<mat-button-toggle *ngFor="let option of options" [value]="option.value"
[disableRipple]="disableRipple">
{{option.label}}
Expand All @@ -828,6 +843,7 @@ class ButtonTogglesInsideButtonToggleGroup {
`
})
class ButtonToggleGroupWithNgModel {
groupName = 'group-name';
modelValue: string;
options = [
{label: 'Red', value: 'red'},
Expand Down
5 changes: 4 additions & 1 deletion src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
this._name = value;

if (this._buttonToggles) {
this._buttonToggles.forEach(toggle => toggle.name = this._name);
this._buttonToggles.forEach(toggle => {
toggle.name = this._name;
toggle._markForCheck();
});
}
}
private _name = `mat-button-toggle-group-${_uniqueIdCounter++}`;
Expand Down