Skip to content

Commit afa170e

Browse files
committed
fix(form-field): hide required asterisk if control is disabled
* No longer shows the required asterisk if the form control is disabled. Fixes #8251.
1 parent c3d7cd9 commit afa170e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/lib/form-field/form-field.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<span
4242
class="mat-placeholder-required mat-form-field-required-marker"
4343
aria-hidden="true"
44-
*ngIf="!hideRequiredMarker && _control.required">&nbsp;*</span>
44+
*ngIf="!hideRequiredMarker && _control.required && !_control.disabled">&nbsp;*</span>
4545
</label>
4646
</span>
4747
</div>

src/lib/input/input.spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ describe('MatInput without forms', () => {
413413
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);
414414
}));
415415

416+
it('should hide the required star if input is disabled', () => {
417+
const fixture = TestBed.createComponent(MatInputPlaceholderRequiredTestComponent);
418+
419+
fixture.componentInstance.disabled = true;
420+
fixture.detectChanges();
421+
422+
const el = fixture.debugElement.query(By.css('label'));
423+
424+
expect(el).not.toBeNull();
425+
expect(el.nativeElement.textContent).toMatch(/^hello$/);
426+
});
427+
416428
it('should hide the required star from screen readers', fakeAsync(() => {
417429
let fixture = TestBed.createComponent(MatInputPlaceholderRequiredTestComponent);
418430
fixture.detectChanges();
@@ -1151,11 +1163,12 @@ class MatInputWithType {
11511163

11521164
@Component({
11531165
template: `<mat-form-field [hideRequiredMarker]="hideRequiredMarker">
1154-
<input matInput required placeholder="hello">
1166+
<input matInput required [disabled]="disabled" placeholder="hello">
11551167
</mat-form-field>`
11561168
})
11571169
class MatInputPlaceholderRequiredTestComponent {
1158-
hideRequiredMarker: boolean;
1170+
hideRequiredMarker: boolean = false;
1171+
disabled: boolean = false;
11591172
}
11601173

11611174
@Component({

0 commit comments

Comments
 (0)