Skip to content

Commit

Permalink
chore(selection): switch option and pseudo checkbox to OnPush change …
Browse files Browse the repository at this point in the history
…detection (angular#5585)

* Switches the MdPseudoCheckbox, MdOption and MdOptgroup to OnPush change detection.
* Fixes a few cases in MdOption where the UI state wasn't being updated properly.

Relates to angular#5035.
  • Loading branch information
crisbeto authored and jelbourn committed Jul 11, 2017
1 parent bcf4826 commit 70117ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/lib/core/option/optgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, ViewEncapsulation, Input} from '@angular/core';
import {Component, ViewEncapsulation, Input, ChangeDetectionStrategy} from '@angular/core';
import {mixinDisabled, CanDisable} from '../common-behaviors/disabled';

// Boilerplate for applying mixins to MdOptgroup.
Expand All @@ -25,6 +25,7 @@ let _uniqueOptgroupIdCounter = 0;
selector: 'md-optgroup, mat-optgroup',
templateUrl: 'optgroup.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
inputs: ['disabled'],
host: {
'class': 'mat-optgroup',
Expand Down
28 changes: 24 additions & 4 deletions src/lib/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ViewEncapsulation,
Inject,
Optional,
ChangeDetectionStrategy,
ChangeDetectorRef,
} from '@angular/core';
import {ENTER, SPACE} from '../keyboard/keycodes';
import {coerceBooleanProperty} from '@angular/cdk';
Expand Down Expand Up @@ -54,19 +56,27 @@ export class MdOptionSelectionChange {
'class': 'mat-option',
},
templateUrl: 'option.html',
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdOption {
private _selected: boolean = false;
private _active: boolean = false;
private _multiple: boolean = false;

/** Whether the option is disabled. */
private _disabled: boolean = false;

private _id: string = `md-option-${_uniqueIdCounter++}`;

/** Whether the wrapping component is in multiple selection mode. */
multiple: boolean = false;
get multiple() { return this._multiple; }
set multiple(value: boolean) {
if (value !== this._multiple) {
this._multiple = value;
this._changeDetectorRef.markForCheck();
}
}

/** The unique ID of the option. */
get id() { return this._id; }
Expand All @@ -87,6 +97,7 @@ export class MdOption {

constructor(
private _element: ElementRef,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() public readonly group: MdOptgroup,
@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) public _isCompatibilityMode: boolean) {}

Expand All @@ -112,12 +123,14 @@ export class MdOption {
/** Selects the option. */
select(): void {
this._selected = true;
this._changeDetectorRef.markForCheck();
this._emitSelectionChangeEvent();
}

/** Deselects the option. */
deselect(): void {
this._selected = false;
this._changeDetectorRef.markForCheck();
this._emitSelectionChangeEvent();
}

Expand All @@ -132,7 +145,10 @@ export class MdOption {
* events will display the proper options as active on arrow key events.
*/
setActiveStyles(): void {
this._active = true;
if (!this._active) {
this._active = true;
this._changeDetectorRef.markForCheck();
}
}

/**
Expand All @@ -141,7 +157,10 @@ export class MdOption {
* events will display the proper options as active on arrow key events.
*/
setInactiveStyles(): void {
this._active = false;
if (this._active) {
this._active = false;
this._changeDetectorRef.markForCheck();
}
}

/** Ensures the option is selected when activated from the keyboard. */
Expand All @@ -161,6 +180,7 @@ export class MdOption {
_selectViaInteraction(): void {
if (!this.disabled) {
this._selected = this.multiple ? !this._selected : true;
this._changeDetectorRef.markForCheck();
this._emitSelectionChangeEvent(true);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/selection/pseudo-checkbox/pseudo-checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Input,
ElementRef,
Renderer2,
ChangeDetectionStrategy,
} from '@angular/core';
import {CanColor, mixinColor} from '../../common-behaviors/color';

Expand Down Expand Up @@ -40,6 +41,7 @@ export const _MdPseudoCheckboxBase = mixinColor(MdPseudoCheckboxBase, 'accent');
@Component({
moduleId: module.id,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'md-pseudo-checkbox, mat-pseudo-checkbox',
styleUrls: ['pseudo-checkbox.css'],
inputs: ['color'],
Expand Down

0 comments on commit 70117ff

Please sign in to comment.