Skip to content

Commit 976562f

Browse files
committed
fix(material/form-field): allow ng-container to be used as prefix/suffix (#26127)
* fix(material/form-field): allow ng-container to be used as text prefix/suffix * fixup! fix(material/form-field): allow ng-container to be used as text prefix/suffix (cherry picked from commit b3da94a)
1 parent aa0f0c4 commit 976562f

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

src/material/form-field/directives/prefix.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, InjectionToken} from '@angular/core';
9+
import {Directive, InjectionToken, Input} from '@angular/core';
1010

1111
/**
1212
* 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>('MatPrefix');
2121
providers: [{provide: MAT_PREFIX, useExisting: MatPrefix}],
2222
})
2323
export class MatPrefix {
24-
_isText = false;
25-
26-
constructor(elementRef: ElementRef) {
27-
this._isText = elementRef.nativeElement.hasAttribute('matTextPrefix');
24+
@Input('matTextPrefix')
25+
set _isTextSelector(value: '') {
26+
this._isText = true;
2827
}
28+
29+
_isText = false;
2930
}

src/material/form-field/directives/suffix.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, InjectionToken} from '@angular/core';
9+
import {Directive, InjectionToken, Input} from '@angular/core';
1010

1111
/**
1212
* 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>('MatSuffix');
2121
providers: [{provide: MAT_SUFFIX, useExisting: MatSuffix}],
2222
})
2323
export class MatSuffix {
24-
_isText = false;
25-
26-
constructor(elementRef: ElementRef) {
27-
this._isText = elementRef.nativeElement.hasAttribute('matTextSuffix');
24+
@Input('matTextSuffix')
25+
set _isTextSelector(value: '') {
26+
this._isText = true;
2827
}
28+
29+
_isText = false;
2930
}

src/material/input/input.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,21 @@ describe('MatMdcInput without forms', () => {
784784
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('favorite');
785785
}));
786786

787+
it('should allow ng-container as prefix and suffix', () => {
788+
const fixture = createComponent(InputWithNgContainerPrefixAndSuffix);
789+
fixture.detectChanges();
790+
791+
const textPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-prefix'))!;
792+
const textSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-suffix'))!;
793+
const iconPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-prefix'))!;
794+
const iconSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-suffix'))!;
795+
796+
expect(textPrefixEl.nativeElement.innerText.trim()).toEqual('text-prefix');
797+
expect(textSuffixEl.nativeElement.innerText.trim()).toEqual('text-suffix');
798+
expect(iconPrefixEl.nativeElement.innerText.trim()).toEqual('icon-prefix');
799+
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('icon-suffix');
800+
});
801+
787802
it('should update empty class when value changes programmatically and OnPush', fakeAsync(() => {
788803
let fixture = createComponent(MatInputOnPush);
789804
fixture.detectChanges();
@@ -2064,3 +2079,16 @@ class MatInputWithRequiredFormControl {
20642079
`,
20652080
})
20662081
class MatInputSimple {}
2082+
2083+
@Component({
2084+
template: `
2085+
<mat-form-field>
2086+
<ng-container matIconPrefix>icon-prefix</ng-container>
2087+
<ng-container matTextPrefix>text-prefix</ng-container>
2088+
<input matInput>
2089+
<ng-container matTextSuffix>text-suffix</ng-container>
2090+
<ng-container matIconSuffix>icon-suffix</ng-container>
2091+
</mat-form-field>
2092+
`,
2093+
})
2094+
class InputWithNgContainerPrefixAndSuffix {}

tools/public_api_guard/material/form-field.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,24 @@ export class MatLabel {
223223

224224
// @public
225225
export class MatPrefix {
226-
constructor(elementRef: ElementRef);
227226
// (undocumented)
228227
_isText: boolean;
229228
// (undocumented)
230-
static ɵdir: i0.ɵɵDirectiveDeclaration<MatPrefix, "[matPrefix], [matIconPrefix], [matTextPrefix]", never, {}, {}, never, never, false, never>;
229+
set _isTextSelector(value: '');
230+
// (undocumented)
231+
static ɵdir: i0.ɵɵDirectiveDeclaration<MatPrefix, "[matPrefix], [matIconPrefix], [matTextPrefix]", never, { "_isTextSelector": "matTextPrefix"; }, {}, never, never, false, never>;
231232
// (undocumented)
232233
static ɵfac: i0.ɵɵFactoryDeclaration<MatPrefix, never>;
233234
}
234235

235236
// @public
236237
export class MatSuffix {
237-
constructor(elementRef: ElementRef);
238238
// (undocumented)
239239
_isText: boolean;
240240
// (undocumented)
241-
static ɵdir: i0.ɵɵDirectiveDeclaration<MatSuffix, "[matSuffix], [matIconSuffix], [matTextSuffix]", never, {}, {}, never, never, false, never>;
241+
set _isTextSelector(value: '');
242+
// (undocumented)
243+
static ɵdir: i0.ɵɵDirectiveDeclaration<MatSuffix, "[matSuffix], [matIconSuffix], [matTextSuffix]", never, { "_isTextSelector": "matTextSuffix"; }, {}, never, never, false, never>;
242244
// (undocumented)
243245
static ɵfac: i0.ɵɵFactoryDeclaration<MatSuffix, never>;
244246
}

0 commit comments

Comments
 (0)