diff --git a/src/material/form-field/directives/prefix.ts b/src/material/form-field/directives/prefix.ts index f60d3a9e9801..ba116b0c4ad7 100644 --- a/src/material/form-field/directives/prefix.ts +++ b/src/material/form-field/directives/prefix.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Directive, ElementRef, InjectionToken} from '@angular/core'; +import {Directive, InjectionToken, Input} from '@angular/core'; /** * Injection token that can be used to reference instances of `MatPrefix`. It serves as @@ -21,9 +21,10 @@ export const MAT_PREFIX = new InjectionToken('MatPrefix'); providers: [{provide: MAT_PREFIX, useExisting: MatPrefix}], }) export class MatPrefix { - _isText = false; - - constructor(elementRef: ElementRef) { - this._isText = elementRef.nativeElement.hasAttribute('matTextPrefix'); + @Input('matTextPrefix') + set _isTextSelector(value: '') { + this._isText = true; } + + _isText = false; } diff --git a/src/material/form-field/directives/suffix.ts b/src/material/form-field/directives/suffix.ts index 2c0ca14db3ca..411eb6bed8c2 100644 --- a/src/material/form-field/directives/suffix.ts +++ b/src/material/form-field/directives/suffix.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Directive, ElementRef, InjectionToken} from '@angular/core'; +import {Directive, InjectionToken, Input} from '@angular/core'; /** * Injection token that can be used to reference instances of `MatSuffix`. It serves as @@ -21,9 +21,10 @@ export const MAT_SUFFIX = new InjectionToken('MatSuffix'); providers: [{provide: MAT_SUFFIX, useExisting: MatSuffix}], }) export class MatSuffix { - _isText = false; - - constructor(elementRef: ElementRef) { - this._isText = elementRef.nativeElement.hasAttribute('matTextSuffix'); + @Input('matTextSuffix') + set _isTextSelector(value: '') { + this._isText = true; } + + _isText = false; } diff --git a/src/material/input/input.spec.ts b/src/material/input/input.spec.ts index ab31bfc1e3f7..3d2f9dd47ed6 100644 --- a/src/material/input/input.spec.ts +++ b/src/material/input/input.spec.ts @@ -784,6 +784,21 @@ describe('MatMdcInput without forms', () => { expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('favorite'); })); + it('should allow ng-container as prefix and suffix', () => { + const fixture = createComponent(InputWithNgContainerPrefixAndSuffix); + fixture.detectChanges(); + + const textPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-prefix'))!; + const textSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-suffix'))!; + const iconPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-prefix'))!; + const iconSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-suffix'))!; + + expect(textPrefixEl.nativeElement.innerText.trim()).toEqual('text-prefix'); + expect(textSuffixEl.nativeElement.innerText.trim()).toEqual('text-suffix'); + expect(iconPrefixEl.nativeElement.innerText.trim()).toEqual('icon-prefix'); + expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('icon-suffix'); + }); + it('should update empty class when value changes programmatically and OnPush', fakeAsync(() => { let fixture = createComponent(MatInputOnPush); fixture.detectChanges(); @@ -2064,3 +2079,16 @@ class MatInputWithRequiredFormControl { `, }) class MatInputSimple {} + +@Component({ + template: ` + + icon-prefix + text-prefix + + text-suffix + icon-suffix + + `, +}) +class InputWithNgContainerPrefixAndSuffix {} diff --git a/tools/public_api_guard/material/form-field.md b/tools/public_api_guard/material/form-field.md index dee619be1c24..161fba342f23 100644 --- a/tools/public_api_guard/material/form-field.md +++ b/tools/public_api_guard/material/form-field.md @@ -223,22 +223,24 @@ export class MatLabel { // @public export class MatPrefix { - constructor(elementRef: ElementRef); // (undocumented) _isText: boolean; // (undocumented) - static ɵdir: i0.ɵɵDirectiveDeclaration; + set _isTextSelector(value: ''); + // (undocumented) + static ɵdir: i0.ɵɵDirectiveDeclaration; // (undocumented) static ɵfac: i0.ɵɵFactoryDeclaration; } // @public export class MatSuffix { - constructor(elementRef: ElementRef); // (undocumented) _isText: boolean; // (undocumented) - static ɵdir: i0.ɵɵDirectiveDeclaration; + set _isTextSelector(value: ''); + // (undocumented) + static ɵdir: i0.ɵɵDirectiveDeclaration; // (undocumented) static ɵfac: i0.ɵɵFactoryDeclaration; }