Skip to content

Commit dde9359

Browse files
traviskaufmanjelbourn
authored andcommitted
fix(checkbox): Handle transition when unset checkbox is interacted with
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
1 parent 87e3a32 commit dde9359

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/components/checkbox/checkbox.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,29 @@ export function main() {
621621
let el = fixture.debugElement.query(By.css('.md-checkbox'));
622622
expect(el.nativeElement.className).not.toMatch(/^md\-checkbox\-anim/g);
623623
});
624+
625+
describe('when interacted with from the initial state', function() {
626+
beforeEach(function(done: any) {
627+
builder.createAsync(CheckboxBarebonesController).then(function(f) {
628+
fixture = f;
629+
controller = fixture.componentInstance;
630+
fixture.detectChanges();
631+
}).then(done).catch(done.fail);
632+
});
633+
634+
it('applies a transition class when checked', function() {
635+
let el = fixture.debugElement.query(By.css('.md-checkbox'));
636+
click(el, fixture);
637+
expect(el.nativeElement.className).toContain('md-checkbox-anim-unchecked-checked');
638+
});
639+
640+
it('applies a transition class when made indeterminate', function() {
641+
let el = fixture.debugElement.query(By.css('.md-checkbox'));
642+
controller.isIndeterminate = true;
643+
fixture.detectChanges();
644+
expect(el.nativeElement.className).toContain('md-checkbox-anim-unchecked-indeterminate');
645+
});
646+
});
624647
});
625648
});
626649
}
@@ -783,3 +806,12 @@ class CheckboxFormcontrolController {
783806
directives: [MdCheckbox]
784807
})
785808
class CheckboxEndAlignedController {}
809+
810+
@Component({
811+
selector: 'checkbox-barebones-controller',
812+
template: `<md-checkbox [indeterminate]="isIndeterminate"></md-checkbox>`,
813+
directives: [MdCheckbox]
814+
})
815+
class CheckboxBarebonesController {
816+
public isIndeterminate: boolean = false;
817+
}

src/components/checkbox/checkbox.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,14 @@ export class MdCheckbox implements ControlValueAccessor {
246246

247247
switch (oldState) {
248248
case TransitionCheckState.Init:
249-
return '';
249+
// Handle edge case where user interacts with checkbox that does not have [(ngModel)] or
250+
// [checked] bound to it.
251+
if (newState === TransitionCheckState.Checked) {
252+
animSuffix = 'unchecked-checked';
253+
} else {
254+
return '';
255+
}
256+
break;
250257
case TransitionCheckState.Unchecked:
251258
animSuffix = newState === TransitionCheckState.Checked ?
252259
'unchecked-checked' : 'unchecked-indeterminate';

0 commit comments

Comments
 (0)