Skip to content

Commit 05ad237

Browse files
committed
Make MdRadioButton OnPush
1 parent 4ab6f30 commit 05ad237

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

src/lib/radio/radio.ts

Lines changed: 50 additions & 6 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,
@@ -125,14 +127,31 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
125127
}
126128

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

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

138157
/** Value of the radio button. */
@@ -261,7 +280,8 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
261280
selector: 'md-radio-button, mat-radio-button',
262281
templateUrl: 'radio.html',
263282
styleUrls: ['radio.css'],
264-
encapsulation: ViewEncapsulation.None
283+
encapsulation: ViewEncapsulation.None,
284+
changeDetection: ChangeDetectionStrategy.OnPush,
265285
})
266286
export class MdRadioButton implements OnInit {
267287

@@ -281,10 +301,26 @@ export class MdRadioButton implements OnInit {
281301
name: string;
282302

283303
/** Used to set the 'aria-label' attribute on the underlying input element. */
284-
@Input('aria-label') ariaLabel: string;
304+
_ariaLabel: string;
305+
@Input('aria-label')
306+
get ariaLabel(): string {
307+
return this._ariaLabel;
308+
}
309+
set ariaLabel(value: string) {
310+
this._ariaLabel = value;
311+
this._change.markForCheck();
312+
}
285313

286314
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
287-
@Input('aria-labelledby') ariaLabelledby: string;
315+
_ariaLabelledby: string;
316+
@Input('aria-labelledby')
317+
get ariaLabelledby(): string {
318+
return this._ariaLabelledby;
319+
}
320+
set ariaLabelledby(value: string) {
321+
this._ariaLabelledby = value;
322+
this._change.markForCheck();
323+
}
288324

289325
/** Whether this radio is disabled. */
290326
private _disabled: boolean;
@@ -301,7 +337,10 @@ export class MdRadioButton implements OnInit {
301337
/** Whether the ripple effect for this radio button is disabled. */
302338
@Input()
303339
get disableRipple(): boolean { return this._disableRipple; }
304-
set disableRipple(value) { this._disableRipple = coerceBooleanProperty(value); }
340+
set disableRipple(value) {
341+
this._disableRipple = coerceBooleanProperty(value);
342+
this._change.markForCheck();
343+
}
305344

306345
/**
307346
* Event emitted when the checked state of this radio button changes.
@@ -317,6 +356,7 @@ export class MdRadioButton implements OnInit {
317356
constructor(@Optional() radioGroup: MdRadioGroup,
318357
private _elementRef: ElementRef,
319358
private _renderer: Renderer,
359+
private _change: ChangeDetectorRef,
320360
public radioDispatcher: UniqueSelectionDispatcher) {
321361
// Assertions. Ideally these should be stripped out by the compiler.
322362
// TODO(jelbourn): Assert that there's no name binding AND a parent radio group.
@@ -358,6 +398,7 @@ export class MdRadioButton implements OnInit {
358398
// Notify all radio buttons with the same name to un-check.
359399
this.radioDispatcher.notify(this.id, this.name);
360400
}
401+
this._change.markForCheck();
361402
}
362403
}
363404

@@ -379,7 +420,7 @@ export class MdRadioButton implements OnInit {
379420
this.radioGroup.selected = this;
380421
}
381422
}
382-
423+
this._change.markForCheck();
383424
}
384425
}
385426

@@ -396,6 +437,7 @@ export class MdRadioButton implements OnInit {
396437

397438
set align(v) {
398439
this.labelPosition = (v == 'start') ? 'after' : 'before';
440+
this._change.markForCheck();
399441
}
400442

401443
private _labelPosition: 'before' | 'after';
@@ -408,6 +450,7 @@ export class MdRadioButton implements OnInit {
408450

409451
set labelPosition(value) {
410452
this._labelPosition = value;
453+
this._change.markForCheck();
411454
}
412455

413456
/** Whether the radio button is disabled. */
@@ -420,6 +463,7 @@ export class MdRadioButton implements OnInit {
420463
set disabled(value: boolean) {
421464
// The presence of *any* disabled value makes the component disabled, *except* for false.
422465
this._disabled = (value != null && value !== false) ? true : null;
466+
this._change.markForCheck();
423467
}
424468

425469
ngOnInit() {

0 commit comments

Comments
 (0)