Skip to content

Commit 0389dcb

Browse files
committed
Make MdRadioButton OnPush
1 parent 4e4c6a6 commit 0389dcb

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

src/lib/radio/radio.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
AfterContentInit,
3+
ChangeDetectionStrategy,
4+
ChangeDetectorRef,
35
Component,
46
ContentChildren,
57
Directive,
@@ -126,14 +128,31 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
126128
}
127129

128130
/** Whether the labels should appear after or before the radio-buttons. Defaults to 'after' */
129-
@Input() labelPosition: 'before' | 'after' = 'after';
131+
_labelPosition: 'before' | 'after' = 'after';
132+
@Input()
133+
get labelPosition(): 'before' | 'after' {
134+
return this._labelPosition;
135+
}
136+
set labelPosition(v: 'before' | 'after') {
137+
this._labelPosition = v
138+
if (this._radios) {
139+
this._radios.forEach((radio) => {
140+
radio.labelPosition = this._labelPosition;
141+
});
142+
}
143+
}
130144

131145
/** Whether the radio button is disabled. */
132146
@Input()
133147
get disabled(): boolean { return this._disabled; }
134148
set disabled(value) {
135149
// The presence of *any* disabled value makes the component disabled, *except* for false.
136150
this._disabled = (value != null && value !== false) ? true : null;
151+
if (this._radios) {
152+
this._radios.forEach((radio) => {
153+
radio.disabled = this._disabled;
154+
})
155+
}
137156
}
138157

139158
/** Value of the radio button. */
@@ -266,6 +285,7 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
266285
host: {
267286
'[class.mat-radio-button]': 'true',
268287
}
288+
changeDetection: ChangeDetectionStrategy.OnPush,
269289
})
270290
export class MdRadioButton implements OnInit {
271291

@@ -285,10 +305,26 @@ export class MdRadioButton implements OnInit {
285305
name: string;
286306

287307
/** Used to set the 'aria-label' attribute on the underlying input element. */
288-
@Input('aria-label') ariaLabel: string;
308+
_ariaLabel: string;
309+
@Input('aria-label')
310+
get ariaLabel(): string {
311+
return this._ariaLabel;
312+
}
313+
set ariaLabel(value: string) {
314+
this._ariaLabel = value;
315+
this._change.markForCheck();
316+
}
289317

290318
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
291-
@Input('aria-labelledby') ariaLabelledby: string;
319+
_ariaLabelledby: string;
320+
@Input('aria-labelledby')
321+
get ariaLabelledby(): string {
322+
return this._ariaLabelledby;
323+
}
324+
set ariaLabelledby(value: string) {
325+
this._ariaLabelledby = value;
326+
this._change.markForCheck();
327+
}
292328

293329
/** Whether this radio is disabled. */
294330
private _disabled: boolean;
@@ -305,7 +341,10 @@ export class MdRadioButton implements OnInit {
305341
/** Whether the ripple effect for this radio button is disabled. */
306342
@Input()
307343
get disableRipple(): boolean { return this._disableRipple; }
308-
set disableRipple(value) { this._disableRipple = coerceBooleanProperty(value); }
344+
set disableRipple(value) {
345+
this._disableRipple = coerceBooleanProperty(value);
346+
this._change.markForCheck();
347+
}
309348

310349
/**
311350
* Event emitted when the checked state of this radio button changes.
@@ -321,6 +360,7 @@ export class MdRadioButton implements OnInit {
321360
constructor(@Optional() radioGroup: MdRadioGroup,
322361
private _elementRef: ElementRef,
323362
private _renderer: Renderer,
363+
private _change: ChangeDetectorRef,
324364
public radioDispatcher: UniqueSelectionDispatcher) {
325365
// Assertions. Ideally these should be stripped out by the compiler.
326366
// TODO(jelbourn): Assert that there's no name binding AND a parent radio group.
@@ -362,6 +402,7 @@ export class MdRadioButton implements OnInit {
362402
// Notify all radio buttons with the same name to un-check.
363403
this.radioDispatcher.notify(this.id, this.name);
364404
}
405+
this._change.markForCheck();
365406
}
366407
}
367408

@@ -383,7 +424,7 @@ export class MdRadioButton implements OnInit {
383424
this.radioGroup.selected = this;
384425
}
385426
}
386-
427+
this._change.markForCheck();
387428
}
388429
}
389430

@@ -400,6 +441,7 @@ export class MdRadioButton implements OnInit {
400441

401442
set align(v) {
402443
this.labelPosition = (v == 'start') ? 'after' : 'before';
444+
this._change.markForCheck();
403445
}
404446

405447
private _labelPosition: 'before' | 'after';
@@ -412,6 +454,7 @@ export class MdRadioButton implements OnInit {
412454

413455
set labelPosition(value) {
414456
this._labelPosition = value;
457+
this._change.markForCheck();
415458
}
416459

417460
/** Whether the radio button is disabled. */
@@ -424,6 +467,7 @@ export class MdRadioButton implements OnInit {
424467
set disabled(value: boolean) {
425468
// The presence of *any* disabled value makes the component disabled, *except* for false.
426469
this._disabled = (value != null && value !== false) ? true : null;
470+
this._change.markForCheck();
427471
}
428472

429473
ngOnInit() {

0 commit comments

Comments
 (0)