Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(form-field): allow hideRequiredMarker default value to be configured #16244

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1455,32 +1455,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