Skip to content

Commit

Permalink
feat(form-field): allow hideRequiredMarker default value to be… (#16244)
Browse files Browse the repository at this point in the history
Allows for the `hideRequiredMarker` default value to be configured through the `MAT_FORM_FIELD_DEFAULT_OPTIONS` injection token.

Fixes #16243.
  • Loading branch information
crisbeto authored and mmalerba committed Aug 13, 2019
1 parent 1f0ab1b commit 94c3fed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/material/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type MatFormFieldAppearance = 'legacy' | 'standard' | 'fill' | 'outline';
*/
export interface MatFormFieldDefaultOptions {
appearance?: MatFormFieldAppearance;
hideRequiredMarker?: boolean;
}

/**
Expand Down Expand Up @@ -280,6 +281,8 @@ export class MatFormField extends _MatFormFieldMixinBase

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

/**
Expand Down
45 changes: 30 additions & 15 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,32 +1450,47 @@ describe('MatInput with appearance', () => {
});

describe('MatFormField default options', () => {
it('should be legacy appearance if no default options provided', fakeAsync(() => {
it('should be legacy appearance if no default options provided', () => {
const fixture = createComponent(MatInputWithAppearance);
fixture.detectChanges();
flush();
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
}));
});

it('should be legacy appearance if empty default options provided', fakeAsync(() => {
it('should be legacy appearance if empty default options provided', () => {
const fixture = createComponent(MatInputWithAppearance, [{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {}}
]);

fixture.detectChanges();
flush();
expect(fixture.componentInstance.formField.appearance).toBe('legacy');
}));
});

it('should be able to change the default appearance', () => {
const fixture = createComponent(MatInputWithAppearance, [{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}
]);
fixture.detectChanges();
expect(fixture.componentInstance.formField.appearance).toBe('fill');
});

it('should default hideRequiredMarker to false', () => {
const fixture = createComponent(MatInputWithAppearance, [{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {}}
]);

fixture.detectChanges();
expect(fixture.componentInstance.formField.hideRequiredMarker).toBe(false);
});

it('should be able to change the default value of hideRequiredMarker', () => {
const fixture = createComponent(MatInputWithAppearance, [{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {hideRequiredMarker: true}}
]);

fixture.detectChanges();
expect(fixture.componentInstance.formField.hideRequiredMarker).toBe(true);
});

it('should be custom default appearance if custom appearance specified in default options',
fakeAsync(() => {
const fixture = createComponent(MatInputWithAppearance, [{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}
]);
fixture.detectChanges();
flush();
expect(fixture.componentInstance.formField.appearance).toBe('fill');
}));
});

describe('MatInput with textarea autosize', () => {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/form-field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export declare abstract class MatFormFieldControl<T> {

export interface MatFormFieldDefaultOptions {
appearance?: MatFormFieldAppearance;
hideRequiredMarker?: boolean;
}

export declare class MatFormFieldModule {
Expand Down

0 comments on commit 94c3fed

Please sign in to comment.