Skip to content

Commit 0a4dbe1

Browse files
authored
refactor(material/core): reduce mixin function boilerplate (#22637)
It seems like the various `*Ctor` interfaces for mixins aren't necessary anymore, because TS infers the type correctly without them. These changes clean up all the usages.
1 parent 940b164 commit 0a4dbe1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+186
-323
lines changed

src/material-experimental/mdc-button/button-base.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ import {Platform} from '@angular/cdk/platform';
1111
import {Directive, ElementRef, HostListener, NgZone, ViewChild} from '@angular/core';
1212
import {
1313
CanColor,
14-
CanColorCtor,
1514
CanDisable,
16-
CanDisableCtor,
1715
CanDisableRipple,
18-
CanDisableRippleCtor,
1916
MatRipple,
2017
mixinColor,
2118
mixinDisabled,
@@ -81,17 +78,14 @@ const HOST_SELECTOR_MDC_CLASS_PAIR: {selector: string, mdcClasses: string[]}[] =
8178

8279
// Boilerplate for applying mixins to MatButton.
8380
/** @docs-private */
84-
export class MatButtonMixinCore {
81+
export const _MatButtonMixin = mixinColor(mixinDisabled(mixinDisableRipple(class {
8582
constructor(public _elementRef: ElementRef) {}
86-
}
87-
88-
export const _MatButtonBaseMixin: CanDisableRippleCtor&CanDisableCtor&CanColorCtor&
89-
typeof MatButtonMixinCore = mixinColor(mixinDisabled(mixinDisableRipple(MatButtonMixinCore)));
83+
})));
9084

9185
/** Base class for all buttons. */
9286
@Directive()
93-
export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, CanColor,
94-
CanDisableRipple {
87+
export class MatButtonBase extends _MatButtonMixin implements CanDisable, CanColor,
88+
CanDisableRipple {
9589
/** The ripple animation configuration to use for the buttons. */
9690
_rippleAnimation: RippleAnimationConfig =
9791
this._animationMode === 'NoopAnimations' ?

src/material-experimental/mdc-checkbox/checkbox.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import {
3232
import {
3333
ThemePalette,
3434
RippleAnimationConfig,
35-
CanColorCtor,
36-
CanDisableCtor,
3735
mixinColor,
3836
mixinDisabled,
3937
CanColor,
@@ -65,14 +63,9 @@ export class MatCheckboxChange {
6563

6664
// Boilerplate for applying mixins to MatCheckbox.
6765
/** @docs-private */
68-
class MatCheckboxBase {
66+
const _MatCheckboxBase = mixinColor(mixinDisabled(class {
6967
constructor(public _elementRef: ElementRef) {}
70-
}
71-
const _MatCheckboxMixinBase:
72-
CanColorCtor &
73-
CanDisableCtor &
74-
typeof MatCheckboxBase =
75-
mixinColor(mixinDisabled(MatCheckboxBase));
68+
}));
7669

7770

7871
/** Configuration for the ripple animation. */
@@ -98,7 +91,7 @@ const RIPPLE_ANIMATION_CONFIG: RippleAnimationConfig = {
9891
encapsulation: ViewEncapsulation.None,
9992
changeDetection: ChangeDetectionStrategy.OnPush,
10093
})
101-
export class MatCheckbox extends _MatCheckboxMixinBase implements AfterViewInit, OnDestroy,
94+
export class MatCheckbox extends _MatCheckboxBase implements AfterViewInit, OnDestroy,
10295
ControlValueAccessor, CanColor, CanDisable {
10396
/**
10497
* The `aria-label` attribute to use for the input element. In most cases, `aria-labelledby` will

src/material-experimental/mdc-chips/chip.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ import {
3131
} from '@angular/core';
3232
import {
3333
CanColor,
34-
CanColorCtor,
3534
CanDisable,
3635
CanDisableRipple,
37-
CanDisableRippleCtor,
3836
HasTabIndex,
39-
HasTabIndexCtor,
4037
MatRipple,
4138
MAT_RIPPLE_GLOBAL_OPTIONS,
4239
mixinColor,
@@ -93,12 +90,7 @@ abstract class MatChipBase {
9390
constructor(public _elementRef: ElementRef) {}
9491
}
9592

96-
const _MatChipMixinBase:
97-
CanColorCtor &
98-
CanDisableRippleCtor &
99-
HasTabIndexCtor &
100-
typeof MatChipBase =
101-
mixinTabIndex(mixinColor(mixinDisableRipple(MatChipBase), 'primary'), -1);
93+
const _MatChipMixinBase = mixinTabIndex(mixinColor(mixinDisableRipple(MatChipBase), 'primary'), -1);
10294

10395
/**
10496
* Material design styled Chip base component. Used inside the MatChipSet component.

src/material-experimental/mdc-progress-bar/progress-bar.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
AfterViewInit,
2121
OnDestroy,
2222
} from '@angular/core';
23-
import {CanColor, CanColorCtor, mixinColor} from '@angular/material-experimental/mdc-core';
23+
import {CanColor, mixinColor} from '@angular/material-experimental/mdc-core';
2424
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2525
import {ProgressAnimationEnd} from '@angular/material/progress-bar';
2626
import {
@@ -34,12 +34,9 @@ import {Directionality} from '@angular/cdk/bidi';
3434

3535
// Boilerplate for applying mixins to MatProgressBar.
3636
/** @docs-private */
37-
class MatProgressBarBase {
38-
constructor(public _elementRef: ElementRef) { }
39-
}
40-
41-
const _MatProgressBarMixinBase: CanColorCtor & typeof MatProgressBarBase =
42-
mixinColor(MatProgressBarBase, 'primary');
37+
const _MatProgressBarBase = mixinColor(class {
38+
constructor(public _elementRef: ElementRef) {}
39+
}, 'primary');
4340

4441
export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'query';
4542

@@ -64,7 +61,7 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
6461
changeDetection: ChangeDetectionStrategy.OnPush,
6562
encapsulation: ViewEncapsulation.None,
6663
})
67-
export class MatProgressBar extends _MatProgressBarMixinBase implements AfterViewInit, OnDestroy,
64+
export class MatProgressBar extends _MatProgressBarBase implements AfterViewInit, OnDestroy,
6865
CanColor {
6966

7067
constructor(public _elementRef: ElementRef<HTMLElement>,

src/material-experimental/mdc-progress-spinner/progress-spinner.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
MDCCircularProgressAdapter,
2323
MDCCircularProgressFoundation
2424
} from '@material/circular-progress';
25-
import {CanColor, CanColorCtor, mixinColor} from '@angular/material-experimental/mdc-core';
25+
import {CanColor, mixinColor} from '@angular/material-experimental/mdc-core';
2626
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2727
import {
2828
MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS,
@@ -31,13 +31,10 @@ import {
3131
import {coerceNumberProperty, NumberInput} from '@angular/cdk/coercion';
3232

3333
// Boilerplate for applying mixins to MatProgressBar.
34-
class MatProgressSpinnerBase {
34+
const _MatProgressSpinnerBase = mixinColor(class {
3535
constructor(public _elementRef: ElementRef) {
3636
}
37-
}
38-
39-
const _MatProgressSpinnerMixinBase: CanColorCtor & typeof MatProgressSpinnerBase =
40-
mixinColor(MatProgressSpinnerBase, 'primary');
37+
}, 'primary');
4138

4239
/** Possible mode for a progress spinner. */
4340
export type ProgressSpinnerMode = 'determinate' | 'indeterminate';
@@ -75,7 +72,7 @@ const BASE_STROKE_WIDTH = 10;
7572
changeDetection: ChangeDetectionStrategy.OnPush,
7673
encapsulation: ViewEncapsulation.None,
7774
})
78-
export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements AfterViewInit,
75+
export class MatProgressSpinner extends _MatProgressSpinnerBase implements AfterViewInit,
7976
OnDestroy, CanColor {
8077

8178
/** Whether the _mat-animation-noopable class should be applied, disabling animations. */

src/material/autocomplete/autocomplete.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
} from '@angular/core';
3131
import {
3232
CanDisableRipple,
33-
CanDisableRippleCtor,
3433
MAT_OPTGROUP,
3534
MAT_OPTION_PARENT_COMPONENT,
3635
_MatOptgroupBase,
@@ -68,9 +67,7 @@ export interface MatAutocompleteActivatedEvent {
6867

6968
// Boilerplate for applying mixins to MatAutocomplete.
7069
/** @docs-private */
71-
class MatAutocompleteBase {}
72-
const _MatAutocompleteMixinBase: CanDisableRippleCtor & typeof MatAutocompleteBase =
73-
mixinDisableRipple(MatAutocompleteBase);
70+
const _MatAutocompleteMixinBase = mixinDisableRipple(class {});
7471

7572
/** Default `mat-autocomplete` options that can be overridden. */
7673
export interface MatAutocompleteDefaultOptions {

src/material/badge/badge.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@ import {
2020
Renderer2,
2121
SimpleChanges,
2222
} from '@angular/core';
23-
import {CanDisable, CanDisableCtor, mixinDisabled, ThemePalette} from '@angular/material/core';
23+
import {CanDisable, mixinDisabled, ThemePalette} from '@angular/material/core';
2424
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2525

2626

2727
let nextId = 0;
2828

2929
// Boilerplate for applying mixins to MatBadge.
3030
/** @docs-private */
31-
class MatBadgeBase {}
32-
33-
const _MatBadgeMixinBase:
34-
CanDisableCtor & typeof MatBadgeBase = mixinDisabled(MatBadgeBase);
31+
const _MatBadgeBase = mixinDisabled(class {});
3532

3633
/** Allowed position options for matBadgePosition */
3734
export type MatBadgePosition =
@@ -59,7 +56,7 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
5956
'[class.mat-badge-disabled]': 'disabled',
6057
},
6158
})
62-
export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges, CanDisable {
59+
export class MatBadge extends _MatBadgeBase implements OnDestroy, OnChanges, CanDisable {
6360
/** Whether the badge has any content. */
6461
_hasContent = false;
6562

src/material/button-toggle/button-toggle.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3636
import {
3737
CanDisableRipple,
3838
mixinDisableRipple,
39-
CanDisableRippleCtor,
4039
} from '@angular/material/core';
4140

4241

@@ -381,9 +380,7 @@ export class MatButtonToggleGroup implements ControlValueAccessor, OnInit, After
381380

382381
// Boilerplate for applying mixins to the MatButtonToggle class.
383382
/** @docs-private */
384-
class MatButtonToggleBase {}
385-
const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonToggleBase =
386-
mixinDisableRipple(MatButtonToggleBase);
383+
const _MatButtonToggleBase = mixinDisableRipple(class {});
387384

388385
/** Single button inside of a toggle group. */
389386
@Component({
@@ -408,7 +405,7 @@ const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonToggleBa
408405
'role': 'presentation',
409406
}
410407
})
411-
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, AfterViewInit,
408+
export class MatButtonToggle extends _MatButtonToggleBase implements OnInit, AfterViewInit,
412409
CanDisableRipple, OnDestroy {
413410

414411
private _isSingleSelector = false;

src/material/button/button.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import {
2424
CanColor,
2525
CanDisable,
2626
CanDisableRipple,
27-
CanColorCtor,
28-
CanDisableCtor,
29-
CanDisableRippleCtor,
3027
MatRipple,
3128
mixinColor,
3229
mixinDisabled,
@@ -52,13 +49,9 @@ const BUTTON_HOST_ATTRIBUTES = [
5249
];
5350

5451
// Boilerplate for applying mixins to MatButton.
55-
/** @docs-private */
56-
class MatButtonBase {
52+
const _MatButtonBase = mixinColor(mixinDisabled(mixinDisableRipple(class {
5753
constructor(public _elementRef: ElementRef) {}
58-
}
59-
60-
const _MatButtonMixinBase: CanDisableRippleCtor & CanDisableCtor & CanColorCtor &
61-
typeof MatButtonBase = mixinColor(mixinDisabled(mixinDisableRipple(MatButtonBase)));
54+
})));
6255

6356
/**
6457
* Material design button.
@@ -83,7 +76,7 @@ const _MatButtonMixinBase: CanDisableRippleCtor & CanDisableCtor & CanColorCtor
8376
encapsulation: ViewEncapsulation.None,
8477
changeDetection: ChangeDetectionStrategy.OnPush,
8578
})
86-
export class MatButton extends _MatButtonMixinBase
79+
export class MatButton extends _MatButtonBase
8780
implements AfterViewInit, OnDestroy, CanDisable, CanColor, CanDisableRipple, FocusableOption {
8881

8982
/** Whether the button is round. */

src/material/checkbox/checkbox.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ import {
3030
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3131
import {
3232
CanColor,
33-
CanColorCtor,
3433
CanDisable,
35-
CanDisableCtor,
3634
CanDisableRipple,
37-
CanDisableRippleCtor,
3835
HasTabIndex,
39-
HasTabIndexCtor,
4036
MatRipple,
4137
mixinColor,
4238
mixinDisabled,
@@ -93,16 +89,9 @@ export class MatCheckboxChange {
9389

9490
// Boilerplate for applying mixins to MatCheckbox.
9591
/** @docs-private */
96-
class MatCheckboxBase {
92+
const _MatCheckboxBase = mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(class {
9793
constructor(public _elementRef: ElementRef) {}
98-
}
99-
const _MatCheckboxMixinBase:
100-
HasTabIndexCtor &
101-
CanColorCtor &
102-
CanDisableRippleCtor &
103-
CanDisableCtor &
104-
typeof MatCheckboxBase =
105-
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatCheckboxBase))));
94+
}))));
10695

10796

10897
/**
@@ -133,7 +122,7 @@ const _MatCheckboxMixinBase:
133122
encapsulation: ViewEncapsulation.None,
134123
changeDetection: ChangeDetectionStrategy.OnPush
135124
})
136-
export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAccessor,
125+
export class MatCheckbox extends _MatCheckboxBase implements ControlValueAccessor,
137126
AfterViewInit, AfterViewChecked, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple,
138127
FocusableOption {
139128

src/material/chips/chip-list.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
3232
import {
3333
CanUpdateErrorState,
34-
CanUpdateErrorStateCtor,
3534
ErrorStateMatcher,
3635
mixinErrorState,
3736
} from '@angular/material/core';
@@ -43,15 +42,13 @@ import {MatChipTextControl} from './chip-text-control';
4342

4443
// Boilerplate for applying mixins to MatChipList.
4544
/** @docs-private */
46-
class MatChipListBase {
45+
const _MatChipListBase = mixinErrorState(class {
4746
constructor(public _defaultErrorStateMatcher: ErrorStateMatcher,
4847
public _parentForm: NgForm,
4948
public _parentFormGroup: FormGroupDirective,
5049
/** @docs-private */
5150
public ngControl: NgControl) {}
52-
}
53-
const _MatChipListMixinBase: CanUpdateErrorStateCtor & typeof MatChipListBase =
54-
mixinErrorState(MatChipListBase);
51+
});
5552

5653

5754
// Increasing integer for generating unique ids for chip-list components.
@@ -96,7 +93,7 @@ export class MatChipListChange {
9693
encapsulation: ViewEncapsulation.None,
9794
changeDetection: ChangeDetectionStrategy.OnPush
9895
})
99-
export class MatChipList extends _MatChipListMixinBase implements MatFormFieldControl<any>,
96+
export class MatChipList extends _MatChipListBase implements MatFormFieldControl<any>,
10097
ControlValueAccessor, AfterContentInit, DoCheck, OnInit, OnDestroy, CanUpdateErrorState {
10198
/**
10299
* Implemented as part of MatFormFieldControl.

src/material/chips/chip.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ import {
2828
} from '@angular/core';
2929
import {
3030
CanColor,
31-
CanColorCtor,
3231
CanDisable,
3332
CanDisableRipple,
34-
CanDisableRippleCtor,
3533
HasTabIndex,
36-
HasTabIndexCtor,
3734
MAT_RIPPLE_GLOBAL_OPTIONS,
3835
mixinColor,
3936
mixinDisableRipple,
@@ -94,9 +91,7 @@ abstract class MatChipBase {
9491
constructor(public _elementRef: ElementRef) {}
9592
}
9693

97-
const _MatChipMixinBase: CanColorCtor & CanDisableRippleCtor &
98-
HasTabIndexCtor & typeof MatChipBase =
99-
mixinTabIndex(mixinColor(mixinDisableRipple(MatChipBase), 'primary'), -1);
94+
const _MatChipMixinBase = mixinTabIndex(mixinColor(mixinDisableRipple(MatChipBase), 'primary'), -1);
10095

10196
/**
10297
* Dummy directive to add CSS class to chip avatar.

src/material/core/common-behaviors/color.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export interface CanColor {
1818
defaultColor: ThemePalette | undefined;
1919
}
2020

21-
/** @docs-private */
21+
/**
22+
* @docs-private
23+
* @deprecated No longer necessary to apply to mixin classes. To be made private.
24+
* @breaking-change 13.0.0
25+
*/
2226
export type CanColorCtor = Constructor<CanColor> & AbstractConstructor<CanColor>;
2327

2428
/** @docs-private */

0 commit comments

Comments
 (0)