Skip to content

Commit 0206d6f

Browse files
devversionkara
authored andcommitted
refactor: replace disabled getters/setters with mixin (#5137)
1 parent 8860090 commit 0206d6f

File tree

5 files changed

+49
-83
lines changed

5 files changed

+49
-83
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import {
2525
AfterViewInit,
2626
} from '@angular/core';
2727
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
28-
import {Observable} from 'rxjs/Observable';
2928
import {UniqueSelectionDispatcher, coerceBooleanProperty, FocusOriginMonitor} from '../core';
29+
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
3030

3131
/** Acceptable types for a button toggle. */
3232
export type ToggleType = 'checkbox' | 'radio';
3333

34-
34+
// Boilerplate for applying mixins to MdButtonToggleGroup and MdButtonToggleGroupMultiple
35+
export class MdButtonToggleGroupBase {}
36+
export const _MdButtonToggleGroupMixinBase = mixinDisabled(MdButtonToggleGroupBase);
3537

3638
/**
3739
* Provider Expression that allows md-button-toggle-group to register as a ControlValueAccessor.
@@ -58,23 +60,23 @@ export class MdButtonToggleChange {
5860
@Directive({
5961
selector: 'md-button-toggle-group:not([multiple]), mat-button-toggle-group:not([multiple])',
6062
providers: [MD_BUTTON_TOGGLE_GROUP_VALUE_ACCESSOR],
63+
inputs: ['disabled'],
6164
host: {
6265
'role': 'radiogroup',
6366
'class': 'mat-button-toggle-group',
6467
'[class.mat-button-toggle-vertical]': 'vertical'
6568
},
6669
exportAs: 'mdButtonToggleGroup',
6770
})
68-
export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor {
71+
export class MdButtonToggleGroup extends _MdButtonToggleGroupMixinBase implements AfterViewInit,
72+
ControlValueAccessor, CanDisable {
73+
6974
/** The value for the button toggle group. Should match currently selected button toggle. */
7075
private _value: any = null;
7176

7277
/** The HTML name attribute applied to toggles in this group. */
7378
private _name: string = `md-button-toggle-group-${_uniqueIdCounter++}`;
7479

75-
/** Disables all toggles in the group. */
76-
private _disabled: boolean = null;
77-
7880
/** Whether the button toggle group should be vertical. */
7981
private _vertical: boolean = false;
8082

@@ -112,16 +114,6 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
112114
this._updateButtonToggleNames();
113115
}
114116

115-
/** Whether the toggle group is disabled. */
116-
@Input()
117-
get disabled(): boolean {
118-
return this._disabled;
119-
}
120-
121-
set disabled(value) {
122-
this._disabled = coerceBooleanProperty(value);
123-
}
124-
125117
/** Whether the toggle group is vertical. */
126118
@Input()
127119
get vertical(): boolean {
@@ -245,28 +237,18 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
245237
@Directive({
246238
selector: 'md-button-toggle-group[multiple], mat-button-toggle-group[multiple]',
247239
exportAs: 'mdButtonToggleGroup',
240+
inputs: ['disabled'],
248241
host: {
249242
'class': 'mat-button-toggle-group',
250243
'[class.mat-button-toggle-vertical]': 'vertical'
251244
}
252245
})
253-
export class MdButtonToggleGroupMultiple {
254-
/** Disables all toggles in the group. */
255-
private _disabled: boolean = null;
246+
export class MdButtonToggleGroupMultiple extends _MdButtonToggleGroupMixinBase
247+
implements CanDisable {
256248

257249
/** Whether the button toggle group should be vertical. */
258250
private _vertical: boolean = false;
259251

260-
/** Whether the toggle group is disabled. */
261-
@Input()
262-
get disabled(): boolean {
263-
return this._disabled;
264-
}
265-
266-
set disabled(value) {
267-
this._disabled = (value != null && value !== false) ? true : null;
268-
}
269-
270252
/** Whether the toggle group is vertical. */
271253
@Input()
272254
get vertical(): boolean {

src/lib/menu/menu-item.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component, ElementRef, Input} from '@angular/core';
9+
import {Component, ElementRef} from '@angular/core';
1010
import {Focusable} from '../core/a11y/focus-key-manager';
11-
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
11+
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
12+
13+
// Boilerplate for applying mixins to MdMenuItem.
14+
export class MdMenuItemBase {}
15+
export const _MdMenuItemMixinBase = mixinDisabled(MdMenuItemBase);
1216

1317
/**
1418
* This directive is intended to be used inside an md-menu tag.
@@ -17,6 +21,7 @@ import {coerceBooleanProperty} from '../core/coercion/boolean-property';
1721
@Component({
1822
moduleId: module.id,
1923
selector: '[md-menu-item], [mat-menu-item]',
24+
inputs: ['disabled'],
2025
host: {
2126
'role': 'menuitem',
2227
'class': 'mat-menu-item',
@@ -28,32 +33,25 @@ import {coerceBooleanProperty} from '../core/coercion/boolean-property';
2833
templateUrl: 'menu-item.html',
2934
exportAs: 'mdMenuItem'
3035
})
31-
export class MdMenuItem implements Focusable {
32-
/** Whether the menu item is disabled */
33-
private _disabled: boolean = false;
36+
export class MdMenuItem extends _MdMenuItemMixinBase implements Focusable, CanDisable {
3437

35-
constructor(private _elementRef: ElementRef) {}
38+
constructor(private _elementRef: ElementRef) {
39+
super();
40+
}
3641

3742
/** Focuses the menu item. */
3843
focus(): void {
3944
this._getHostElement().focus();
4045
}
4146

42-
/** Whether the menu item is disabled. */
43-
@Input()
44-
get disabled() { return this._disabled; }
45-
set disabled(value: any) {
46-
this._disabled = coerceBooleanProperty(value);
47-
}
48-
4947
/** Used to set the `tabindex`. */
5048
_getTabIndex(): string {
51-
return this._disabled ? '-1' : '0';
49+
return this.disabled ? '-1' : '0';
5250
}
5351

5452
/** Used to set the HTML `disabled` attribute. Necessary for links to be disabled properly. */
5553
_getDisabledAttr(): boolean {
56-
return this._disabled ? true : null;
54+
return this.disabled ? true : null;
5755
}
5856

5957
/** Returns the host DOM element. */

src/lib/select/select.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ import {coerceBooleanProperty} from '../core/coercion/boolean-property';
3838
import {ConnectedOverlayDirective} from '../core/overlay/overlay-directives';
3939
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
4040
import {SelectionModel} from '../core/selection/selection';
41-
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
4241
import {getMdSelectDynamicMultipleError, getMdSelectNonArrayValueError} from './select-errors';
4342
import 'rxjs/add/observable/merge';
4443
import 'rxjs/add/operator/startWith';
4544
import 'rxjs/add/operator/filter';
4645
import {CanColor, mixinColor} from '../core/common-behaviors/color';
47-
import {CanDisable} from '../core/common-behaviors/disabled';
46+
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
4847
import {
4948
FloatPlaceholderType,
5049
PlaceholderOptions,
@@ -114,14 +113,14 @@ export class MdSelectChange {
114113
export class MdSelectBase {
115114
constructor(public _renderer: Renderer2, public _elementRef: ElementRef) {}
116115
}
117-
export const _MdSelectMixinBase = mixinColor(MdSelectBase, 'primary');
116+
export const _MdSelectMixinBase = mixinColor(mixinDisabled(MdSelectBase), 'primary');
118117

119118
@Component({
120119
moduleId: module.id,
121120
selector: 'md-select, mat-select',
122121
templateUrl: 'select.html',
123122
styleUrls: ['select.css'],
124-
inputs: ['color'],
123+
inputs: ['color', 'disabled'],
125124
encapsulation: ViewEncapsulation.None,
126125
host: {
127126
'role': 'listbox',
@@ -145,7 +144,7 @@ export const _MdSelectMixinBase = mixinColor(MdSelectBase, 'primary');
145144
exportAs: 'mdSelect',
146145
})
147146
export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, OnDestroy, OnInit,
148-
ControlValueAccessor, CanColor {
147+
ControlValueAccessor, CanColor, CanDisable {
149148
/** Whether or not the overlay panel is open. */
150149
private _panelOpen = false;
151150

@@ -161,9 +160,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
161160
/** Whether filling out the select is required in the form. */
162161
private _required: boolean = false;
163162

164-
/** Whether the select is disabled. */
165-
private _disabled: boolean = false;
166-
167163
/** The scroll position of the overlay panel, calculated to center the selected option. */
168164
private _scrollTop = 0;
169165

@@ -268,13 +264,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
268264
Promise.resolve(null).then(() => this._setTriggerWidth());
269265
}
270266

271-
/** Whether the component is disabled. */
272-
@Input()
273-
get disabled() { return this._disabled; }
274-
set disabled(value: any) {
275-
this._disabled = coerceBooleanProperty(value);
276-
}
277-
278267
/** Whether the component is required. */
279268
@Input()
280269
get required() { return this._required; }
@@ -301,7 +290,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
301290

302291
/** Tab index for the select element. */
303292
@Input()
304-
get tabIndex(): number { return this._disabled ? -1 : this._tabIndex; }
293+
get tabIndex(): number { return this.disabled ? -1 : this._tabIndex; }
305294
set tabIndex(value: number) {
306295
if (typeof value !== 'undefined') {
307296
this._tabIndex = value;

src/lib/tabs/tab-label-wrapper.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, Input} from '@angular/core';
10-
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
9+
import {Directive, ElementRef} from '@angular/core';
10+
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
1111

12+
// Boilerplate for applying mixins to MdTabLabelWrapper.
13+
export class MdTabLabelWrapperBase {}
14+
export const _MdTabLabelWrapperMixinBase = mixinDisabled(MdTabLabelWrapperBase);
1215

1316
/**
1417
* Used in the `md-tab-group` view to display tab labels.
1518
* @docs-private
1619
*/
1720
@Directive({
1821
selector: '[md-tab-label-wrapper], [mat-tab-label-wrapper]',
22+
inputs: ['disabled'],
1923
host: {
2024
'[class.mat-tab-disabled]': 'disabled'
2125
}
2226
})
23-
export class MdTabLabelWrapper {
24-
constructor(public elementRef: ElementRef) {}
25-
26-
/** Whether the tab label is disabled. */
27-
private _disabled: boolean = false;
28-
29-
/** Whether the element is disabled. */
30-
@Input()
31-
get disabled() { return this._disabled; }
32-
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
27+
export class MdTabLabelWrapper extends _MdTabLabelWrapperMixinBase implements CanDisable {
28+
constructor(public elementRef: ElementRef) {
29+
super();
30+
}
3331

3432
/** Sets focus on the wrapper element */
3533
focus(): void {

src/lib/tabs/tab.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ import {
1111
ViewContainerRef, Input, TemplateRef, ViewChild, OnInit, ContentChild,
1212
Component
1313
} from '@angular/core';
14-
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
15-
14+
import {CanDisable, mixinDisabled} from '../core/common-behaviors/disabled';
1615
import {MdTabLabel} from './tab-label';
1716

17+
// Boilerplate for applying mixins to MdTab.
18+
export class MdTabBase {}
19+
export const _MdTabMixinBase = mixinDisabled(MdTabBase);
20+
1821
@Component({
1922
moduleId: module.id,
2023
selector: 'md-tab, mat-tab',
2124
templateUrl: 'tab.html',
25+
inputs: ['disabled']
2226
})
23-
export class MdTab implements OnInit {
27+
export class MdTab extends _MdTabMixinBase implements OnInit, CanDisable {
2428
/** Content for the tab label given by <ng-template md-tab-label>. */
2529
@ContentChild(MdTabLabel) templateLabel: MdTabLabel;
2630

@@ -46,14 +50,9 @@ export class MdTab implements OnInit {
4650
*/
4751
origin: number = null;
4852

49-
private _disabled = false;
50-
51-
/** Whether the tab is disabled */
52-
@Input()
53-
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value); }
54-
get disabled(): boolean { return this._disabled; }
55-
56-
constructor(private _viewContainerRef: ViewContainerRef) { }
53+
constructor(private _viewContainerRef: ViewContainerRef) {
54+
super();
55+
}
5756

5857
ngOnInit() {
5958
this._contentPortal = new TemplatePortal(this._content, this._viewContainerRef);

0 commit comments

Comments
 (0)