Skip to content

Commit

Permalink
fix(checkbox): margin for empty checkboxes incorrectly added
Browse files Browse the repository at this point in the history
With angular#2121 the margin will be removed for checkboxes that don't have any label set.

A problem is that the Checkbox uses the OnPush change detection strategy and therefore the checkbox is not able to detect any delayed / async label change.

This means that checkboxes that set their label from an async binding will not have any margin until the users clicks on the checkbox.

Using the `cdkObserveContent` seems to be an elegant approach when using the OnPush strategy.

The `:empty` CSS selector would be more elegant but it's very sensitive about whitespaces and therefore it doesn't work properly.

Fixes angular#4720
  • Loading branch information
devversion committed May 23, 2017
1 parent 3569805 commit d3f954f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/lib/checkbox/checkbox.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<label class="mat-checkbox-layout" #label>
<div class="mat-checkbox-inner-container"
[class.mat-checkbox-inner-container-no-side-margin]="!_hasLabel()">
[class.mat-checkbox-inner-container-no-side-margin]="!_hasLabel">
<input #input
class="mat-checkbox-input cdk-visually-hidden" type="checkbox"
[id]="inputId"
Expand Down Expand Up @@ -35,7 +35,8 @@
<div class="mat-checkbox-mixedmark"></div>
</div>
</div>
<span class="mat-checkbox-label" #labelWrapper>
<span class="mat-checkbox-label" #userLabel
(cdkObserveContent)="_hasLabel = !!userLabel.textContent.trim()">
<ng-content></ng-content>
</span>
</label>
11 changes: 3 additions & 8 deletions src/lib/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,12 @@ export class MdCheckbox extends _MdCheckboxMixinBase
/** The native `<input type="checkbox"> element */
@ViewChild('input') _inputElement: ElementRef;

@ViewChild('labelWrapper') _labelWrapper: ElementRef;

/** Whether the checkbox has label */
_hasLabel(): boolean {
const labelText = this._labelWrapper.nativeElement.textContent || '';
return !!labelText.trim().length;
}

/** Called when the checkbox is blurred. Needed to properly implement ControlValueAccessor. */
@ViewChild(MdRipple) _ripple: MdRipple;

/** Whether the checkbox has a label set or not. */
_hasLabel: boolean = false;

/**
* Called when the checkbox is blurred. Needed to properly implement ControlValueAccessor.
* @docs-private
Expand Down
4 changes: 2 additions & 2 deletions src/lib/checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MdRippleModule, MdCommonModule, FocusOriginMonitor} from '../core';
import {MdRippleModule, MdCommonModule, FocusOriginMonitor, ObserveContentModule} from '../core';
import {MdCheckbox} from './checkbox';


@NgModule({
imports: [CommonModule, MdRippleModule, MdCommonModule],
imports: [CommonModule, MdRippleModule, MdCommonModule, ObserveContentModule],
exports: [MdCheckbox, MdCommonModule],
declarations: [MdCheckbox],
providers: [FocusOriginMonitor]
Expand Down

0 comments on commit d3f954f

Please sign in to comment.