Skip to content

Commit

Permalink
fix(button-toggle): add the setDisabledState from ControlValueAccessor (
Browse files Browse the repository at this point in the history
#2430)

Adds the `setDisabledState` method from the `ControlValueAccessor` interface, which allows the control to be disabled when it is used with the `ReactiveFormsModule`. Also adds a couple of unit tests for the other `ControlValueAccessor` methods.
  • Loading branch information
crisbeto authored and tinayuangao committed Jan 10, 2017
1 parent d4ab3d3 commit fb750b4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/lib/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ComponentFixture,
TestBed,
} from '@angular/core/testing';
import {NgControl, FormsModule} from '@angular/forms';
import {NgControl, FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {
Expand All @@ -20,12 +20,13 @@ describe('MdButtonToggle', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdButtonToggleModule.forRoot(), FormsModule],
imports: [MdButtonToggleModule.forRoot(), FormsModule, ReactiveFormsModule],
declarations: [
ButtonTogglesInsideButtonToggleGroup,
ButtonToggleGroupWithNgModel,
ButtonTogglesInsideButtonToggleGroupMultiple,
ButtonToggleGroupWithInitialValue,
ButtonToggleGroupWithFormControl,
StandaloneButtonToggle,
],
});
Expand Down Expand Up @@ -464,6 +465,52 @@ describe('MdButtonToggle', () => {

});

describe('using FormControl', () => {
let fixture: ComponentFixture<ButtonToggleGroupWithFormControl>;
let groupDebugElement: DebugElement;
let groupInstance: MdButtonToggleGroup;
let testComponent: ButtonToggleGroupWithFormControl;

beforeEach(async(() => {
fixture = TestBed.createComponent(ButtonToggleGroupWithFormControl);
fixture.detectChanges();

testComponent = fixture.debugElement.componentInstance;

groupDebugElement = fixture.debugElement.query(By.directive(MdButtonToggleGroup));
groupInstance = groupDebugElement.injector.get(MdButtonToggleGroup);
}));

it('should toggle the disabled state', () => {
testComponent.control.disable();

expect(groupInstance.disabled).toBe(true);

testComponent.control.enable();

expect(groupInstance.disabled).toBe(false);
});

it('should set the value', () => {
testComponent.control.setValue('green');

expect(groupInstance.value).toBe('green');

testComponent.control.setValue('red');

expect(groupInstance.value).toBe('red');
});

it('should register the on change callback', () => {
let spy = jasmine.createSpy('onChange callback');

testComponent.control.registerOnChange(spy);
testComponent.control.setValue('blue');

expect(spy).toHaveBeenCalled();
});
});

describe('as standalone', () => {
let fixture: ComponentFixture<StandaloneButtonToggle>;
let buttonToggleDebugElement: DebugElement;
Expand Down Expand Up @@ -598,3 +645,16 @@ class StandaloneButtonToggle { }
class ButtonToggleGroupWithInitialValue {
lastEvent: MdButtonToggleChange;
}

@Component({
template: `
<md-button-toggle-group [formControl]="control">
<md-button-toggle value="red">Value Red</md-button-toggle>
<md-button-toggle value="green">Value Green</md-button-toggle>
<md-button-toggle value="blue">Value Blue</md-button-toggle>
</md-button-toggle-group>
`
})
class ButtonToggleGroupWithFormControl {
control = new FormControl();
}
8 changes: 8 additions & 0 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
registerOnTouched(fn: any) {
this.onTouched = fn;
}

/**
* Toggles the disabled state of the component. Implemented as part of ControlValueAccessor.
* @param isDisabled Whether the component should be disabled.
*/
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
}

/** Multiple selection button-toggle group. `ngModel` is not supported in this mode. */
Expand Down

0 comments on commit fb750b4

Please sign in to comment.