-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(radio): Make MdRadioButton change detection strategy OnPush #2526
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e73ad6b
Make MdRadioButton OnPush
tinayuangao 44ef4c0
.
tinayuangao 7026609
Fixed tests
tinayuangao 6691443
fixed group ripple disabled test
tinayuangao 7b0cfec
fix lint
tinayuangao 96fb7d7
fix test
tinayuangao e1a3618
sync and fix test
tinayuangao 0e4dfdc
address comments
tinayuangao f7ec787
address comments
tinayuangao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { | ||
AfterContentInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
ContentChildren, | ||
Directive, | ||
|
@@ -86,6 +88,12 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
/** Whether the `value` has been set to its initial value. */ | ||
private _isInitialized: boolean = false; | ||
|
||
/** Whether the labels should appear after or before the radio-buttons. Defaults to 'after' */ | ||
private _labelPosition: 'before' | 'after' = 'after'; | ||
|
||
/** Whether the radio group is disabled. */ | ||
private _disabled: boolean = false; | ||
|
||
/** The method to be called in order to update ngModel */ | ||
_controlValueAccessorChangeFn: (value: any) => void = (value) => {}; | ||
|
||
|
@@ -129,8 +137,17 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
this.labelPosition = (v == 'start') ? 'after' : 'before'; | ||
} | ||
|
||
|
||
/** Whether the labels should appear after or before the radio-buttons. Defaults to 'after' */ | ||
@Input() labelPosition: 'before' | 'after' = 'after'; | ||
@Input() | ||
get labelPosition() { | ||
return this._labelPosition; | ||
} | ||
|
||
set labelPosition(v) { | ||
this._labelPosition = (v == 'before') ? 'before' : 'after'; | ||
this._markRadiosForCheck(); | ||
} | ||
|
||
/** Value of the radio button. */ | ||
@Input() | ||
|
@@ -160,6 +177,21 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
this._checkSelectedRadioButton(); | ||
} | ||
|
||
/** Whether the radio group is diabled */ | ||
@Input() | ||
get disabled() { return this._disabled; } | ||
set disabled(value) { | ||
this._disabled = value; | ||
if (this._radios) { | ||
// Update radios disabled state | ||
this._radios.forEach((r) => r._markForCheck()); | ||
} | ||
} | ||
|
||
constructor(private _change: ChangeDetectorRef) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call this |
||
super(); | ||
} | ||
|
||
/** | ||
* Initialize properties once content children are available. | ||
* This allows us to propagate relevant attributes to associated buttons. | ||
|
@@ -215,12 +247,19 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
} | ||
} | ||
|
||
_markRadiosForCheck() { | ||
if (this._radios) { | ||
this._radios.forEach(radio => radio._markForCheck()); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the model value. Implemented as part of ControlValueAccessor. | ||
* @param value | ||
*/ | ||
writeValue(value: any) { | ||
this.value = value; | ||
this._change.markForCheck(); | ||
} | ||
|
||
/** | ||
|
@@ -247,6 +286,7 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
*/ | ||
setDisabledState(isDisabled: boolean) { | ||
this.disabled = isDisabled; | ||
this._change.markForCheck(); | ||
} | ||
} | ||
|
||
|
@@ -264,7 +304,8 @@ export class MdRadioGroup extends _MdRadioGroupMixinBase | |
'[class.mat-radio-checked]': 'checked', | ||
'[class.mat-radio-disabled]': 'disabled', | ||
'[attr.id]': 'id', | ||
} | ||
}, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | ||
|
||
|
@@ -307,6 +348,7 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | |
// Notify all radio buttons with the same name to un-check. | ||
this._radioDispatcher.notify(this.id, this.name); | ||
} | ||
this._changeDetector.markForCheck(); | ||
} | ||
} | ||
|
||
|
@@ -328,7 +370,6 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | |
this.radioGroup.selected = this; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
@@ -364,7 +405,6 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | |
get disabled(): boolean { | ||
return this._disabled || (this.radioGroup != null && this.radioGroup.disabled); | ||
} | ||
|
||
set disabled(value: boolean) { | ||
this._disabled = coerceBooleanProperty(value); | ||
} | ||
|
@@ -408,6 +448,7 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | |
constructor(@Optional() radioGroup: MdRadioGroup, | ||
private _elementRef: ElementRef, | ||
private _renderer: Renderer2, | ||
private _changeDetector: ChangeDetectorRef, | ||
private _focusOriginMonitor: FocusOriginMonitor, | ||
private _radioDispatcher: UniqueSelectionDispatcher) { | ||
// Assertions. Ideally these should be stripped out by the compiler. | ||
|
@@ -427,6 +468,17 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy { | |
this._focusOriginMonitor.focusVia(this._inputElement.nativeElement, this._renderer, 'keyboard'); | ||
} | ||
|
||
/** | ||
* Marks the radio button as needing checking for change detection. | ||
* This method is exposed because the parent radio group will directly | ||
* update bound properties of the radio button. | ||
*/ | ||
_markForCheck() { | ||
// When group value changes, the button will not be notified. Use `markForCheck` to explicit | ||
// update radio button's status | ||
this._changeDetector.markForCheck(); | ||
} | ||
|
||
ngOnInit() { | ||
if (this.radioGroup) { | ||
// If the radio is inside a radio group, determine if it should be checked | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call
_markRadiosForCheck()
here?