Skip to content

Commit cb37c17

Browse files
committed
fix(material/form-field): allow ng-container to be used as prefix/suffix
1 parent 7d60159 commit cb37c17

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
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, ElementRef, 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 {}

0 commit comments

Comments
 (0)