Skip to content

Commit 94c3fed

Browse files
crisbetommalerba
authored andcommitted
feat(form-field): allow hideRequiredMarker default value to be… (#16244)
Allows for the `hideRequiredMarker` default value to be configured through the `MAT_FORM_FIELD_DEFAULT_OPTIONS` injection token. Fixes #16243.
1 parent 1f0ab1b commit 94c3fed

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export type MatFormFieldAppearance = 'legacy' | 'standard' | 'fill' | 'outline';
8484
*/
8585
export interface MatFormFieldDefaultOptions {
8686
appearance?: MatFormFieldAppearance;
87+
hideRequiredMarker?: boolean;
8788
}
8889

8990
/**
@@ -280,6 +281,8 @@ export class MatFormField extends _MatFormFieldMixinBase
280281

281282
// Set the default through here so we invoke the setter on the first run.
282283
this.appearance = (_defaults && _defaults.appearance) ? _defaults.appearance : 'legacy';
284+
this._hideRequiredMarker = (_defaults && _defaults.hideRequiredMarker != null) ?
285+
_defaults.hideRequiredMarker : false;
283286
}
284287

285288
/**

src/material/input/input.spec.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,32 +1450,47 @@ describe('MatInput with appearance', () => {
14501450
});
14511451

14521452
describe('MatFormField default options', () => {
1453-
it('should be legacy appearance if no default options provided', fakeAsync(() => {
1453+
it('should be legacy appearance if no default options provided', () => {
14541454
const fixture = createComponent(MatInputWithAppearance);
14551455
fixture.detectChanges();
1456-
flush();
14571456
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
1458-
}));
1457+
});
14591458

1460-
it('should be legacy appearance if empty default options provided', fakeAsync(() => {
1459+
it('should be legacy appearance if empty default options provided', () => {
14611460
const fixture = createComponent(MatInputWithAppearance, [{
14621461
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {}}
14631462
]);
14641463

14651464
fixture.detectChanges();
1466-
flush();
14671465
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
1468-
}));
1466+
});
1467+
1468+
it('should be able to change the default appearance', () => {
1469+
const fixture = createComponent(MatInputWithAppearance, [{
1470+
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}
1471+
]);
1472+
fixture.detectChanges();
1473+
expect(fixture.componentInstance.formField.appearance).toBe('fill');
1474+
});
1475+
1476+
it('should default hideRequiredMarker to false', () => {
1477+
const fixture = createComponent(MatInputWithAppearance, [{
1478+
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {}}
1479+
]);
1480+
1481+
fixture.detectChanges();
1482+
expect(fixture.componentInstance.formField.hideRequiredMarker).toBe(false);
1483+
});
1484+
1485+
it('should be able to change the default value of hideRequiredMarker', () => {
1486+
const fixture = createComponent(MatInputWithAppearance, [{
1487+
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {hideRequiredMarker: true}}
1488+
]);
1489+
1490+
fixture.detectChanges();
1491+
expect(fixture.componentInstance.formField.hideRequiredMarker).toBe(true);
1492+
});
14691493

1470-
it('should be custom default appearance if custom appearance specified in default options',
1471-
fakeAsync(() => {
1472-
const fixture = createComponent(MatInputWithAppearance, [{
1473-
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}
1474-
]);
1475-
fixture.detectChanges();
1476-
flush();
1477-
expect(fixture.componentInstance.formField.appearance).toBe('fill');
1478-
}));
14791494
});
14801495

14811496
describe('MatInput with textarea autosize', () => {

tools/public_api_guard/material/form-field.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export declare abstract class MatFormFieldControl<T> {
8181

8282
export interface MatFormFieldDefaultOptions {
8383
appearance?: MatFormFieldAppearance;
84+
hideRequiredMarker?: boolean;
8485
}
8586

8687
export declare class MatFormFieldModule {

0 commit comments

Comments
 (0)