Skip to content
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

fix(button-toggle): toggle group should not emit an initial change event. #1144

Merged
merged 4 commits into from
Sep 2, 2016
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: 36 additions & 0 deletions src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('MdButtonToggle', () => {
ButtonTogglesInsideButtonToggleGroup,
ButtonToggleGroupWithNgModel,
ButtonTogglesInsideButtonToggleGroupMultiple,
ButtonToggleGroupWithInitialValue,
StandaloneButtonToggle,
],
});
Expand Down Expand Up @@ -320,6 +321,29 @@ describe('MdButtonToggle', () => {
expect(testComponent.modelValue).toBe('red');
expect(testComponent.lastEvent.value).toBe('red');
}));

});

describe('with initial value and change event', () => {

it('should not fire an initial change event', async(() => {
let fixture = TestBed.createComponent(ButtonToggleGroupWithInitialValue);
let testComponent = fixture.debugElement.componentInstance;
let groupDebugElement = fixture.debugElement.query(By.directive(MdButtonToggleGroup));
let groupInstance: MdButtonToggleGroup = groupDebugElement.injector.get(MdButtonToggleGroup);

fixture.detectChanges();

expect(groupInstance.value).toBe('red');
expect(testComponent.lastEvent).toBeFalsy();

groupInstance.value = 'green';
fixture.detectChanges();

expect(groupInstance.value).toBe('green');
expect(testComponent.lastEvent.value).toBe('green');
}));

});

describe('inside of a multiple selection group', () => {
Expand Down Expand Up @@ -532,3 +556,15 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
`
})
class StandaloneButtonToggle { }

@Component({
template: `
<md-button-toggle-group (change)="lastEvent = $event" value="red">
<md-button-toggle value="red">Value Red</md-button-toggle>
<md-button-toggle value="green">Value Green</md-button-toggle>
</md-button-toggle-group>
`
})
class ButtonToggleGroupWithInitialValue {
lastEvent: MdButtonToggleChange;
}
20 changes: 17 additions & 3 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Output,
QueryList,
ViewEncapsulation,
forwardRef
forwardRef,
AfterViewInit
} from '@angular/core';
import {
NG_VALUE_ACCESSOR,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class MdButtonToggleChange {
'role': 'radiogroup',
},
})
export class MdButtonToggleGroup implements ControlValueAccessor {
export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor {
/** The value for the button toggle group. Should match currently selected button toggle. */
private _value: any = null;

Expand All @@ -68,6 +69,9 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
/** The currently selected button toggle, should match the value. */
private _selected: MdButtonToggle = null;

/** Whether the button toggle group is initialized or not. */
private _isInitialized: boolean = false;

/** The method to be called in order to update ngModel. */
private _controlValueAccessorChangeFn: (value: any) => void = (value) => {};

Expand All @@ -84,6 +88,11 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
@ContentChildren(forwardRef(() => MdButtonToggle))
_buttonToggles: QueryList<MdButtonToggle> = null;

/** TODO: internal */
ngAfterViewInit() {
this._isInitialized = true;
}

@Input()
get name(): string {
return this._name;
Expand Down Expand Up @@ -114,7 +123,12 @@ export class MdButtonToggleGroup implements ControlValueAccessor {
this._value = newValue;

this._updateSelectedButtonToggleFromValue();
this._emitChangeEvent();

// Only emit a change event if the view is completely initialized.
// We don't want to emit a change event for the initial values.
if (this._isInitialized) {
this._emitChangeEvent();
}
}
}

Expand Down