Skip to content

Commit 92c4caa

Browse files
committed
feat(material/form-field): add color to default options (#24438)
closes #24438
1 parent bab3b98 commit 92c4caa

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@ export type SubscriptSizing = 'fixed' | 'dynamic';
6969
* using the `MAT_FORM_FIELD_DEFAULT_OPTIONS` injection token.
7070
*/
7171
export interface MatFormFieldDefaultOptions {
72+
/** Default form-field appearance style. */
7273
appearance?: MatFormFieldAppearance;
74+
/** Default color of the form-field. */
75+
color?: ThemePalette;
76+
/** Whether the required marker should be hidden by default. */
7377
hideRequiredMarker?: boolean;
78+
/**
79+
* Whether the label for form-fields should by default float `always`,
80+
* `never`, or `auto` (only when necessary).
81+
*/
7482
floatLabel?: FloatLabelType;
83+
/** Whether the form field should reserve space for one line by default. */
7584
subscriptSizing?: SubscriptSizing;
7685
}
7786

@@ -147,7 +156,7 @@ const WRAPPER_HORIZONTAL_PADDING = 16;
147156
providers: [{provide: MAT_FORM_FIELD, useExisting: MatFormField}],
148157
})
149158
export class MatFormField
150-
implements AfterViewInit, OnDestroy, AfterContentChecked, AfterContentInit
159+
implements AfterContentInit, AfterContentChecked, AfterViewInit, OnDestroy
151160
{
152161
@ViewChild('textField') _textField: ElementRef<HTMLElement>;
153162
@ViewChild('iconPrefixContainer') _iconPrefixContainer: ElementRef<HTMLElement>;
@@ -172,7 +181,7 @@ export class MatFormField
172181
set hideRequiredMarker(value: BooleanInput) {
173182
this._hideRequiredMarker = coerceBooleanProperty(value);
174183
}
175-
private _hideRequiredMarker: boolean;
184+
private _hideRequiredMarker = false;
176185

177186
/** The color palette for the form-field. */
178187
@Input() color: ThemePalette = 'primary';
@@ -201,7 +210,7 @@ export class MatFormField
201210
}
202211
set appearance(value: MatFormFieldAppearance) {
203212
const oldValue = this._appearance;
204-
this._appearance = value || (this._defaults && this._defaults.appearance) || DEFAULT_APPEARANCE;
213+
this._appearance = value || this._defaults?.appearance || DEFAULT_APPEARANCE;
205214
if (this._appearance === 'outline' && this._appearance !== oldValue) {
206215
this._refreshOutlineNotchWidth();
207216

@@ -363,11 +372,15 @@ export class MatFormField
363372
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
364373
@Inject(DOCUMENT) private _document?: any,
365374
) {
366-
if (_defaults && _defaults.appearance) {
367-
this.appearance = _defaults.appearance;
375+
if (_defaults) {
376+
if (_defaults.appearance) {
377+
this.appearance = _defaults.appearance;
378+
}
379+
this._hideRequiredMarker = Boolean(_defaults?.hideRequiredMarker);
380+
if (_defaults.color) {
381+
this.color = _defaults.color;
382+
}
368383
}
369-
370-
this._hideRequiredMarker = _defaults?.hideRequiredMarker ?? false;
371384
}
372385

373386
ngAfterViewInit() {

src/material-experimental/mdc-input/input.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,18 @@ describe('MatFormField default options', () => {
14051405
expect(fixture.componentInstance.formField.appearance).toBe('outline');
14061406
});
14071407

1408+
it('should be able to change the default color', fakeAsync(() => {
1409+
const fixture = createComponent(MatInputWithColor, [
1410+
{
1411+
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
1412+
useValue: {color: 'accent'},
1413+
},
1414+
]);
1415+
fixture.detectChanges();
1416+
const formField = fixture.nativeElement.querySelector('.mat-form-field');
1417+
expect(formField.classList).toContain('mat-accent');
1418+
}));
1419+
14081420
it('defaults subscriptSizing to false', () => {
14091421
const fixture = createComponent(MatInputWithSubscriptSizing);
14101422
fixture.detectChanges();

src/material/form-field/form-field.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
ViewEncapsulation,
2929
OnDestroy,
3030
} from '@angular/core';
31-
import {CanColor, mixinColor} from '@angular/material/core';
31+
import {CanColor, mixinColor, ThemePalette} from '@angular/material/core';
3232
import {fromEvent, merge, Subject} from 'rxjs';
3333
import {startWith, take, takeUntil} from 'rxjs/operators';
3434
import {MAT_ERROR, MatError} from './error';
@@ -74,7 +74,11 @@ export type FloatLabelType = 'always' | 'never' | 'auto';
7474
* using the `MAT_FORM_FIELD_DEFAULT_OPTIONS` injection token.
7575
*/
7676
export interface MatFormFieldDefaultOptions {
77+
/** Default form-field appearance style. */
7778
appearance?: MatFormFieldAppearance;
79+
/** Default color of the form-field. */
80+
color?: ThemePalette;
81+
/** Whether the required marker should be hidden by default. */
7882
hideRequiredMarker?: boolean;
7983
/**
8084
* Whether the label for form-fields should by default float `always`,
@@ -166,7 +170,7 @@ export class MatFormField
166170
set appearance(value: MatFormFieldAppearance) {
167171
const oldValue = this._appearance;
168172

169-
this._appearance = value || (this._defaults && this._defaults.appearance) || 'legacy';
173+
this._appearance = value || this._defaults?.appearance || 'legacy';
170174

171175
if (this._appearance === 'outline' && oldValue !== value) {
172176
this._outlineGapCalculationNeededOnStable = true;
@@ -182,7 +186,7 @@ export class MatFormField
182186
set hideRequiredMarker(value: BooleanInput) {
183187
this._hideRequiredMarker = coerceBooleanProperty(value);
184188
}
185-
private _hideRequiredMarker: boolean;
189+
private _hideRequiredMarker = false;
186190

187191
/** Override for the logic that disables the label animation in certain cases. */
188192
private _showAlwaysAnimate = false;
@@ -282,9 +286,13 @@ export class MatFormField
282286
this._animationsEnabled = _animationMode !== 'NoopAnimations';
283287

284288
// Set the default through here so we invoke the setter on the first run.
285-
this.appearance = _defaults && _defaults.appearance ? _defaults.appearance : 'legacy';
286-
this._hideRequiredMarker =
287-
_defaults && _defaults.hideRequiredMarker != null ? _defaults.hideRequiredMarker : false;
289+
this.appearance = _defaults?.appearance || 'legacy';
290+
if (_defaults) {
291+
this._hideRequiredMarker = Boolean(_defaults.hideRequiredMarker);
292+
if (_defaults.color) {
293+
this.color = this.defaultColor = _defaults.color;
294+
}
295+
}
288296
}
289297

290298
/**

src/material/input/input.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,18 @@ describe('MatFormField default options', () => {
18451845
expect(fixture.componentInstance.formField.hideRequiredMarker).toBe(true);
18461846
expect(fixture.componentInstance.formField.appearance).toBe('outline');
18471847
});
1848+
1849+
it('should be able to change the default color', fakeAsync(() => {
1850+
const fixture = createComponent(MatInputWithColor, [
1851+
{
1852+
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
1853+
useValue: {color: 'accent'},
1854+
},
1855+
]);
1856+
fixture.detectChanges();
1857+
const formField = fixture.nativeElement.querySelector('.mat-form-field');
1858+
expect(formField.classList).toContain('mat-accent');
1859+
}));
18481860
});
18491861

18501862
function createComponent<T>(

tools/public_api_guard/material/form-field.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { Observable } from 'rxjs';
2626
import { OnDestroy } from '@angular/core';
2727
import { Platform } from '@angular/cdk/platform';
2828
import { QueryList } from '@angular/core';
29+
import { ThemePalette } from '@angular/material/core';
2930

3031
// @public
3132
export type FloatLabelType = 'always' | 'never' | 'auto';
@@ -179,10 +180,9 @@ export abstract class MatFormFieldControl<T> {
179180

180181
// @public
181182
export interface MatFormFieldDefaultOptions {
182-
// (undocumented)
183183
appearance?: MatFormFieldAppearance;
184+
color?: ThemePalette;
184185
floatLabel?: FloatLabelType;
185-
// (undocumented)
186186
hideRequiredMarker?: boolean;
187187
}
188188

0 commit comments

Comments
 (0)