Skip to content

Commit 4e8d806

Browse files
belevmmalerba
authored andcommitted
fix(checkbox): no side margin if label has no content (#2121)
* Remove side margin in checkbox if label has no content. * Use `checkboxNativeElement` instead of `checkboxDebugElement.nativeElement` in expect statements. * Fix typo. Closes #2011
1 parent 3560ada commit 4e8d806

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

src/lib/checkbox/checkbox.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<label class="mat-checkbox-layout" #label>
2-
<div class="mat-checkbox-inner-container">
2+
<div class="mat-checkbox-inner-container"
3+
[class.mat-checkbox-inner-container-no-side-margin]="!_hasLabel()">
34
<input #input
45
class="mat-checkbox-input cdk-visually-hidden" type="checkbox"
56
[id]="inputId"
@@ -34,7 +35,7 @@
3435
<div class="mat-checkbox-mixedmark"></div>
3536
</div>
3637
</div>
37-
<span class="mat-checkbox-label">
38+
<span class="mat-checkbox-label" #labelWrapper>
3839
<ng-content></ng-content>
3940
</span>
4041
</label>

src/lib/checkbox/checkbox.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ $_mat-checkbox-mark-stroke-size: 2 / 15 * $mat-checkbox-size !default;
228228
}
229229
}
230230

231+
.mat-checkbox-inner-container-no-side-margin {
232+
margin: {
233+
left: 0;
234+
right: 0;
235+
}
236+
}
237+
231238
// TODO(kara): Remove this style when fixing vertical baseline
232239
.mat-checkbox-layout .mat-checkbox-label {
233240
line-height: 24px;

src/lib/checkbox/checkbox.spec.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('MdCheckbox', () => {
3232
CheckboxWithNameAttribute,
3333
CheckboxWithChangeEvent,
3434
CheckboxWithFormControl,
35+
CheckboxWithoutLabel,
3536
],
3637
providers: [
3738
{provide: ViewportRuler, useClass: FakeViewportRuler}
@@ -436,28 +437,28 @@ describe('MdCheckbox', () => {
436437
it('should apply class based on color attribute', () => {
437438
testComponent.checkboxColor = 'primary';
438439
fixture.detectChanges();
439-
expect(checkboxDebugElement.nativeElement.classList.contains('mat-primary')).toBe(true);
440+
expect(checkboxNativeElement.classList.contains('mat-primary')).toBe(true);
440441

441442
testComponent.checkboxColor = 'accent';
442443
fixture.detectChanges();
443-
expect(checkboxDebugElement.nativeElement.classList.contains('mat-accent')).toBe(true);
444+
expect(checkboxNativeElement.classList.contains('mat-accent')).toBe(true);
444445
});
445446

446447
it('should should not clear previous defined classes', () => {
447-
checkboxDebugElement.nativeElement.classList.add('custom-class');
448+
checkboxNativeElement.classList.add('custom-class');
448449

449450
testComponent.checkboxColor = 'primary';
450451
fixture.detectChanges();
451452

452-
expect(checkboxDebugElement.nativeElement.classList.contains('mat-primary')).toBe(true);
453-
expect(checkboxDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
453+
expect(checkboxNativeElement.classList.contains('mat-primary')).toBe(true);
454+
expect(checkboxNativeElement.classList.contains('custom-class')).toBe(true);
454455

455456
testComponent.checkboxColor = 'accent';
456457
fixture.detectChanges();
457458

458-
expect(checkboxDebugElement.nativeElement.classList.contains('mat-primary')).toBe(false);
459-
expect(checkboxDebugElement.nativeElement.classList.contains('mat-accent')).toBe(true);
460-
expect(checkboxDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
459+
expect(checkboxNativeElement.classList.contains('mat-primary')).toBe(false);
460+
expect(checkboxNativeElement.classList.contains('mat-accent')).toBe(true);
461+
expect(checkboxNativeElement.classList.contains('custom-class')).toBe(true);
461462

462463
});
463464
});
@@ -730,7 +731,6 @@ describe('MdCheckbox', () => {
730731
});
731732
});
732733

733-
734734
describe('with form control', () => {
735735
let checkboxDebugElement: DebugElement;
736736
let checkboxInstance: MdCheckbox;
@@ -763,6 +763,22 @@ describe('MdCheckbox', () => {
763763
expect(inputElement.disabled).toBe(false);
764764
});
765765
});
766+
767+
describe('without label', () => {
768+
let checkboxDebugElement: DebugElement;
769+
let checkboxNativeElement: HTMLElement;
770+
771+
it('should add a css class to inner-container to remove side margin', () => {
772+
fixture = TestBed.createComponent(CheckboxWithoutLabel);
773+
fixture.detectChanges();
774+
checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox));
775+
checkboxNativeElement = checkboxDebugElement.nativeElement;
776+
777+
let checkboxInnerContainerWithoutMarginCount = checkboxNativeElement
778+
.querySelectorAll('.mat-checkbox-inner-container-no-side-margin').length;
779+
expect(checkboxInnerContainerWithoutMarginCount).toBe(1);
780+
});
781+
});
766782
});
767783

768784
/** Simple component for testing a single checkbox. */
@@ -872,3 +888,9 @@ class CheckboxWithChangeEvent {
872888
class CheckboxWithFormControl {
873889
formControl = new FormControl();
874890
}
891+
892+
/** Test component without label */
893+
@Component({
894+
template: `<md-checkbox></md-checkbox>`
895+
})
896+
class CheckboxWithoutLabel {}

src/lib/checkbox/checkbox.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ export class MdCheckbox extends _MdCheckboxMixinBase
158158
/** The native `<input type="checkbox"> element */
159159
@ViewChild('input') _inputElement: ElementRef;
160160

161+
@ViewChild('labelWrapper') _labelWrapper: ElementRef;
162+
163+
/** Whether the checkbox has label */
164+
_hasLabel(): boolean {
165+
const labelText = this._labelWrapper.nativeElement.textContent || '';
166+
return !!labelText.trim().length;
167+
}
168+
169+
/** Called when the checkbox is blurred. Needed to properly implement ControlValueAccessor. */
161170
@ViewChild(MdRipple) _ripple: MdRipple;
162171

163172
/**

0 commit comments

Comments
 (0)