Skip to content

Commit c4b7604

Browse files
authored
fix(core): allow for default color and tabindex to be set per instance (#20125)
This is something that came up in #18467. Currently the default values in the `mixinColor` and `mixinTabIndex` are set when the mixin class is created, however we have some components where the default color can be configured through an injection token. These changes add a default field on each instance so that it can be updated after the class is instantiated.
1 parent 90d6b70 commit c4b7604

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

src/material/checkbox/checkbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
216216
this._options = this._options || {};
217217

218218
if (this._options.color) {
219-
this.color = this._options.color;
219+
this.color = this.defaultColor = this._options.color;
220220
}
221221

222222
this.tabIndex = parseInt(tabIndex) || 0;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ describe('MixinColor', () => {
6767
.toContain('mat-accent', 'Expected the default color "mat-accent" to be set.');
6868
});
6969

70+
it('should allow for the default color to change after init', () => {
71+
const classWithColor = mixinColor(TestClass, 'accent');
72+
const instance = new classWithColor();
73+
74+
expect(instance.testElement.classList).toContain('mat-accent');
75+
76+
instance.defaultColor = 'warn';
77+
instance.color = undefined;
78+
79+
expect(instance.testElement.classList).toContain('mat-warn');
80+
});
81+
7082
});
7183

7284
class TestClass {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {ElementRef} from '@angular/core';
1313
export interface CanColor {
1414
/** Theme color palette for the component. */
1515
color: ThemePalette;
16+
17+
/** Default color to fall back to if no value is set. */
18+
defaultColor: ThemePalette | undefined;
1619
}
1720

1821
/** @docs-private */
@@ -31,10 +34,11 @@ export function mixinColor<T extends Constructor<HasElementRef>>(
3134
base: T, defaultColor?: ThemePalette): CanColorCtor & T {
3235
return class extends base {
3336
private _color: ThemePalette;
37+
defaultColor = defaultColor;
3438

3539
get color(): ThemePalette { return this._color; }
3640
set color(value: ThemePalette) {
37-
const colorPalette = value || defaultColor;
41+
const colorPalette = value || this.defaultColor;
3842

3943
if (colorPalette !== this._color) {
4044
if (this._color) {

src/material/core/common-behaviors/tabindex.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ describe('mixinTabIndex', () => {
4141
.toBe(0, 'Expected tabIndex to still support 0 as value');
4242
});
4343

44+
it('should allow for the default tabIndex to change after init', () => {
45+
const classWithMixin = mixinTabIndex(TestClass, 20);
46+
const instance = new classWithMixin();
47+
48+
expect(instance.tabIndex).toBe(20);
49+
50+
instance.defaultTabIndex = 50;
51+
instance.tabIndex = undefined!;
52+
53+
expect(instance.tabIndex).toBe(50);
54+
});
55+
4456
});
4557

4658
class TestClass {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {CanDisable} from './disabled';
1515
export interface HasTabIndex {
1616
/** Tabindex of the component. */
1717
tabIndex: number;
18+
19+
/** Tabindex to which to fall back to if no value is set. */
20+
defaultTabIndex: number;
1821
}
1922

2023
/** @docs-private */
@@ -25,11 +28,12 @@ export function mixinTabIndex<T extends Constructor<CanDisable>>(base: T, defaul
2528
: HasTabIndexCtor & T {
2629
return class extends base {
2730
private _tabIndex: number = defaultTabIndex;
31+
defaultTabIndex = defaultTabIndex;
2832

2933
get tabIndex(): number { return this.disabled ? -1 : this._tabIndex; }
3034
set tabIndex(value: number) {
3135
// If the specified tabIndex value is null or undefined, fall back to the default value.
32-
this._tabIndex = value != null ? coerceNumberProperty(value) : defaultTabIndex;
36+
this._tabIndex = value != null ? coerceNumberProperty(value) : this.defaultTabIndex;
3337
}
3438

3539
constructor(...args: any[]) {

src/material/datepicker/datepicker-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export interface MatDatepickerControl<D> {
238238
/** Base class for a datepicker. */
239239
@Directive()
240240
export abstract class MatDatepickerBase<C extends MatDatepickerControl<D>, S,
241-
D = ExtractDateTypeFromSelection<S>> implements OnDestroy, CanColor, OnChanges {
241+
D = ExtractDateTypeFromSelection<S>> implements OnDestroy, OnChanges {
242242
private _scrollStrategy: () => ScrollStrategy;
243243
private _inputStateChanges = Subscription.EMPTY;
244244

tools/public_api_guard/material/core.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export declare const JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL =
6161

6262
export interface CanColor {
6363
color: ThemePalette;
64+
defaultColor: ThemePalette | undefined;
6465
}
6566

6667
export declare type CanColorCtor = Constructor<CanColor>;
@@ -152,6 +153,7 @@ export interface HasInitialized {
152153
export declare type HasInitializedCtor = Constructor<HasInitialized>;
153154

154155
export interface HasTabIndex {
156+
defaultTabIndex: number;
155157
tabIndex: number;
156158
}
157159

0 commit comments

Comments
 (0)