Skip to content

Commit e5830d1

Browse files
devversionkara
authored andcommitted
fix(button-toggle): toggle group should not emit an initial change event. (#1144)
1 parent 0390bd5 commit e5830d1

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('MdButtonToggle', () => {
2525
ButtonTogglesInsideButtonToggleGroup,
2626
ButtonToggleGroupWithNgModel,
2727
ButtonTogglesInsideButtonToggleGroupMultiple,
28+
ButtonToggleGroupWithInitialValue,
2829
StandaloneButtonToggle,
2930
],
3031
});
@@ -320,6 +321,29 @@ describe('MdButtonToggle', () => {
320321
expect(testComponent.modelValue).toBe('red');
321322
expect(testComponent.lastEvent.value).toBe('red');
322323
}));
324+
325+
});
326+
327+
describe('with initial value and change event', () => {
328+
329+
it('should not fire an initial change event', async(() => {
330+
let fixture = TestBed.createComponent(ButtonToggleGroupWithInitialValue);
331+
let testComponent = fixture.debugElement.componentInstance;
332+
let groupDebugElement = fixture.debugElement.query(By.directive(MdButtonToggleGroup));
333+
let groupInstance: MdButtonToggleGroup = groupDebugElement.injector.get(MdButtonToggleGroup);
334+
335+
fixture.detectChanges();
336+
337+
expect(groupInstance.value).toBe('red');
338+
expect(testComponent.lastEvent).toBeFalsy();
339+
340+
groupInstance.value = 'green';
341+
fixture.detectChanges();
342+
343+
expect(groupInstance.value).toBe('green');
344+
expect(testComponent.lastEvent.value).toBe('green');
345+
}));
346+
323347
});
324348

325349
describe('inside of a multiple selection group', () => {
@@ -532,3 +556,15 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
532556
`
533557
})
534558
class StandaloneButtonToggle { }
559+
560+
@Component({
561+
template: `
562+
<md-button-toggle-group (change)="lastEvent = $event" value="red">
563+
<md-button-toggle value="red">Value Red</md-button-toggle>
564+
<md-button-toggle value="green">Value Green</md-button-toggle>
565+
</md-button-toggle-group>
566+
`
567+
})
568+
class ButtonToggleGroupWithInitialValue {
569+
lastEvent: MdButtonToggleChange;
570+
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
Output,
1313
QueryList,
1414
ViewEncapsulation,
15-
forwardRef
15+
forwardRef,
16+
AfterViewInit
1617
} from '@angular/core';
1718
import {
1819
NG_VALUE_ACCESSOR,
@@ -52,7 +53,7 @@ export class MdButtonToggleChange {
5253
'role': 'radiogroup',
5354
},
5455
})
55-
export class MdButtonToggleGroup implements ControlValueAccessor {
56+
export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor {
5657
/** The value for the button toggle group. Should match currently selected button toggle. */
5758
private _value: any = null;
5859

@@ -65,6 +66,9 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
6566
/** The currently selected button toggle, should match the value. */
6667
private _selected: MdButtonToggle = null;
6768

69+
/** Whether the button toggle group is initialized or not. */
70+
private _isInitialized: boolean = false;
71+
6872
/** The method to be called in order to update ngModel. */
6973
private _controlValueAccessorChangeFn: (value: any) => void = (value) => {};
7074

@@ -81,6 +85,11 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
8185
@ContentChildren(forwardRef(() => MdButtonToggle))
8286
_buttonToggles: QueryList<MdButtonToggle> = null;
8387

88+
/** TODO: internal */
89+
ngAfterViewInit() {
90+
this._isInitialized = true;
91+
}
92+
8493
@Input()
8594
get name(): string {
8695
return this._name;
@@ -111,7 +120,12 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
111120
this._value = newValue;
112121

113122
this._updateSelectedButtonToggleFromValue();
114-
this._emitChangeEvent();
123+
124+
// Only emit a change event if the view is completely initialized.
125+
// We don't want to emit a change event for the initial values.
126+
if (this._isInitialized) {
127+
this._emitChangeEvent();
128+
}
115129
}
116130
}
117131

0 commit comments

Comments
 (0)