Skip to content

Commit

Permalink
fix(checkbox): Handle transition when unset checkbox is interacted with
Browse files Browse the repository at this point in the history
This commit fixes a bug where the initial `unchecked -> checked`
animation wasn't firing correctly when the checkbox was interacted with
from its initial state (e.g. when `[checked]` or `[(ngModel)]` isn't
bound to the component).

Fixes #183.

Closes #227
  • Loading branch information
traviskaufman authored and jelbourn committed Mar 25, 2016
1 parent 87e3a32 commit dde9359
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/components/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,29 @@ export function main() {
let el = fixture.debugElement.query(By.css('.md-checkbox'));
expect(el.nativeElement.className).not.toMatch(/^md\-checkbox\-anim/g);
});

describe('when interacted with from the initial state', function() {
beforeEach(function(done: any) {
builder.createAsync(CheckboxBarebonesController).then(function(f) {
fixture = f;
controller = fixture.componentInstance;
fixture.detectChanges();
}).then(done).catch(done.fail);
});

it('applies a transition class when checked', function() {
let el = fixture.debugElement.query(By.css('.md-checkbox'));
click(el, fixture);
expect(el.nativeElement.className).toContain('md-checkbox-anim-unchecked-checked');
});

it('applies a transition class when made indeterminate', function() {
let el = fixture.debugElement.query(By.css('.md-checkbox'));
controller.isIndeterminate = true;
fixture.detectChanges();
expect(el.nativeElement.className).toContain('md-checkbox-anim-unchecked-indeterminate');
});
});
});
});
}
Expand Down Expand Up @@ -783,3 +806,12 @@ class CheckboxFormcontrolController {
directives: [MdCheckbox]
})
class CheckboxEndAlignedController {}

@Component({
selector: 'checkbox-barebones-controller',
template: `<md-checkbox [indeterminate]="isIndeterminate"></md-checkbox>`,
directives: [MdCheckbox]
})
class CheckboxBarebonesController {
public isIndeterminate: boolean = false;
}
9 changes: 8 additions & 1 deletion src/components/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ export class MdCheckbox implements ControlValueAccessor {

switch (oldState) {
case TransitionCheckState.Init:
return '';
// Handle edge case where user interacts with checkbox that does not have [(ngModel)] or
// [checked] bound to it.
if (newState === TransitionCheckState.Checked) {
animSuffix = 'unchecked-checked';
} else {
return '';
}
break;
case TransitionCheckState.Unchecked:
animSuffix = newState === TransitionCheckState.Checked ?
'unchecked-checked' : 'unchecked-indeterminate';
Expand Down

0 comments on commit dde9359

Please sign in to comment.