Skip to content

fix(form-field): hide required asterisk if control is disabled #8799

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

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
2 changes: 1 addition & 1 deletion src/lib/form-field/form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<span
class="mat-placeholder-required mat-form-field-required-marker"
aria-hidden="true"
*ngIf="!hideRequiredMarker && _control.required">&nbsp;*</span>
*ngIf="!hideRequiredMarker && _control.required && !_control.disabled">&nbsp;*</span>
</label>
</span>
</div>
Expand Down
17 changes: 15 additions & 2 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ describe('MatInput without forms', () => {
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);
}));

it('should hide the required star if input is disabled', () => {
const fixture = TestBed.createComponent(MatInputPlaceholderRequiredTestComponent);

fixture.componentInstance.disabled = true;
fixture.detectChanges();

const el = fixture.debugElement.query(By.css('label'));

expect(el).not.toBeNull();
expect(el.nativeElement.textContent).toMatch(/^hello$/);
});

it('should hide the required star from screen readers', fakeAsync(() => {
let fixture = TestBed.createComponent(MatInputPlaceholderRequiredTestComponent);
fixture.detectChanges();
Expand Down Expand Up @@ -1151,11 +1163,12 @@ class MatInputWithType {

@Component({
template: `<mat-form-field [hideRequiredMarker]="hideRequiredMarker">
<input matInput required placeholder="hello">
<input matInput required [disabled]="disabled" placeholder="hello">
</mat-form-field>`
})
class MatInputPlaceholderRequiredTestComponent {
hideRequiredMarker: boolean;
hideRequiredMarker: boolean = false;
disabled: boolean = false;
}

@Component({
Expand Down