Skip to content

Commit 0bc14f0

Browse files
committed
Updates
1 parent 4f54eab commit 0bc14f0

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

src/lib/checkbox/checkbox.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<label [attr.for]="inputId" class="mat-checkbox-layout" #label>
22
<div class="mat-checkbox-inner-container"
3-
[class.mat-checkbox-inner-container-no-side-margin]="!_hasLabel">
3+
[class.mat-checkbox-inner-container-no-side-margin]="!checkboxLabel.textContent.trim()">
44
<input #input
55
class="mat-checkbox-input cdk-visually-hidden" type="checkbox"
66
[id]="inputId"
@@ -35,8 +35,7 @@
3535
<div class="mat-checkbox-mixedmark"></div>
3636
</div>
3737
</div>
38-
<span class="mat-checkbox-label" #userLabel
39-
(cdkObserveContent)="_hasLabel = !!userLabel.textContent.trim()">
38+
<span class="mat-checkbox-label" #checkboxLabel (cdkObserveContent)="_onLabelTextChange()">
4039
<!-- Add an invisible span so JAWS can read the label -->
4140
<span style="display:none">&nbsp;</span>
4241
<ng-content></ng-content>

src/lib/checkbox/checkbox.spec.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -765,19 +765,57 @@ describe('MdCheckbox', () => {
765765
});
766766

767767
describe('without label', () => {
768-
let checkboxDebugElement: DebugElement;
769-
let checkboxNativeElement: HTMLElement;
768+
let testComponent: CheckboxWithoutLabel;
769+
let checkboxElement: HTMLElement;
770+
let checkboxInnerContainer: HTMLElement;
770771

771-
it('should add a css class to inner-container to remove side margin', () => {
772+
beforeEach(() => {
772773
fixture = TestBed.createComponent(CheckboxWithoutLabel);
774+
775+
const checkboxDebugEl = fixture.debugElement.query(By.directive(MdCheckbox));
776+
777+
testComponent = fixture.componentInstance;
778+
checkboxElement = checkboxDebugEl.nativeElement;
779+
checkboxInnerContainer = checkboxDebugEl
780+
.query(By.css('.mat-checkbox-inner-container')).nativeElement;
781+
});
782+
783+
it('should remove margin for checkbox without a label', () => {
773784
fixture.detectChanges();
774-
checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox));
775-
checkboxNativeElement = checkboxDebugElement.nativeElement;
776785

777-
let checkboxInnerContainerWithoutMarginCount = checkboxNativeElement
778-
.querySelectorAll('.mat-checkbox-inner-container-no-side-margin').length;
779-
expect(checkboxInnerContainerWithoutMarginCount).toBe(1);
786+
expect(checkboxInnerContainer.classList)
787+
.toContain('mat-checkbox-inner-container-no-side-margin');
780788
});
789+
790+
it('should not remove margin if initial label is set through binding', async(() => {
791+
testComponent.label = 'Some content';
792+
fixture.detectChanges();
793+
794+
expect(checkboxInnerContainer.classList)
795+
.not.toContain('mat-checkbox-inner-container-no-side-margin');
796+
}));
797+
798+
it('should re-add margin if label is added asynchronously', async(() => {
799+
fixture.detectChanges();
800+
801+
expect(checkboxInnerContainer.classList)
802+
.toContain('mat-checkbox-inner-container-no-side-margin');
803+
804+
testComponent.label = 'Some content';
805+
fixture.detectChanges();
806+
807+
// Wait for the MutationObserver to detect the content change and for the cdkObserveContent
808+
// to emit the change event to the checkbox.
809+
setTimeout(() => {
810+
// The MutationObserver from the cdkObserveContent directive detected the content change
811+
// and notified the checkbox component. The checkbox then marks the component as dirty
812+
// by calling `markForCheck()`. This needs to be reflected by the component template then.
813+
fixture.detectChanges();
814+
815+
expect(checkboxInnerContainer.classList)
816+
.not.toContain('mat-checkbox-inner-container-no-side-margin');
817+
}, 1);
818+
}));
781819
});
782820
});
783821

@@ -889,6 +927,8 @@ class CheckboxWithFormControl {
889927

890928
/** Test component without label */
891929
@Component({
892-
template: `<md-checkbox></md-checkbox>`
930+
template: `<md-checkbox>{{ label }}</md-checkbox>`
893931
})
894-
class CheckboxWithoutLabel {}
932+
class CheckboxWithoutLabel {
933+
label: string;
934+
}

src/lib/checkbox/checkbox.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ export class MdCheckbox extends _MdCheckboxMixinBase
161161
/** Called when the checkbox is blurred. Needed to properly implement ControlValueAccessor. */
162162
@ViewChild(MdRipple) _ripple: MdRipple;
163163

164-
/** Whether the checkbox has a label set or not. */
165-
_hasLabel: boolean = false;
166-
167164
/**
168165
* Called when the checkbox is blurred. Needed to properly implement ControlValueAccessor.
169166
* @docs-private
@@ -267,6 +264,14 @@ export class MdCheckbox extends _MdCheckboxMixinBase
267264
return this.disableRipple || this.disabled;
268265
}
269266

267+
/** Method being called whenever the label text changes. */
268+
_onLabelTextChange() {
269+
// This method is getting called whenever the label of the checkbox changes.
270+
// Since the checkbox uses the OnPush strategy we need to notify it about the change
271+
// that has been recognized by the cdkObserveContent directive.
272+
this._changeDetectorRef.markForCheck();
273+
}
274+
270275
/**
271276
* Sets the model value. Implemented as part of ControlValueAccessor.
272277
* @param value Value to be set to the model.

0 commit comments

Comments
 (0)