Skip to content

Commit 158b1db

Browse files
mmalerbajelbourn
authored andcommitted
feat(form-field): allow setting default appearance via provider (#9815)
1 parent 498534b commit 158b1db

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

src/lib/form-field/form-field.ts

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

9+
import {Directionality} from '@angular/cdk/bidi';
910
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1011
import {
1112
AfterContentChecked,
@@ -18,6 +19,7 @@ import {
1819
ContentChildren,
1920
ElementRef,
2021
Inject,
22+
InjectionToken,
2123
Input,
2224
Optional,
2325
QueryList,
@@ -48,7 +50,6 @@ import {MatLabel} from './label';
4850
import {MatPlaceholder} from './placeholder';
4951
import {MatPrefix} from './prefix';
5052
import {MatSuffix} from './suffix';
51-
import {Directionality} from '@angular/cdk/bidi';
5253

5354

5455
let nextUniqueId = 0;
@@ -69,6 +70,14 @@ export const _MatFormFieldMixinBase = mixinColor(MatFormFieldBase, 'primary');
6970
export type MatFormFieldAppearance = 'legacy' | 'standard' | 'fill' | 'outline';
7071

7172

73+
export interface MatFormFieldDefaultOptions {
74+
appearance?: MatFormFieldAppearance;
75+
}
76+
77+
export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
78+
new InjectionToken<MatFormFieldDefaultOptions>('MAT_FORM_FIELD_DEFAULT_OPTIONS');
79+
80+
7281
/** Container for form controls that applies Material Design styling and behavior. */
7382
@Component({
7483
moduleId: module.id,
@@ -122,7 +131,14 @@ export class MatFormField extends _MatFormFieldMixinBase
122131
private _labelOptions: LabelOptions;
123132

124133
/** The form-field appearance style. */
125-
@Input() appearance: MatFormFieldAppearance = 'legacy';
134+
@Input()
135+
get appearance(): MatFormFieldAppearance {
136+
return this._appearance || this._defaultOptions && this._defaultOptions.appearance || 'legacy';
137+
}
138+
set appearance(value: MatFormFieldAppearance) {
139+
this._appearance = value;
140+
}
141+
_appearance: MatFormFieldAppearance;
126142

127143
/**
128144
* @deprecated Use `color` instead.
@@ -220,7 +236,9 @@ export class MatFormField extends _MatFormFieldMixinBase
220236
public _elementRef: ElementRef,
221237
private _changeDetectorRef: ChangeDetectorRef,
222238
@Optional() @Inject(MAT_LABEL_GLOBAL_OPTIONS) labelOptions: LabelOptions,
223-
@Optional() private _dir: Directionality) {
239+
@Optional() private _dir: Directionality,
240+
@Optional() @Inject(MAT_FORM_FIELD_DEFAULT_OPTIONS) private _defaultOptions:
241+
MatFormFieldDefaultOptions) {
224242
super(_elementRef);
225243

226244
this._labelOptions = labelOptions ? labelOptions : {};

src/lib/input/input.spec.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import {
2121
getMatFormFieldDuplicatedHintError,
2222
getMatFormFieldMissingControlError,
23-
getMatFormFieldPlaceholderConflictError,
23+
getMatFormFieldPlaceholderConflictError, MAT_FORM_FIELD_DEFAULT_OPTIONS,
2424
MatFormField,
2525
MatFormFieldAppearance,
2626
MatFormFieldModule,
@@ -1218,6 +1218,77 @@ describe('MatInput with appearance', () => {
12181218
}));
12191219
});
12201220

1221+
describe('MatFormField default options', () => {
1222+
it('should be legacy appearance if no default options provided', fakeAsync(() => {
1223+
TestBed.configureTestingModule({
1224+
imports: [
1225+
FormsModule,
1226+
MatFormFieldModule,
1227+
MatInputModule,
1228+
NoopAnimationsModule,
1229+
PlatformModule,
1230+
],
1231+
declarations: [
1232+
MatInputWithAppearance,
1233+
],
1234+
});
1235+
1236+
TestBed.compileComponents();
1237+
1238+
const fixture = TestBed.createComponent(MatInputWithAppearance);
1239+
fixture.detectChanges();
1240+
flush();
1241+
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
1242+
}));
1243+
1244+
it('should be legacy appearance if empty default options provided', fakeAsync(() => {
1245+
TestBed.configureTestingModule({
1246+
imports: [
1247+
FormsModule,
1248+
MatFormFieldModule,
1249+
MatInputModule,
1250+
NoopAnimationsModule,
1251+
PlatformModule,
1252+
],
1253+
declarations: [
1254+
MatInputWithAppearance,
1255+
],
1256+
providers: [{provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {}}],
1257+
});
1258+
1259+
TestBed.compileComponents();
1260+
1261+
const fixture = TestBed.createComponent(MatInputWithAppearance);
1262+
fixture.detectChanges();
1263+
flush();
1264+
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
1265+
}));
1266+
1267+
it('should be custom default appearance if custom appearance specified in default options',
1268+
fakeAsync(() => {
1269+
TestBed.configureTestingModule({
1270+
imports: [
1271+
FormsModule,
1272+
MatFormFieldModule,
1273+
MatInputModule,
1274+
NoopAnimationsModule,
1275+
PlatformModule,
1276+
],
1277+
declarations: [
1278+
MatInputWithAppearance,
1279+
],
1280+
providers: [{provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}],
1281+
});
1282+
1283+
TestBed.compileComponents();
1284+
1285+
const fixture = TestBed.createComponent(MatInputWithAppearance);
1286+
fixture.detectChanges();
1287+
flush();
1288+
expect(fixture.componentInstance.formField.appearance).toBe('fill');
1289+
}));
1290+
});
1291+
12211292
@Component({
12221293
template: `
12231294
<mat-form-field>

0 commit comments

Comments
 (0)